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

問題
如下方式可以成功掃描到@Controller註解的Bean,不會掃描@Service/@Repository的Bean。正確

Java代碼 收藏代碼
<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,可能造成一些問題

Java代碼 收藏代碼
<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處理;

Java代碼 收藏代碼
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來調用如下代碼:

Java代碼 收藏代碼
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類是否是合法的:

Java代碼 收藏代碼
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 進行白名單過濾;
否則默認排除。

結論
Java代碼 收藏代碼
<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使用上的問題。總結一下方便再遇到類似問題的朋友參考。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章