spring组件扫描<context:component-scan/>使用详解
来源:互联网 发布:淘宝代码生成器 编辑:程序博客网 时间:2024/06/07 09:16
spring组件扫描<context:component-scan/>使用详解
<context:component-scan base-package="leot.test" use-default-filters="false">
</context:component-scan>
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:component-scan>提供兩個子標籤:<context:include-filter>和<context:exclude-filter>各代表引入和排除的過濾。而上例把use-default-filters屬性設為false,意即在base-package所有被宣告為@Component和@Configuration等target Class不予註冊為bean,由filter子標籤代勞。
filter標籤在Spring3有五個type,如下:
Filter TypeExamples ExpressionDescriptionannotationorg.example.SomeAnnotation符合SomeAnnoation的target classassignableorg.example.SomeClass指定class或interface的全名aspectjorg.example..*Service+AspectJ語法regexorg\.example\.Default.*Regelar Expressioncustomorg.example.MyTypeFilterSpring3新增自訂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。
我按他的例子,配置了我自己的如下:
</context:component-scan>
</context:component-scan>
</context:component-scan>问题依旧
</context:component-scan>
</context:component-scan>
- spring组件扫描<context:component-scan/>详解
- spring组件扫描<context:component-scan/>使用详解
- spring组件扫描<context:component-scan/>使用详解
- spring组件扫描<context:component-scan/>使用详解
- spring组件扫描<context:component-scan/>使用详解
- spring组件扫描<context:component-scan/>使用详解
- spring组件扫描<context:component-scan/>使用详解
- Spring组件扫描<context:component-scan/>使用详解
- spring组件扫描<context:component-scan/>使用详解 .
- spring组件扫描<context:component-scan/>使用详解
- spring组件扫描<context:component-scan/>使用详解
- spring组件扫描<context:component-scan/>使用详解
- spring组件扫描<context:component-scan/>使用详解
- spring组件扫描<context:component-scan/>使用详解
- spring组件扫描<context:component-scan/>使用详解
- spring组件扫描<context:component-scan/>使用详解
- spring组件扫描<context:component-scan/>使用详解
- spring组件扫描<context:component-scan/>使用详解
- equals() ,hashcode() 使用
- 对与几种javax.el.PropertyNotFoundException: Property 'XXX' not found on type java.lang.String问题原因
- 本年哪一天?
- UVa 10714 - Ants
- 在Sandbox(沙盒)中使用PeoplePicker
- spring组件扫描<context:component-scan/>使用详解
- Centos 6.4 搭建svn
- Gleasy CTO薛珂:成立3年,企业用户2万+,移动办公蕴藏较大发展机遇
- 如何在英特尔® 架构手机上为 Miracast* 实现英特尔® 无线显示差异化
- OpenLayers基础知识(二)
- Cannot generate SSPI context
- 三种工厂模式的C++实现
- synchronized
- 解决infobright 的group by 因为NULL值,会出现两条一样的结果的问题