前言
关于上篇文章中的只是一个简单的流程代码,我这篇文章记录一下自己的学习结果。
过滤器配置
ClassPathBeanDefinitionScanner scanner = configureScanner(parserContext, element);
通过对类ComponentScanBeanDefinitionParser中parse(),我们把扫描到的useDefaultFilters当作参数传递过来,进行配置扫描器configureScanner()。
通过进入configureScanner()内部发现以下代码块:
boolean useDefaultFilters = true;
if (element.hasAttribute(USE_DEFAULT_FILTERS_ATTRIBUTE)) {
useDefaultFilters = Boolean.valueOf(element.getAttribute(USE_DEFAULT_FILTERS_ATTRIBUTE));
}
// Delegate bean definition registration to scanner class.
ClassPathBeanDefinitionScanner scanner = createScanner(readerContext, useDefaultFilters);
默认的过滤器属性是true。我们去xml中的节点中去取use-default-filters的属性,如果有进行加载配置的过滤器,没有的话把userDefaultFilters=TRUE当成参数传入到createScanner()方法中,去创建默认的过滤器。当进入createScanner()中
protected ClassPathBeanDefinitionScanner createScanner(XmlReaderContext readerContext, boolean useDefaultFilters) {
return new ClassPathBeanDefinitionScanner(readerContext.getRegistry(), useDefaultFilters);
}
这时,我们new了一个ClassPathBeanDefinitionScanner,并把过滤器参数传进去,
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters) {
this(registry, useDefaultFilters, getOrCreateEnvironment(registry));
}
接着进行注册。
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters, Environment environment) {
super(useDefaultFilters, environment);
Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
this.registry = registry;
// Determine ResourceLoader to use.
if (this.registry instanceof ResourceLoader) {
setResourceLoader((ResourceLoader) this.registry);
}
}
关注super(useDefaultFilters, environment);该方法进行注册传进来的过滤器
public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters, Environment environment) {
if (useDefaultFilters) {
registerDefaultFilters();
}
Assert.notNull(environment, "Environment must not be null");
this.environme