參考了多篇文章都說明了use-default-filters參數的基本用途,但有些主要點沒有說到,這裏補充記錄下:java
<context:component-scan base-package="com.jaamy" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan>
這個只掃描com.jaamy包下的@Controller,不會掃描@Service、@Repositoryspring
<context:component-scan base-package="com.jaamy"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan>
這個不但掃描com.jaamy包下的@Controller,同時也會掃描@Service、@Repository,注意這裏沒有添加use-default-filters參數express
下面配合源碼說明下use-default-filters參數的做用以及和context:include-filter、exclude-filter的關係。ide
代碼中是根據use-default-filters的值來肯定是否須要調用registerDefaultFilters來添加默認的filters到includeFilters中,看下面的代碼:this
org.springframework.context.annotation.ClassPathScanningCandidateComponentProviderspa
public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters, Environment environment) { if (useDefaultFilters) { registerDefaultFilters(); } Assert.notNull(environment, "Environment must not be null"); this.environment = environment; }
use-default-filters爲true時調用了下面的代碼,在includeFilters列表中添加了Component、ManagedBean和Named,所以use-default-filters的值直接影響includeFilters的內容,而includeFilters的容直接影響了要掃描的內容,所以use-default-filters的值是否配置也就決定了總體要掃描的內容。debug
protected void registerDefaultFilters() { this.includeFilters.add(new AnnotationTypeFilter(Component.class)); ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader(); try { this.includeFilters.add(new AnnotationTypeFilter( ((Class<? extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false)); logger.debug("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>) ClassUtils.forName("javax.inject.Named", cl)), false)); logger.debug("JSR-330 'javax.inject.Named' annotation found and supported for component scanning"); } catch (ClassNotFoundException ex) { // JSR-330 API not available - simply skip. } }
關於include-filter、exclude-filter的做用主要是用來過濾掃描到的bean是否合法:code
首先經過exclude-filter進行黑名單過濾;component
而後經過include-filter進行白名單過濾;blog
不然默認排除。 看下面的代碼:
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)) { return isConditionMatch(metadataReader); } } return false; }
總結:
<context:component-scan base-package="com.jaamy" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
針對上面的配置,use-default-filters的做用以下:
use-default-filters不配置或者是配置爲true時:
不但要掃描include配置的com.jaamy包下的@Controller,並且還要掃描@Service和@Repository
use-default-filters配置爲false時:
只掃描include配置的com.jaamy包下的@Controller,不掃描@Service和@Repository
參考地址:http://jinnianshilongnian.iteye.com/blog/1762632