spring组件扫描<context:component-scan/>使用详解
2015-11-11 09:07
561 查看
关于spring自动检测组件的使用方式网上太多了,而且也不是我记录的重点,我想说下一点可能你还不知道的经验
我们知道如果不想在xml文件中配置bean,我们可以给我们的类加上spring组件注解,只需再配置下spring的扫描器就可以实现bean的自动载入。
下面是引用spring framework开发手册中的一段话 “
Spring 2.5引入了更多典型化注解(stereotype annotations):
下面是网上目前关于组件扫描最详细的介绍
Spring applicationContext.xml的<context:component-scan >标签用途比我想像的还要实用。而且后来才知道,有了<context:component-scan >,另一个<context:annotation-config/>标签根本可以移除掉,因为被包含进去了。原本我survery Spring3通常只配置成<context:component-scan base-package="com.foo.bar"/>,意即在base-package下寻找有@Component和@Configuration的target Class。而现在如下的饭粒:
<context:component-scan
>
提供两个子标签:<context:include-filter>和<context:exclude-filter>各代表
引入和排除的过滤。而上例把use-default-filters属性设为false,意即在base-package所有被宣告为
@Component和@Configuration等target
Class不予注册为bean,由filter子标签代劳。
filter标签在Spring3有五个type,如下:
所以上例用的regex就有个语病,com.foo.config.*
可以找到com.foo.config.WebLogger,但也可以找到com1fool2config3abcde,因为小数点在Regex是任意字
元,是故要用\.把小数点跳脱为佳。(2010/3/15补充:但要使用\.方式,其use-default-filters不能为false,否则抓不
到,感觉是Bug)
Spring3提供丰富的Filter支援,有益配置策略,不需面临Configuration
Hell,比如Regex的com\.foo\.*\.action\.*Config,这样就可以找到com.foo
package下所有action子package的*Config的target class。
我按他的例子,配置了我自己的如下:
<context:component-scan
base-package="com.xhlx.finance.budget"
>
<context:include-filter
type="regex"
expression="com.lee.finance.budget.service.*"/>
<context:include-filter
type="regex"
expression="com.lee.finance.budget.security.*"/>
</context:component-scan
>
但是死活扫描不到,网上又没有更好的讲解,没办法只好自己试,改成
<context:component-scan
base-package="com.xhlx.finance.budget" >
<context:include-filter
type="regex"
expression="com.lee.finance.budget.*"/>
</context:component-scan
>
这样连没有加注解的类也扫描并实例化,结果报错,因为有的类根本就没有默认的构造函数不能实例化,能不报错吗
改成
<context:component-scan
base-package="com.xhlx.finance.budget"
>
<context:include-filter
type="regex"
expression="com\.lee\.finance\.budget\.service.*"/>
</context:component-scan
>问题依旧
<context:component-scan
base-package="com.xhlx.finance.budget" >
<context:include-filter
type="regex"
expression="com.lee.finance.budget.service.TestService"/>
</context:component-scan
>
嘿,这次可以了,写全限定名就可以,表达式却不行,但是如果我有成千上百个还得一个一个这样配置啊?不行,继续研究,我想有个base-package的配置,从表面意思来看这是个“基本包”,是否表示下面的过滤路径是基于这个包的呢?于是试着改成
<context:component-scan
base-package="com.xhlx.finance.budget" >
<context:include-filter
type="regex" expression=".service.*"/>
</context:component-scan
>
我们知道如果不想在xml文件中配置bean,我们可以给我们的类加上spring组件注解,只需再配置下spring的扫描器就可以实现bean的自动载入。
下面是引用spring framework开发手册中的一段话 “
Spring 2.5引入了更多典型化注解(stereotype annotations):
@Component、
@Service和
@Controller。
@Component是所有受Spring管理组件的通用形式; 而
@Repository、
@Service和
@Controller则是
@Component的细化, 用来表示更具体的用例(例如,分别对应了持久化层、服务层和表现层)。也就是说, 你能用
@Component来注解你的组件类, 但如果用
@Repository、
@Service或
@Controller来注解它们,你的类也许能更好地被工具处理,或与切面进行关联。 例如,这些典型化注解可以成为理想的切入点目标。当然,在Spring Framework以后的版本中,
@Repository、
@Service和
@Controller也许还能携带更多语义。如此一来,如果你正在考虑服务层中是该用
@Component还是
@Service, 那
@Service显然是更好的选择。同样的,就像前面说的那样,
@Repository已经能在持久化层中进行异常转换时被作为标记使用了。”
下面是网上目前关于组件扫描最详细的介绍
Spring applicationContext.xml的<context:component-scan >标签用途比我想像的还要实用。而且后来才知道,有了<context:component-scan >,另一个<context:annotation-config/>标签根本可以移除掉,因为被包含进去了。原本我survery Spring3通常只配置成<context:component-scan base-package="com.foo.bar"/>,意即在base-package下寻找有@Component和@Configuration的target Class。而现在如下的饭粒:
<context:component-scan base-package="com.foo" use-default-filters ="false"> <context:include-filter type="regex" expression="com.foo.bar.*Config"/> <context:include-filter type="regex" expression="com.foo.config.*"/> </context:component-scan > |
>
提供两个子标签:<context:include-filter>和<context:exclude-filter>各代表
引入和排除的过滤。而上例把use-default-filters属性设为false,意即在base-package所有被宣告为
@Component和@Configuration等target
Class不予注册为bean,由filter子标签代劳。
filter标签在Spring3有五个type,如下:
Filter Type | Examples Expression | Description |
annotation | org.example.SomeAnnotation | 符合SomeAnnoation的target class |
assignable | org.example.SomeClass | 指定class或interface的全名 |
aspectj | org.example..*Service+ | AspectJ语法 |
regex | org\.example\.Default.* | Regelar Expression |
custom | org.example.MyTypeFilter | Spring3新增自订Type,实作org.springframework.core.type.TypeFilter |
可以找到com.foo.config.WebLogger,但也可以找到com1fool2config3abcde,因为小数点在Regex是任意字
元,是故要用\.把小数点跳脱为佳。(2010/3/15补充:但要使用\.方式,其use-default-filters不能为false,否则抓不
到,感觉是Bug)
Spring3提供丰富的Filter支援,有益配置策略,不需面临Configuration
Hell,比如Regex的com\.foo\.*\.action\.*Config,这样就可以找到com.foo
package下所有action子package的*Config的target class。
我按他的例子,配置了我自己的如下:
<context:component-scan
base-package="com.xhlx.finance.budget"
>
<context:include-filter
type="regex"
expression="com.lee.finance.budget.service.*"/>
<context:include-filter
type="regex"
expression="com.lee.finance.budget.security.*"/>
</context:component-scan
>
但是死活扫描不到,网上又没有更好的讲解,没办法只好自己试,改成
<context:component-scan
base-package="com.xhlx.finance.budget" >
<context:include-filter
type="regex"
expression="com.lee.finance.budget.*"/>
</context:component-scan
>
这样连没有加注解的类也扫描并实例化,结果报错,因为有的类根本就没有默认的构造函数不能实例化,能不报错吗
改成
<context:component-scan
base-package="com.xhlx.finance.budget"
>
<context:include-filter
type="regex"
expression="com\.lee\.finance\.budget\.service.*"/>
</context:component-scan
>问题依旧
<context:component-scan
base-package="com.xhlx.finance.budget" >
<context:include-filter
type="regex"
expression="com.lee.finance.budget.service.TestService"/>
</context:component-scan
>
嘿,这次可以了,写全限定名就可以,表达式却不行,但是如果我有成千上百个还得一个一个这样配置啊?不行,继续研究,我想有个base-package的配置,从表面意思来看这是个“基本包”,是否表示下面的过滤路径是基于这个包的呢?于是试着改成
<context:component-scan
base-package="com.xhlx.finance.budget" >
<context:include-filter
type="regex" expression=".service.*"/>
</context:component-scan
>
相关文章推荐
- java基础
- Spring IOC容器
- springboot 项目运行jsp
- 深入解析Java中的Classloader的运行机制
- 利用Hbase解决HDFS小文件合并
- 使用Spring框架的好处(转帖)
- myeclipse10.7破解成功 但 无法打war包 securecrt alert:integrity check error 解决方案
- MyEclipse新建工程中的Source Folder
- java集合之TreeSet
- java集合之HashSet
- struts2 hibernate登录
- eclipse jdk adt sdk abtbundle之间的关系
- Struts2中表单与Action传递数据三种方式
- struts2.0获取各种表单的数据
- Struts2中Action访问Servlet API的三种方法
- Struts2的Action和Servlet有什么联系,区别?
- java常量池简单使用
- 实战struts2——用户登录
- struts2下的helloworld(如何让第一个struts2跑起来)——struts2第一讲
- Java file read & write