Spring 自动扫描
2017-04-28 本文已影响47人
ChanHsu
1.自动组件扫描注释类型
在Spring2.5中,有4种类型的组件自动扫描注释类型
- @Component – 指示自动扫描组件。
- @Repository – 表示在持久层DAO组件。
- @Service – 表示在业务层服务组件。
- @Controller – 表示在表示层控制器组件。
<context:component-scan base-package="com.srpingapp.customer" />
2过滤器组件自动扫描
2.1过滤组件 - 包含
“过滤” 扫描并注册匹配定义“regex”,即使该类组件的名称未标注 @Componen
<context:component-scan base-package="com.springapp" >
<context:include-filter type="regex"
expression="com.springapp.customer.dao.*DAO.*" />
<context:include-filter type="regex"
expression="com.springapp.customer.services.*Service.*" />
</context:component-scan>
2.2过滤组件 - 不包含
排除指定组件,以避免 Spring 检测和 Spring 容器注册。
2.2.1不包括在这些文件中标注有 @Service 。
<context:component-scan base-package="com.springapp.customer" >
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan>
2.2.2不包括那些包含DAO这个词组文件名
<context:component-scan base-package="com.springapp" >
<context:exclude-filter type="regex"
expression="com.springapp.customer.dao.*DAO.*" />
</context:component-scan>