`
jinnianshilongnian
  • 浏览: 21439798 次
  • 性别: Icon_minigender_1
博客专栏
5c8dac6a-21dc-3466-8abb-057664ab39c7
跟我学spring3
浏览量:2406102
D659df3e-4ad7-3b12-8b9a-1e94abd75ac3
Spring杂谈
浏览量:2998575
43989fe4-8b6b-3109-aaec-379d27dd4090
跟开涛学SpringMVC...
浏览量:5632108
1df97887-a9e1-3328-b6da-091f51f886a1
Servlet3.1规范翻...
浏览量:257776
4f347843-a078-36c1-977f-797c7fc123fc
springmvc杂谈
浏览量:1593485
22722232-95c1-34f2-b8e1-d059493d3d98
hibernate杂谈
浏览量:249078
45b32b6f-7468-3077-be40-00a5853c9a48
跟我学Shiro
浏览量:5848368
Group-logo
跟我学Nginx+Lua开...
浏览量:698415
5041f67a-12b2-30ba-814d-b55f466529d5
亿级流量网站架构核心技术
浏览量:780838
社区版块
存档分类
最新评论

context:component-scan扫描使用上的容易忽略的use-default-filters

 
阅读更多

问题

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

 

 <context:component-scan base-package="org.bdp.system.test.controller"> 
     <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
</context:component-scan>

  

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

 

 <context:component-scan base-package="org.bdp"> 
     <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
</context:component-scan>

 

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

事务不起作用

 

这是什么问题呢?

分析

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

 

registerBeanDefinitionParser("component-scan", new ComponentScanBeanDefinitionParser());

 

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

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

 

    protected void registerDefaultFilters() {
		this.includeFilters.add(new AnnotationTypeFilter(Component.class));
		ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
		try {
			this.includeFilters.add(new AnnotationTypeFilter(
					((Class<? extends Annotation>) cl.loadClass("javax.annotation.ManagedBean")), false));
			logger.info("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
		}
		catch (ClassNotFoundException ex) {
			// JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.
		}
		try {
			this.includeFilters.add(new AnnotationTypeFilter(
					((Class<? extends Annotation>) cl.loadClass("javax.inject.Named")), false));
			logger.info("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
		}
		catch (ClassNotFoundException ex) {
			// JSR-330 API not available - simply skip.
		}
	}

 

 

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

 

 

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

 

	protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {
		for (TypeFilter tf : this.excludeFilters) {
			if (tf.match(metadataReader, this.metadataReaderFactory)) {
				return false;
			}
		}
		for (TypeFilter tf : this.includeFilters) {
			if (tf.match(metadataReader, this.metadataReaderFactory)) {
				AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
				if (!metadata.isAnnotated(Profile.class.getName())) {
					return true;
				}
				AnnotationAttributes profile = MetadataUtils.attributesFor(metadata, Profile.class);
				return this.environment.acceptsProfiles(profile.getStringArray("value"));
			}
		}
		return false;
	}

 

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

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

否则默认排除。

 

结论

<context:component-scan base-package="org.bdp"> 
     <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
</context:component-scan>

 

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

 

 

请参考

《SpringMVC + spring3.1.1 + hibernate4.1.0 集成及常见问题总结》 

《第三章 DispatcherServlet详解 ——跟开涛学SpringMVC》中的ContextLoaderListener初始化的上下文和DispatcherServlet初始化的上下文关系。

 

如果在springmvc配置文件,不使用cn.javass.demo.web.controller前缀,而是使用cn.javass.demo,则service、dao层的bean可能也重新加载了,但事务的AOP代理没有配置在springmvc配置文件中,从而造成新加载的bean覆盖了老的bean,造成事务失效。只要使用use-default-filters=“false”禁用掉默认的行为就可以了。

 

问题不难,spring使用上的问题。总结一下方便再遇到类似问题的朋友参考。

分享到:
评论
7 楼 sendreams 2013-07-10  
tao兄,我有点不明白,scan的设置跟事务不起作用有什么关系呢?假设不过滤,全部扫描进去又有什么问题呢?
6 楼 Motte2010 2013-06-25  
jinnianshilongnian 写道
Motte2010 写道
你好
我刚才试了一个自己的小例子,一个用户注册,用注解实现service dao的注入
首先,在 context:component-scan中 我不配置use-default-filters,使用默认的true,能实现service  dao的注入。
然后我加上 use-default-filters="false"
我发现service 反到不能注入了。容器压根就没有把service实例化(Spring默认的单例,初始化容器时会实例化)
请问tao哥(tao的中文为什么被iteye禁止?),这个是什么原因了,和你上面说的有冲突吗?还是你上面的配置必须是基于某种情况下才能使用。

use-default-filters 会自动注册@Component扫描(即@Service等都是它的子),如果use-default-filters="false" --> 那么你需要自己指定扫描哪些

比如
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> 
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/> 

哦 是这样的。。。非常感谢你耐心解答我的一堆的疑问。。
5 楼 jinnianshilongnian 2013-06-25  
Motte2010 写道
你好
我刚才试了一个自己的小例子,一个用户注册,用注解实现service dao的注入
首先,在 context:component-scan中 我不配置use-default-filters,使用默认的true,能实现service  dao的注入。
然后我加上 use-default-filters="false"
我发现service 反到不能注入了。容器压根就没有把service实例化(Spring默认的单例,初始化容器时会实例化)
请问tao哥(tao的中文为什么被iteye禁止?),这个是什么原因了,和你上面说的有冲突吗?还是你上面的配置必须是基于某种情况下才能使用。

use-default-filters 会自动注册@Component扫描(即@Service等都是它的子),如果use-default-filters="false" --> 那么你需要自己指定扫描哪些

比如
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> 
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/> 
4 楼 Motte2010 2013-06-25  
你好
我刚才试了一个自己的小例子,一个用户注册,用注解实现service dao的注入
首先,在 context:component-scan中 我不配置use-default-filters,使用默认的true,能实现service  dao的注入。
然后我加上 use-default-filters="false"
我发现service 反到不能注入了。容器压根就没有把service实例化(Spring默认的单例,初始化容器时会实例化)
请问tao哥(tao的中文为什么被iteye禁止?),这个是什么原因了,和你上面说的有冲突吗?还是你上面的配置必须是基于某种情况下才能使用。
3 楼 kevinhrw 2013-01-26  
真的,谢谢。以前一直是使用多个包名来解决这个问题。
如:
<context:component-scan base-package="com.abc.webapp,com.abc.ems.webapp,com.abc.pm.webapp,com.abc.schedule.webapp">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

真是又长又臭。
2 楼 greenhunter 2013-01-08  
以前还真没研究过这里。现在终于知道了。多谢!!!!
1 楼 grzrt 2013-01-08  
       

相关推荐

Global site tag (gtag.js) - Google Analytics