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使用上的问题。总结一下方便再遇到类似问题的朋友参考。
***这个尤其在springmvc+spring+hibernate等集成时最容易出问题的地,最典型的错误就是:
事务不起作用***
牢牢记住涛涛这句话
spring getBean的时候会先从parent factroy中获取bean,正常配置时SpringMVC的web applicationContext都会继承applicationContext.xml中的root applicationContext
原文中对这个部分的说明,缺少了**指定了use-default-filters=“false”**这个前提,导致理解很混乱,还好在回复中看到了这个答案
我试了, 在applicationContext和XX-servlet中都使用use-default-fileter=true,就是两个xml配置文件中的component-scan元素的配置一样, 事务还是有效的。 不知道是不是现在新版本有优化过这个问题,还是你的文章不太对啊。
这篇文章有点让人糊涂啊,所谓中心不突出导致的 作者本身应该是想说 use-default-filters="true"(默认值就是true) 它的作用就是扫描一些相关的注解,包括了@controller,@Component,@Service,@Repository等,
而use-default-filters="false",同时使用白名单context:include-filter对指定的注解进行扫描 如:
这就表示只扫描"org.bdp.system.test.controller"下的Controller注解
还有就是作者说到的事物失效,应该是applicationContext.xml中已经扫描过@Service和@Repository,而在xx-servlet.xml扫描@Controller时就不应该再去扫描@Service和@Repository,不然就会导致事物失效的情况
以上,纯属自己理解,如有错误请及时指正,以免继续误导人
下面弄错了。是配置的问题
使用了“<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> ”会导致 <mvc:resources location="/resource/" mapping="/resource/" />报异常:
菜鸟有问题,劳烦大神解答,为什么要把controller和service等其他注解分开注入?比如 <context:component-scan base-package="com"/> context:annotation-config/ 或者 <context:component-scan base-package="com.controller,com.service,com.dao"/> context:annotation-config/ 配置的时候一次全写,所有的包都扫描过去,和分开配置有什么区别?
菜鸟有问题,劳烦大神解答,为什么要把controller和service等其他注解分开注入?比如 <context:component-scan base-package="com"/> context:annotation-config/ 注入的时候直接写一次这样,所有的包都扫描过去,两者有什么区别?
昨天,还在纠结这个include怎么不管用呢,原来是这个问题, :idea:
是不是因为DispatcherServler又是继承Servlet的,所以会排除扫描此servlet,故此对象没有被spring注入。
麻烦,开套胸下 :oops: :oops: 项目部分代码:
出现问题:fileUploadAction 并没有被实例化。。 我的理解:exclude-filter这里排除扫描controller,然后我把这exclude-filter删除, fileUploadAction 就可以被实例化,这里FileUploadServlet 这个类是继承servlet,那servlet应该不属于controller吧,虽然有同样的意义:"跳转".. 但是奇怪,排除扫描controller,会影响到servlet?
默认扫描指定包下的全部 @Component, exclude-filter 指定的不扫描,include-filter指定的扫描, include-filter和exclude-filter 没有指定的仍然扫描。对;
指定了use-default-filters=“false” 是这样的 首先通过exclude-filter 进行黑名单过滤;
然后通过include-filter 进行白名单过滤;
否则默认排除
首先通过exclude-filter 进行黑名单过滤;
然后通过include-filter 进行白名单过滤;
否则默认排除
上面的描述, 从字面上不好理解。 我的理解是: 默认扫描指定包下的全部 @Component, exclude-filter 指定的不扫描,include-filter指定的扫描, include-filter和exclude-filter 没有指定的仍然扫描。
不知道我的理解对不,请指正。
好吧,我看到的三段配置代码几乎没有区别。
请参考 http://jinnianshilongnian.iteye.com/blog/1423971
我现在在搭一个开发框架,使用的是struts2.3.15+hibernate4.2.3+spring3.2.3,用spring单元测试,各种数据添加删除都没问题,可是到了web应用中,就出现: org.hibernate.HibernateException: No Session found for current thread at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97) at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988) at com.googlecode.genericdao.dao.hibernate.HibernateBaseDAO.getSession(HibernateBaseDAO.java:68) at com.googlecode.genericdao.dao.hibernate.HibernateBaseDAO._get(HibernateBaseDAO.java:332) at com.googlecode.genericdao.dao.hibernate.GenericDAOImpl.find(GenericDAOImpl.java:53) at org.ware4u.sbp.service.impl.CategoryServiceImpl.findById(CategoryServiceImpl.java:78) 这个错误,实在不解。
比如使用springmvc时可能有两个容器:
1、父容器 ContextLoaderListener加载的 假设此时aop代理了
2、springmvc容器加载了,假设此时没有aop代理,那么 很明显 如果controller查找依赖时 查找的是2 而不是1,,这时候如果需要事务支持就失败了
不好意思,我还是不大明白,系统启动的时候spring就负责各类对象的创建和依赖注入,你说的“已经执行代理的对象”是什么地方发生的呢?tao兄能否说的再清楚一些呢
可能覆盖掉已经执行代理的对象,这样新扫描进来的可能不代理