Spring MVC註解方式service和controller的掃描順序
如下方式可以成功掃描到@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.
- }
//@Service和@Controller都是Component,因為這些註解都添加了@Component註解
可以看到預設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;
- }
指定了use-default-filters=“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”禁用掉。