spring加载流程之ClassPathBeanDefinitionScanner

spring加载流程之ClassPathBeanDefinitionScanner

    • ClassPathBeanDefinitionScanner
      • ClassPathBeanDefinitionScanner的构造函数
    • 父类ClassPathScanningCandidateComponentProvider
      • 扫描过滤器includeFilters与excludeFilters
    • ClassPathBeanDefinitionScanner的scan()方法

缘起:继上篇 spring加载流程之AnnotatedBeanDefinitionReader之后,按照spring加载流程本篇继续分析ClassPathBeanDefinitionScanner扫描器

ClassPathBeanDefinitionScanner

public AnnotationConfigApplicationContext() {
		/**
		 * 创建一个读取注解的Bean定义读取器
		 * 什么是bean定义?BeanDefinition
		 *
		 * 完成了spring内部BeanDefinition的注册(主要是后置处理器)
		 */
		this.reader = new AnnotatedBeanDefinitionReader(this);

		/**
		 * 创建BeanDefinition扫描器
		 * 可以用来扫描包或者类,继而转换为bd
		 *
		 * spring默认的扫描包不是这个scanner对象
		 * 而是自己new的一个ClassPathBeanDefinitionScanner
		 * spring在执行工程后置处理器ConfigurationClassPostProcessor时,去扫描包时会new一个ClassPathBeanDefinitionScanner
		 *
		 * 这里的scanner仅仅是为了程序员可以手动调用AnnotationConfigApplicationContext对象的scan方法
		 *
		 */
		this.scanner = new ClassPathBeanDefinitionScanner(this);
	}

ClassPathBeanDefinitionScanner的构造函数

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 ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
			Environment environment, @Nullable ResourceLoader resourceLoader) {

		Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
		this.registry = registry;

		if (useDefaultFilters) {
			/**
			 * 注册spring扫描类过滤器
			 * 加了特定注解的类会被扫描到
			 * 带有@Component、@Repository、@Service、@Controller、@ManagedBean、@Named
			 */
			registerDefaultFilters();
		}
		setEnvironment(environment);
		setResourceLoader(resourceLoader);
	}

只关心最后一个构造函数的registerDefaultFilters();方法

父类ClassPathScanningCandidateComponentProvider

spring加载流程之ClassPathBeanDefinitionScanner_第1张图片
registerDefaultFilters();是父类ClassPathScanningCandidateComponentProvider的方法

/**
	 * 注册过滤器
	 * 带有@Component、@Repository、@Service、@Controller、@ManagedBean、@Named
	 * 注解的类会被spring扫描到
	 *
	 * Register the default filter for {@link Component @Component}.
	 * 

This will implicitly register all annotations that have the * {@link Component @Component} meta-annotation including the * {@link Repository @Repository}, * {@link Service @Service}, and * {@link Controller @Controller} stereotype annotations. *

Also supports Java EE 6's {@link javax.annotation.ManagedBean} and * JSR-330's {@link javax.inject.Named} annotations, if available. * */ protected void registerDefaultFilters() { this.includeFilters.add(new AnnotationTypeFilter(Component.class)); ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader(); try { this.includeFilters.add(new AnnotationTypeFilter( ((Class) 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) 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. } }

扫描过滤器includeFilters与excludeFilters

首先这里的includeFilters大家熟悉吗,还有个excludeFilters,先看一下属性

private final List includeFilters = new LinkedList<>();
private final List excludeFilters = new LinkedList<>();

这里提前往includeFilters里面添加需要扫描的特定注解
1.添加元注解@Component,需要注意的是@Repository@Service@Controller里面都标注了@Component。很好理解,扫描的时候用includeFilters 去过滤时,会找到并处理这4个注解的类。
2.下面两个注解@ManagedBean@Named需要有对应的jar包,否则(也就是说把这个方法走完),includeFilters里面只会有一个元素
spring加载流程之ClassPathBeanDefinitionScanner_第2张图片

ClassPathBeanDefinitionScanner的scan()方法

其实按照spring的加载流程,ClassPathBeanDefinitionScanner到这里的作用就结束,里面的很多重要方法是在流程加载后面用到的,但是既然都是一个类里面的方法,就在这里先讲一下吧。

AnnotationConfigApplicationContext context =
				new AnnotationConfigApplicationContext();
context.scan("com.yk.demo");

调用的是

public void scan(String... basePackages) {
		Assert.notEmpty(basePackages, "At least one base package must be specified");
		this.scanner.scan(basePackages);
	}

这个scanner就是默认构造函数初始化的ClassPathBeanDefinitionScanner,只有手动调用context.scan("com.yk.demo");这个初始化的scanner才有发挥的作用。

public int scan(String... basePackages) {
		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);
	}

跟进doScan(basePackages);

/**
	 * 扫描指定的包,并注册beanDefinition
	 */
protected Set doScan(String... basePackages) {
		Assert.notEmpty(basePackages, "At least one base package must be specified");
		Set beanDefinitions = new LinkedHashSet<>();
		for (String basePackage : basePackages) {
			/**
			 * 扫描basePackage路径下的java文件
			 *
			 * 先全部转为Resource,然后再判断拿出符合条件的bd
			 */
			Set candidates = findCandidateComponents(basePackage);

			for (BeanDefinition candidate : candidates) {
				/**
				 * 解析scope属性
				 */
				ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
				candidate.setScope(scopeMetadata.getScopeName());
				/**
				 * 获取beanName
				 * 先判断注解上有没有显示设置beanName
				 * 没有的话,就以类名小写为beanName
				 */
				String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);

				if (candidate instanceof AbstractBeanDefinition) {
					/**
					 * 如果这个类是AbstractBeanDefinition类型
					 * 则为他设置默认值,比如lazy/init/destroy
					 *
					 * 通过扫描出来的bd是ScannedGenericBeanDefinition,实现了AbstractBeanDefinition
					 */
					postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
				}
				if (candidate instanceof AnnotatedBeanDefinition) {
					/**
					 * 处理加了注解的类
					 * 把常用注解设置到AnnotationBeanDefinition中
					 */
					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(basePackage);进入到父类ClassPathScanningCandidateComponentProvider

public Set findCandidateComponents(String basePackage) {
		if (this.componentsIndex != null && indexSupportsIncludeFilters()) {
			return addCandidateComponentsFromIndex(this.componentsIndex, basePackage);
		}
		else {
			return scanCandidateComponents(basePackage);
		}
	}

根据方法名顾名思义,找到候选组件,在指定的包中找到候选组件,进入到scanCandidateComponents(basePackage);

private Set scanCandidateComponents(String basePackage) {
		Set candidates = new LinkedHashSet<>();
		try {
			/**
			 * 扫描classpath*:下的.class文件
			 * 将class文件转换为resources
			 *
			 * 循环resource,转换为ScannedGenericBeanDefinition
			 *
			 */
			String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
					resolveBasePackage(basePackage) + '/' + this.resourcePattern;
			/**
			 * asm读取class文件
			 */
			Resource[] resources = getResourcePatternResolver().getResources(packageSearchPath);
			boolean traceEnabled = logger.isTraceEnabled();
			boolean debugEnabled = logger.isDebugEnabled();
			for (Resource resource : resources) {
				if (traceEnabled) {
					logger.trace("Scanning " + resource);
				}
				if (resource.isReadable()) {
					try {
						//获取.class对应的元信息,例如注解信息等
						MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(resource);
						/**
						 * 判断excludeFilters与includeFilters
						 * includeFilters在new ClassPathBeanDefinitionScanner时就默认添加了@Component等元注解
						 * 根据注解元信息判断是不是符合条件的.class
						 */
						if (isCandidateComponent(metadataReader)) {
							ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);
							sbd.setResource(resource);
							sbd.setSource(resource);
							//判断能否实例化
							if (isCandidateComponent(sbd)) {
								if (debugEnabled) {
									logger.debug("Identified candidate component class: " + resource);
								}
								//添加到候选BeanDefinition
								candidates.add(sbd);
							}
							else {
								if (debugEnabled) {
									logger.debug("Ignored because not a concrete top-level class: " + resource);
								}
							}
						}
						else {
							if (traceEnabled) {
								logger.trace("Ignored because not matching any filter: " + resource);
							}
						}
					}
					catch (Throwable ex) {
						throw new BeanDefinitionStoreException(
								"Failed to read candidate component class: " + resource, ex);
					}
				}
				else {
					if (traceEnabled) {
						logger.trace("Ignored because not readable: " + resource);
					}
				}
			}
		}
		catch (IOException ex) {
			throw new BeanDefinitionStoreException("I/O failure during classpath scanning", ex);
		}
		return candidates;
	}

这块有一个asm读取class文件的内容,不是很清楚,后面在深磕。
上面有一个组件判断if (isCandidateComponent(metadataReader)),就是判断当前class文件符不符合扫描过滤器includeFiltersexcludeFilters中的定义,最后返回一个符合条件的Set
再回到之前的 doScan(String... basePackages)

Set candidates = findCandidateComponents(basePackage);

			for (BeanDefinition candidate : candidates) 

开始循环,处理注解,设置beanDefinition属性最后执行 registerBeanDefinition(definitionHolder, this.registry);注册beanDefinition

protected void registerBeanDefinition(BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry) {
		BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, registry);
	}

跟进代码,调用BeanDefinitionReaderUtils的方法

public static void registerBeanDefinition(
			BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
			throws BeanDefinitionStoreException {

		// Register bean definition under primary name.
		String beanName = definitionHolder.getBeanName();

		/**
		 * 这里的registerBeanDefinition是由父类GenericApplicationContext实现的
		 *
		 * 跟踪源码可知,是在父类中调用this.beanFactory.registerBeanDefinition(beanName, beanDefinition)
		 * 而这个beanFactory是AnnotationConfigApplicationContext在执行自己的构造方法this()时
		 * 先去执行了父类GenericApplicationContext的构造方法,完成了this.beanFactory = new DefaultListableBeanFactory()
		 *
		 * 所以,最终将beanDefinition注册到了DefaultListableBeanFactory中
		 *
		 * */
		registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());

		// Register aliases for bean name, if any.
		String[] aliases = definitionHolder.getAliases();
		if (aliases != null) {
			for (String alias : aliases) {
				registry.registerAlias(beanName, alias);
			}
		}
	}

这里的registrythis.scanner = new ClassPathBeanDefinitionScanner(this);中的this也就是AnnotationConfigApplicationContext

ClassPathBeanDefinitionScanner的扫描大致就是这样完成的

你可能感兴趣的:(spring源码)