applicationContext.xml配置
一、<context:component-scan>使用说明
1、启用注解扫描,并定义组件查找规则,mvc只负责扫描@Controller
<context:component-scan base-package="com.jjapu.web.controller" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
参数说明: 在context:component-scan可以添加use-default-filters,spring配置中的use-default- filters用来指示是否自动扫描带有@Component、@Repository、@Service和@Controller的类。默认为true,即默认扫描以上四个注解。
2、如果想要过滤其中这四个注解中的一个,比如@Repository,可以添加<context:exclude-filter />子标签,例如:
<context:component-scan base-package="com.jiapu.web" scoped-proxy="targetClass"
use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
/context:component-scan>
参数说明:<context:include-filter/>子标签是用来添加扫描注解的。
3、但是如果添加和排除的是相同,则必须include-filter在前,exclude-filter在后,否则配置不符合spring -context-3.0.xsd要求,Spring容器解析时会出错。上面的配置会把@Repository注解的类排除掉。
<context:component-scan base-package="tv.crm" scoped-proxy="targetClass"
use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
参考很多配置资料总结使用:
<context:component-scan base-package="com.jiapu.web" use-default-filters="false"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
这个只扫描com.jiapu.web包下的@Controller,不会扫描@Service、@Repository
<context:component-scan base-package="com.jiapu">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
这个不但扫描com.jaamy包下的@Controller,同时也会扫描@Service、@Repository,注意这里没有添加use-default-filters参数
关于include-filter、exclude-filter的作用主要是用来过滤扫描到的bean是否合法:
首先通过exclude-filter进行黑名单过滤;
然后通过include-filter进行白名单过滤;
在自动扫描里面我们在配置<context:include-filter/>,然后type=“assignable”,这里的意思是,BaseInterface接口所派生出来的所有类,都自动注册bean
type的四种参数:
assignable-指定扫描某个接口派生出来的类
annotation-指定扫描使用某个注解的类
aspectj-指定扫描AspectJ表达式相匹配的类
custom-指定扫描自定义的实现了org.springframework.core.type.filter.TypeFilter接口的类
regex-指定扫描符合正则表达式的类
二、<context:annotation-config>使用学习
<context:annotation-config>是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)。
context:component-scan>除了具有<context:annotation-config>的功能之外,<context:component-scan>还可以在指定的package下扫描以及注册javabean 。
1、<context:annotation-config/>配置作用:他的作用是式地向 Spring 容器注册 AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、
PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor 这 4 个BeanPostProcessor。注册这4个 BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。
例如:
如果你想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。传统声明方式如下
<bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/>
如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor
如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。
如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。同样,传统的声明方式如下:
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
一般来说,这些注解我们还是比较常用,尤其是Antowired的注解,在自动注入的时候更是经常使用,所以如果总是需要按照传统的方式一条一条配置显得有些繁琐和没有必要,于是spring给我们提供<context:annotation-config/>的简化配置方式,自动帮你完成声明。
不过,我们使用注解一般都会配置扫描包路径选项
<context:component-scan base-package=”XX.XX”/>
该配置项其实也包含了自动注入上述processor的功能,因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了