码迷,mamicode.com
首页 > 其他好文 > 详细

include-filter和exclude-filter的区别

时间:2017-11-04 14:54:52      阅读:880      评论:0      收藏:0      [点我收藏+]

标签:profile   size   start   代码   信息   判断   根据   ott   data   

include-filter和exclude-filter的区别:

前者是扫描,后者是排除扫描。
下面是父子容器配置时需要注意的。




如下方式可以成功扫描到@Controller注解的Bean,不会扫描@Service/@Repository的Bean。正确

 

Java代码  技术分享
  1. package="org.bdp.system.test.controller">   
  2. "annotation" expression="org.springframework.stereotype.Controller"/>   
  3. </context:component-scan>  

  

但是如下方式,不仅仅扫描@Controller,还扫描@Service/@Repository的Bean,可能造成一些问题

 

Java代码  技术分享
  1. package="org.bdp">   
  2. "annotation" expression="org.springframework.stereotype.Controller"/>   
  3. </context:component-scan>  

 

这个尤其在springmvc+spring+hibernate等集成时最容易出问题的地,最典型的错误就是:

事务不起作用

 

这是什么问题呢?

分析

1、<context:component-scan>会交给org.springframework.context.config.ContextNamespaceHandler处理;

 

Java代码  技术分享
  1. new ComponentScanBeanDefinitionParser());  

 

2、ComponentScanBeanDefinitionParser会读取配置文件信息并组装成org.springframework.context.annotation.ClassPathBeanDefinitionScanner进行处理;

3、如果没有配置<context:component-scan>的use-default-filters属性,则默认为true,在创建ClassPathBeanDefinitionScanner时会根据use-default-filters是否为true来调用如下代码:

 

Java代码  技术分享
  1. protected void registerDefaultFilters() {  
  2. this.includeFilters.add(new AnnotationTypeFilter(Component.class));  
  3. class.getClassLoader();  
  4. try {  
  5. this.includeFilters.add(new AnnotationTypeFilter(  
  6. extends Annotation>) cl.loadClass("javax.annotation.ManagedBean")), false));  
  7. "JSR-250 ‘javax.annotation.ManagedBean‘ found and supported for component scanning");  
  8. catch (ClassNotFoundException ex) {  
  9. // JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.  
  10. try {  
  11. this.includeFilters.add(new AnnotationTypeFilter(  
  12. extends Annotation>) cl.loadClass("javax.inject.Named")), false));  
  13. "JSR-330 ‘javax.inject.Named‘ annotation found and supported for component scanning");  
  14. catch (ClassNotFoundException ex) {  
  15. // JSR-330 API not available - simply skip.  
  16. }  

 

 

可以看到默认ClassPathBeanDefinitionScanner会自动注册对@Component、@ManagedBean、@Named注解的Bean进行扫描。如果细心,到此我们就找到问题根源了。

 

 

4、在进行扫描时会通过include-filter/exclude-filter来判断你的Bean类是否是合法的:

 

Java代码  技术分享
  1. protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {  
  2. for (TypeFilter tf : this.excludeFilters) {  
  3. if (tf.match(metadataReader, this.metadataReaderFactory)) {  
  4. return false;  
  5.     }  
  6. for (TypeFilter tf : this.includeFilters) {  
  7. if (tf.match(metadataReader, this.metadataReaderFactory)) {  
  8.             if (!metadata.isAnnotated(Profile.class.getName())) {  
  9. return true;  
  10.             AnnotationAttributes profile = MetadataUtils.attributesFor(metadata, Profile.class);  
  11. return this.environment.acceptsProfiles(profile.getStringArray("value"));  
  12.     }  
  13. return false;  
  14. }  

 

首先通过exclude-filter 进行黑名单过滤;

然后通过include-filter 进行白名单过滤;

否则默认排除。

 

结论

Java代码  技术分享
  1. package="org.bdp">   
  2. "annotation" expression="org.springframework.stereotype.Controller"/>   
  3. </context:component-scan>  

 

为什么这段代码不仅仅扫描@Controller注解的Bean,而且还扫描了@Component的子注解@Service、@Reposity。因为use-default-filters默认为true。所以如果不需要默认的,则use-default-filters=“false”禁用掉。

include-filter和exclude-filter的区别

标签:profile   size   start   代码   信息   判断   根据   ott   data   

原文地址:http://www.cnblogs.com/hanguocai/p/7783078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!