include-filter和exclude-filter的區別
include-filter和exclude-filter的區別:
前者是掃描,後者是排除掃描。 下面是父子容器配置時需要註意的。
如下方式可以成功掃描到@Controller註解的Bean,不會掃描@Service/@Repository的Bean。正確
Java代碼
- package="org.bdp.system.test.controller">
- "annotation" expression="org.springframework.stereotype.Controller"/>
-
</context:component-scan>
但是如下方式,不僅僅掃描@Controller,還掃描@Service/@Repository的Bean,可能造成一些問題
Java代碼
- package="org.bdp">
- "annotation" expression="org.springframework.stereotype.Controller"/>
- </context:component-scan>
這個尤其在springmvc+spring+hibernate等集成時最容易出問題的地,最典型的錯誤就是:
事務不起作用
這是什麽問題呢?
分析
1、<context:component-scan>會交給org.springframework.context.config.ContextNamespaceHandler處理;
Java代碼
- 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));
- class.getClassLoader();
- try {
- this.includeFilters.add(new AnnotationTypeFilter(
- extends Annotation>) cl.loadClass("javax.annotation.ManagedBean")), false));
- "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(
- extends Annotation>) cl.loadClass("javax.inject.Named")), false));
- "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)) {
- 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代碼- package="org.bdp">
- "annotation" expression="org.springframework.stereotype.Controller"/>
- </context:component-scan>
為什麽這段代碼不僅僅掃描@Controller註解的Bean,而且還掃描了@Component的子註解@Service、@Reposity。因為use-default-filters默認為true。所以如果不需要默認的,則use-default-filters=“false”禁用掉。
include-filter和exclude-filter的區別