Spring应用 1 springXML配置说明
隐式对Spring容器注册Process
<
context:annotation-config
/>
为了在spring开发过程中,为了简化开发过程。会使用注解形式的开发模式。但使用相应的注解需要spring.xml中定义相应的BeanProcessor,这样显得很笨重。
使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>
使用 @Required注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/
类似地,使用@Resource、@PostConstruct、@PreDestroy等注解就必须声明 CommonAnnotationBeanPostProcessor;
使用@PersistenceContext注解,就必须声明 PersistenceAnnotationBeanPostProcessor的Bean。
Spring为我们提供了一种极为方便注册这些BeanPostProcessor的方式,即使用<context:annotation- config/>
隐式地向Spring容器注册AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor以及PersistenceAnnotationBeanPostProcessor这4个BeanPostProcessor。
<context:annotation-config/>
自动扫描
<
context:component-scan
base-package
=""/>
@ComponentScan
Spring可以自动去扫描base-package下面或者子包下面的java文件。 如果扫描到@Component@Controller@Service这些注解的类,会将这些类注册成spring bean,存放在spring容器中。
该配置项其实也包含了自动注入上述processor的功能,因此当使用<context:component-scan/>后,即可将<context:annotation-config/>省去。
use-default-filters属性
:
属性默认
为true,
这就意味着会扫描指定包下的全部的标有
@Component
的类,并注册成
bean.
也就是
@Component
的子注解
@Service,@Reposity
等
。设置为false需要自己指定需要什么注解。使用include-filter。
<context:include-filter>
<context:exclude-filter>子标签:可以随意调整扫描行为
一般情况下:use-default-filter为true时使用<context:exclude-filter>指定的不扫描,
use-default-filter为false时使用
<context:include-filter>指定的扫描。
<!-- 过滤掉使用@Controller的类-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<!-- 过滤掉exampleBean下的且类名以Default开头的类 -->
<context:exclude-filter type="regex" expression="exampleBean\.Default*" />
<!-- 过滤掉exampleBean下的类名为MovieFinder的类。也可以使用正则进行全匹配,效果一样 -->
<context:exclude-filter type="assignable" expression="exampleBean.MovieFinder"/>
前提:这些类必须被@Component或者子注解所修饰
annotation:过滤器扫描使用指定注解描述的那些类。通过expression描述指定注解的类名。
assignable :过滤器扫描派生于expression描述类(子类或者自己,可以描述为接口,扫描所有实现类)
aspectj :过滤器扫描与expression熟悉指定的AspectJ表达式所匹配的那些类
regex :
过滤器扫描类名与expression熟悉指定的正则表达式所匹配的那些类
custom :没卵用 spring in action p80
使用注解形式的配置
@ComponentScan(value="com.myhexin",
excludeFilters={@Filter(type=FilterType.REGEX,pattern={"com.myhexin.service.framework.core.Container",
"com.myhexin.http.impl.PersistenceServiceImpl","com.myheixin.cfg.SpringConfig"}),
@Filter(type=FilterType.ANNOTATION,value=Service.class)},
useDefaultFilters=true)