context:component-scan 配置详解
2015-08-24 09:06
253 查看
<转>
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。而現在如下的飯粒:
Java代码
<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:component-scan>提供兩個子標籤:<context:include-filter>和<context:exclude-filter>各代表引入和排除的過濾。而上例把use-default-filters屬性設為false,意即在base-package所有被宣告為@Component和@Configuration等target Class不予註冊為bean,由filter子標籤代勞。
filter標籤在Spring3有五個type,如下:
custom org.example.MyTypeFilter Spring3新增自訂Type,實作org.springframework.core.type.TypeFilter
所以上例用的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。
2010/3/18補充:後來在AppConfig前忘了加@Component,AppConfig內尚留有@Bean,奇怪的是還是能work。我猜有加@Bean的method的class,若沒特別註解AppConfig是@Repository、@Service還是@Configuration,一律被Spring3視為@Component。
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。而現在如下的飯粒:
Java代码
<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: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 |
custom org.example.MyTypeFilter Spring3新增自訂Type,實作org.springframework.core.type.TypeFilter
所以上例用的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。
2010/3/18補充:後來在AppConfig前忘了加@Component,AppConfig內尚留有@Bean,奇怪的是還是能work。我猜有加@Bean的method的class,若沒特別註解AppConfig是@Repository、@Service還是@Configuration,一律被Spring3視為@Component。
相关文章推荐
- springMVC No mapping found for HTTP request with URI
- Java中SerialVersionUID的作用
- POJ 1258 Agri-Net(最小生成树-Prim)
- c#读取文件一行一行读取
- zookeeper集群环境的搭建
- 【自动内存管理(ARC)】
- LA 3027 并查集
- ECMAScript之操作符重点小解
- abstract的方法是否可同时是static 是否可同时是native 是否可同时
- UIScrollView
- php实现递归的三种基本方式
- MySQL的安装过程
- Android开发初识Volley的基本用法
- arcgis发布几何服务(geometryService)
- 第二十三天 TextView、Button
- 分享让人折服的优秀代码基因
- 九度oj1514数值的整数次方
- leetcode题解
- Hibernate的三种缓存机制
- Ubuntu之常用命令