上一篇AnnotatedBeanDefinitionReader中提到了ClassPathBeanDefinitionScanner,这两个都是AnnotationConfigApplicationContext中的属性,现在就看看这个ClassPathBeanDefinitionScanner在Spring上下文中干了啥
public AnnotationConfigApplicationContext() {
this.reader = new AnnotatedBeanDefinitionReader(this);
this.scanner = new ClassPathBeanDefinitionScanner(this);
}
点进它的构造方法
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry) {
this(registry, true);
}
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters) {
this(registry, useDefaultFilters, getOrCreateEnvironment(registry));
}
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
Environment environment) {
this(registry, useDefaultFilters, environment,
(registry instanceof ResourceLoader ? (ResourceLoader) registry : null));
}
public class AnnotationConfigApplicationContext extends GenericApplicationContext implements AnnotationConfigRegistry {
public class GenericApplicationContext extends AbstractApplicationContext implements BeanDefinitionRegistry {
public abstract class AbstractApplicationContext extends DefaultResourceLoader
implements ConfigurableApplicationContext {
public class DefaultResourceLoader implements ResourceLoader {
为啥放这么些代码上来,相信大家也能懂,也就是ClassPathBeanDefinitionScanner构造器传的参数,它并没有像有些构造方法参数里面直接传null或false。
仔细看看这些个参数,registry也就是指的是AnnotationConfigApplicationContext,useDefaultFilters设的是true,这个environment先不管,我也不懂,再对着看这个最后一个参数,看上面类的继承关系,发现这个registry是 instanceof ResourceLoader的,所以传进一个registry 是AnnotationConfigApplicationContext
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
Environment environment, @Nullable ResourceLoader resourceLoader) {
Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
this.registry = registry;
//为true的话
if (useDefaultFilters) {
//注册默认的扫描器的过滤规则
//所谓的过滤规则是由两种过滤器来实现的
//是一种是include,另一种是exclude
//当前 只对include进行注册
//并且注册了三种include的规则
registerDefaultFilters();
}
setEnvironment(environment);
setResourceLoader(resourceLoader);
}
然后再看这个构造方法到底干了啥呢?基本上都给这个对象赋值,看这个if,通过参数可以看到这个if里面默认是true,所以可以进到里面,调用这个方法,而这个方法是干嘛的,也可以看注释
protected void registerDefaultFilters() {
//类上面是否加了component注解
//这里直接用的是Component.class
this.includeFilters.add(new AnnotationTypeFilter(Component.class));
ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
//下面两个都可能是第三方的jar包的调用,使用这种方式是为了不把代码放到spring里面
//
try {
//为什么不直接写成ManagedBean.class呢?仅仅为了编译通过
this.includeFilters.add(new AnnotationTypeFilter(
((Class<? extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false));
logger.trace("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.trace("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
}
catch (ClassNotFoundException ex) {
// JSR-330 API not available - simply skip.
}
}
就是给这个扫描器里加入过滤规则,可以看到这里加的过滤规则也就是includefilter里加的就是扫描带有Component注解的类,也是咱们最常见的,加了@Component就能被扫描到spring容器里了,而后面两个不用看哈,也是通过第三方jar包的一些东西,先不管吧。
然后set
setEnvironment(environment);
setResourceLoader(resourceLoader);
给这个扫描器进行初始化,设置一些属性,让它能够完成扫描。
而在这个类里面,最重要的就是这个方法
public int scan(String... basePackages) {
//获取现在已经存在的bd
int beanCountAtScanStart = this.registry.getBeanDefinitionCount();
doScan(basePackages);
// Register annotation config processors, if necessary.
if (this.includeAnnotationConfig) {
AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
}
return (this.registry.getBeanDefinitionCount() - beanCountAtScanStart);
}
通过它来进行扫描,传入的参数就是包的类名
可以看到会先得到一个int,也就是已经存在的bd,这个在AnnotatedBeanDefinitionReader中将讲过,也就是spring内置的几个bd
然后通过doScan方法正式进行扫描
返回的是通过扫描得到的类数
protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
Assert.notEmpty(basePackages, "At least one base package must be specified");
Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>();
for (String basePackage : basePackages) {
//完成了扫描 .class---beandefinition
Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
for (BeanDefinition candidate : candidates) {
ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
candidate.setScope(scopeMetadata.getScopeName());
String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);
if (candidate instanceof AbstractBeanDefinition) {
postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
}
if (candidate instanceof AnnotatedBeanDefinition) {
AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
}
if (checkCandidate(beanName, candidate)) {
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
definitionHolder =
AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
beanDefinitions.add(definitionHolder);
registerBeanDefinition(definitionHolder, this.registry);
}
}
}
return beanDefinitions;
}
也就是通过findCandidateComponents将一些.class变成bd,然后放进这个set里,这个后面再讲。