Fork me on GitHub

【spring源码系列】之【BeanDefinition】

1. BeanDefinition简介

前面讲的解析bean标签,本质就是将bean的信息封装成BeanDefinition对象的过程,最后放入容器beanDefinitionMap中。spring 要根据 BeanDefinition对象来实例化bean,只要把解析的标签,扫描的注解类封装成BeanDefinition对象,spring才能实例化bean。

BeanDefinition有三个实现类,ChildBeanDefinitionGenericBeanDefinitionRootBeanDefinition,三者都继承 AbstractBeanDefinition,对三个子类共同的类信息进行抽象。如果配置文件中定义了父 和 子 ,则父 用 RootBeanDefinition表示,子 用 ChildBeanDefinition 表示,而没有父 的就使用RootBeanDefinition 表示。GenericBeanDefinition 为一站式服务类。

2. BeanDefinition的属性

上一篇文章中并未对BeanDefinition属性作详细分析,本文再次回到上文提到的BeanDefintionParserDelegate的方法parseBeanDefinitionAttributes方法。

public AbstractBeanDefinition parseBeanDefinitionAttributes(Element ele, String beanName,
			@Nullable BeanDefinition containingBean, AbstractBeanDefinition bd) {
		// 解析scope标签
		if (ele.hasAttribute(SINGLETON_ATTRIBUTE)) {
			error("Old 1.x 'singleton' attribute in use - upgrade to 'scope' declaration", ele);
		}
		else if (ele.hasAttribute(SCOPE_ATTRIBUTE)) {
			bd.setScope(ele.getAttribute(SCOPE_ATTRIBUTE));
		}
		else if (containingBean != null) {
			// Take default from containing bean in case of an inner bean definition.
			bd.setScope(containingBean.getScope());
		}

		// 解析abstract标签
		if (ele.hasAttribute(ABSTRACT_ATTRIBUTE)) {
			bd.setAbstract(TRUE_VALUE.equals(ele.getAttribute(ABSTRACT_ATTRIBUTE)));
		}

		// 解析lazy-init标签
		String lazyInit = ele.getAttribute(LAZY_INIT_ATTRIBUTE);
		if (isDefaultValue(lazyInit)) {
			lazyInit = this.defaults.getLazyInit();
		}
		bd.setLazyInit(TRUE_VALUE.equals(lazyInit));

		// 解析 autowire 标签
		String autowire = ele.getAttribute(AUTOWIRE_ATTRIBUTE);
		bd.setAutowireMode(getAutowireMode(autowire));

		// 解析 depends-on 标签
		if (ele.hasAttribute(DEPENDS_ON_ATTRIBUTE)) {
			String dependsOn = ele.getAttribute(DEPENDS_ON_ATTRIBUTE);
			bd.setDependsOn(StringUtils.tokenizeToStringArray(dependsOn, MULTI_VALUE_ATTRIBUTE_DELIMITERS));
		}

		// 解析 autowire-candidate 标签
		String autowireCandidate = ele.getAttribute(AUTOWIRE_CANDIDATE_ATTRIBUTE);
		if (isDefaultValue(autowireCandidate)) {
			String candidatePattern = this.defaults.getAutowireCandidates();
			if (candidatePattern != null) {
				String[] patterns = StringUtils.commaDelimitedListToStringArray(candidatePattern);
				bd.setAutowireCandidate(PatternMatchUtils.simpleMatch(patterns, beanName));
			}
		}
		else {
			bd.setAutowireCandidate(TRUE_VALUE.equals(autowireCandidate));
		}

		// 解析 primary 标签
		if (ele.hasAttribute(PRIMARY_ATTRIBUTE)) {
			bd.setPrimary(TRUE_VALUE.equals(ele.getAttribute(PRIMARY_ATTRIBUTE)));
		}

		// 解析 init-method 标签
		if (ele.hasAttribute(INIT_METHOD_ATTRIBUTE)) {
			String initMethodName = ele.getAttribute(INIT_METHOD_ATTRIBUTE);
			bd.setInitMethodName(initMethodName);
		}
		else if (this.defaults.getInitMethod() != null) {
			bd.setInitMethodName(this.defaults.getInitMethod());
			bd.setEnforceInitMethod(false);
		}

		// 解析 destroy-method 标签
		if (ele.hasAttribute(DESTROY_METHOD_ATTRIBUTE)) {
			String destroyMethodName = ele.getAttribute(DESTROY_METHOD_ATTRIBUTE);
			bd.setDestroyMethodName(destroyMethodName);
		}
		else if (this.defaults.getDestroyMethod() != null) {
			bd.setDestroyMethodName(this.defaults.getDestroyMethod());
			bd.setEnforceDestroyMethod(false);
		}

		// 解析 factory-method 标签
		if (ele.hasAttribute(FACTORY_METHOD_ATTRIBUTE)) {
			bd.setFactoryMethodName(ele.getAttribute(FACTORY_METHOD_ATTRIBUTE));
		}

		// 解析 factory-bean 标签
		if (ele.hasAttribute(FACTORY_BEAN_ATTRIBUTE)) {
			bd.setFactoryBeanName(ele.getAttribute(FACTORY_BEAN_ATTRIBUTE));
		}

		return bd;
	}

由于BeanDefinition的实现类都继承自父类AbstractBeanDefinition,父类中有三个引用的属性ConstructorArgumentValuesMutablePropertyValuesMethodOverrides,所以GenericBeanDefinition最终包含的属性如下图:

  • id:Bean 的唯一标识名。它必须是合法的 XMLID,在整个 XML 文档中唯一;
  • name:用来为 id 创建一个或多个别名。它可以是任意的字母符合。多个别名之间用逗号或空格分开;
  • class:用来定义类的全限定名(包名+类名)。只有子类 Bean 不用定义该属性;
  • parent:子类 Bean 定义它所引用它的父类 Bean,这时前面的 class 属性失效,子类 Bean 会继承父类 Bean 的所有属性,子类 Bean 也可以覆盖父类 Bean 的属性,注意:子类 Bean 和父类 Bean 是同一个 Java 类;
  • abstract(默认为"false"):用来定义 Bean 是否为抽象 Bean。它表示这个 Bean 将不会被实例化,一般用于父类 Bean,因为父类 Bean 主要是供子类 Bean 继承使用;
  • lazy-init(默认为"false"):用来定义这个 Bean 是否实现懒初始化。如果为"false",它将在 BeanFactory 启动时初始化所有的 SingletonBean。反之,如果为"true",它只在 Bean 请求时才开始创建 SingletonBean;
  • autowire(自动装配,默认为"default"):它定义了 Bean 的自动装载方式;
    --"no":不使用自动装配功能;
    --"byName":通过 Bean 的属性名实现自动装配;
    --"byType":通过 Bean 的类型实现自动装配;
    --"constructor":类似于 byType,但它是用于构造函数的参数的自动组装;
    --"autodetect":通过 Bean 类的反省机制(introspection)决定是使用"constructor"还是使用"byType"。
  • depends-on(依赖对象):这个 Bean 在初始化时依赖的对象,这个对象会在这个 Bean 初始化之前创建;
  • init-method:用来定义 Bean 的初始化方法,它会在 Bean 组装之后调用。它必须是一个无参数的方法;
  • destroy-method:用来定义 Bean 的销毁方法,它在 BeanFactory 关闭时调用。同样,它也必
    须是一个无参数的方法。它只能应用于 singletonBean。
  • factory-method:定义创建该 Bean 对象的工厂方法。它用于下面的"factory-bean",表示这个 Bean 是通过工厂方法创建,此时,"class"属性失效。
  • factory-bean:定义创建该 Bean 对象的工厂类。如果使用了"factory-bean"则"class"属性失效。
  • autowire-candidate:采用 xml 格式配置 bean 时,将元素的 autowire-candidate属性设置为 false,这样容器在查找自动装配对象时,将不考虑该 bean,即它不会被考虑作为其它 bean自动装配的候选者,但是该 bean 本身还是可以使用自动装配来注入其它 bean 的;
  • MutablePropertyValues:用于封装标签的信息,其实类里面就是有一个 list,list里面是 PropertyValue 对象,PropertyValue 就是一个 name 和 value 属性,用于封装标签的名称和值信息
  • ConstructorArgumentValues:用于封装标签的信息,其实类里面就是有一个 map,map 中用构造函数的参数顺序作为 key,值作为 value 存储到 map 中;
  • MethodOverrides:用于封装 lookup-method 和 replaced-method 标签的信息,同样的类里面有一个 Set 对象添加 LookupOverride 对象和ReplaceOverride 对象。

3. component-scan标签解析过程

3.1 流程概览

3.2 详细过程

前面一文提到,自定义标签解析BeanDefinitionParserDelegate类,执行parseCustomElement方法;

public BeanDefinition parseCustomElement(Element ele, @Nullable BeanDefinition containingBd) {
                // 获取namespaceURI
		String namespaceUri = getNamespaceURI(ele);
		if (namespaceUri == null) {
			return null;
		}
                // 解析namespaceURI对应的handler类
		NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
		if (handler == null) {
			error("Unable to locate Spring NamespaceHandler for XML schema namespace [" + namespaceUri + "]", ele);
			return null;
		}
                // 执行handler的解析方法
		return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));
	}

上述过程主要完成以下步骤:
step1: 获取namespaceURI;
step2: 解析namespaceURI对应的handler类;
step3:执行handler方法解析。
step1与step2前文已分析,以component-scan为例,分析step3,代码进入ComponentScanBeanDefinitionParserparse方法

	public BeanDefinition parse(Element element, ParserContext parserContext) {
		/**
		 * 1. 包扫描.class后缀的文件
		 * 2. 判断类上是否有注解
		 * 3. GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();
		 *         genericBeanDefinition.setBeanClass(BeanClass.class);
		 * 4. 完成beanDefinition的注册
		 */
		String basePackage = element.getAttribute(BASE_PACKAGE_ATTRIBUTE);
		basePackage = parserContext.getReaderContext().getEnvironment().resolvePlaceholders(basePackage);
		String[] basePackages = StringUtils.tokenizeToStringArray(basePackage,
				ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);

		// Actually scan for bean definitions and register them.
		// 创建扫描器
		ClassPathBeanDefinitionScanner scanner = configureScanner(parserContext, element);
		// 扫描器扫描
		Set<BeanDefinitionHolder> beanDefinitions = scanner.doScan(basePackages);
		// 注册bean包含的组件
		registerComponents(parserContext.getReaderContext(), beanDefinitions, element);

		return null;
	}

上述过程总共分为三步:
step1:configureScanner方法创建扫描器;
step2:doScan方法扫描器扫描;
step3:registerComponents注册bean包含的组件。

进入上述step2,进入ClassPathBeanDefinitionScannerdoScan方法,

	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) {
			// 扫描有注解的类并封装成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) {
					// 支持@Lazy @Primary @DependOn注解
					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;
	}

上述doScan方法主要做了以下三步:
step1: findCandidateComponents扫描有注解的类并封装成beanDefinition对象;
step2: processCommonDefinitionAnnotations方法支持@Lazy @Primary @DependOn注解;
step3:注册BeanDefinition。

继续进入上述step1中的findCandidateComponents方法,来到ClassPathScanningCandidateComponentProvider类的scanCandidateComponents方法,完成以下步骤:
step1: getResources递归获取.class后缀的文件;
step2: getMetadataReader方法,获取元数据AnnotationMetadataReadingVisitor对象,该元数据收集了扫描类的任何信息;
step3:判断includeFilters是否跟元数据中的注解匹配,如果匹配就实例化该类,创建BeanDefinition对象。

前面还有一个步骤step3:registerComponents注册bean包含的组件还未分析,进入该方法

protected void registerComponents(
			XmlReaderContext readerContext, Set<BeanDefinitionHolder> beanDefinitions, Element element) {

		Object source = readerContext.extractSource(element);
		CompositeComponentDefinition compositeDef = new CompositeComponentDefinition(element.getTagName(), source);

		for (BeanDefinitionHolder beanDefHolder : beanDefinitions) {
			compositeDef.addNestedComponent(new BeanComponentDefinition(beanDefHolder));
		}


		boolean annotationConfig = true;
		if (element.hasAttribute(ANNOTATION_CONFIG_ATTRIBUTE)) {
			annotationConfig = Boolean.parseBoolean(element.getAttribute(ANNOTATION_CONFIG_ATTRIBUTE));
		}
		if (annotationConfig) {
			// 如果类中的属性有注解,注册注解配置处理器
			Set<BeanDefinitionHolder> processorDefinitions =
					AnnotationConfigUtils.registerAnnotationConfigProcessors(readerContext.getRegistry(), source);
			for (BeanDefinitionHolder processorDefinition : processorDefinitions) {
				compositeDef.addNestedComponent(new BeanComponentDefinition(processorDefinition));
			}
		}

		readerContext.fireComponentRegistered(compositeDef);
	}

随后进入AnnotationConfigUtils.registerAnnotationConfigProcessors

public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
			BeanDefinitionRegistry registry, @Nullable Object source) {

		DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
		if (beanFactory != null) {
			if (!(beanFactory.getDependencyComparator() instanceof AnnotationAwareOrderComparator)) {
				beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
			}
			if (!(beanFactory.getAutowireCandidateResolver() instanceof ContextAnnotationAutowireCandidateResolver)) {
				beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
			}
		}

		Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<>(8);

		// @Configuration注解的处理器ConfigurationClassPostProcessor
		if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));
		}

		// @Autowired注解的处理器AutowiredAnnotationBeanPostProcessor
		if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
		}

		// CommonAnnotationBeanPostProcessor处理器.
		if (jsr250Present && !registry.containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
		}

		// Check for JPA support, and if present add the PersistenceAnnotationBeanPostProcessor.
		if (jpaPresent && !registry.containsBeanDefinition(PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition();
			try {
				def.setBeanClass(ClassUtils.forName(PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME,
						AnnotationConfigUtils.class.getClassLoader()));
			}
			catch (ClassNotFoundException ex) {
				throw new IllegalStateException(
						"Cannot load optional framework class: " + PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME, ex);
			}
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME));
		}

		if (!registry.containsBeanDefinition(EVENT_LISTENER_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(EventListenerMethodProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME));
		}

		if (!registry.containsBeanDefinition(EVENT_LISTENER_FACTORY_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(DefaultEventListenerFactory.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_FACTORY_BEAN_NAME));
		}

		return beanDefs;
	}

上面提到了三类处理器ConfigurationClassPostProcessorAutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessor,分别对不同注解作处理,最后封装到BeanDefinition中,注册到容器。

进入ConfigurationClassPostProcessor的processConfigBeanDefinitions方法,如下:

public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {
		......
		// 解析所有加了@Configuration注解的类
		ConfigurationClassParser parser = new ConfigurationClassParser(
				this.metadataReaderFactory, this.problemReporter, this.environment,
				this.resourceLoader, this.componentScanBeanNameGenerator, registry);

		Set<BeanDefinitionHolder> candidates = new LinkedHashSet<>(configCandidates);
		Set<ConfigurationClass> alreadyParsed = new HashSet<>(configCandidates.size());
		do {
			// 解析@Component @ComponentScan @ComponentScans @Bean @Import @ImportResource
			parser.parse(candidates);
			parser.validate();

			......
	}

上述方法主要解析加了@Configuration的类,以及@Component @ComponentScan @ComponentScans @Bean @Import @ImportResource注解,后者是通过parse方法完成的,进入parse方法一路走下来回到processConfigurationClass方法,如下图

protected void processConfigurationClass(ConfigurationClass configClass, Predicate<String> filter) throws IOException {
		......
		do {
			sourceClass = doProcessConfigurationClass(configClass, sourceClass, filter);
		}
		while (sourceClass != null);
                ......
	}

随后进入doProcessConfigurationClass方法,完成@Component @ComponentScan @ComponentScans @Bean @Import @ImportResource注解解析。

protected final SourceClass doProcessConfigurationClass(
			ConfigurationClass configClass, SourceClass sourceClass, Predicate<String> filter)
			throws IOException {

		// 解析 @Component
		if (configClass.getMetadata().isAnnotated(Component.class.getName())) {
			// Recursively process any member (nested) classes first
			processMemberClasses(configClass, sourceClass, filter);
		}

		// 解析 @PropertySource
		for (AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable(
				sourceClass.getMetadata(), PropertySources.class,
				org.springframework.context.annotation.PropertySource.class)) {
			if (this.environment instanceof ConfigurableEnvironment) {
				processPropertySource(propertySource);
			}
			else {
				logger.info("Ignoring @PropertySource annotation on [" + sourceClass.getMetadata().getClassName() +
						"]. Reason: Environment must implement ConfigurableEnvironment");
			}
		}

		// 解析 @ComponentScan @ComponentScans
		Set<AnnotationAttributes> componentScans = AnnotationConfigUtils.attributesForRepeatable(
				sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class);
		if (!componentScans.isEmpty() &&
				!this.conditionEvaluator.shouldSkip(sourceClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) {
			for (AnnotationAttributes componentScan : componentScans) {
				// The config class is annotated with @ComponentScan -> perform the scan immediately
				Set<BeanDefinitionHolder> scannedBeanDefinitions =
						this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());
				// Check the set of scanned definitions for any further config classes and parse recursively if needed
				for (BeanDefinitionHolder holder : scannedBeanDefinitions) {
					BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition();
					if (bdCand == null) {
						bdCand = holder.getBeanDefinition();
					}
					if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) {
						parse(bdCand.getBeanClassName(), holder.getBeanName());
					}
				}
			}
		}

		// 解析 @Import
		processImports(configClass, sourceClass, getImports(sourceClass), filter, true);

		// 解析 @ImportResource
		AnnotationAttributes importResource =
				AnnotationConfigUtils.attributesFor(sourceClass.getMetadata(), ImportResource.class);
		if (importResource != null) {
			String[] resources = importResource.getStringArray("locations");
			Class<? extends BeanDefinitionReader> readerClass = importResource.getClass("reader");
			for (String resource : resources) {
				String resolvedResource = this.environment.resolveRequiredPlaceholders(resource);
				configClass.addImportedResource(resolvedResource, readerClass);
			}
		}

		// 解析 @Bean 方法
		Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(sourceClass);
		for (MethodMetadata methodMetadata : beanMethods) {
			configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));
		}

		// Process default methods on interfaces
		processInterfaces(configClass, sourceClass);

		// Process superclass, if any
		if (sourceClass.getMetadata().hasSuperClass()) {
			String superclass = sourceClass.getMetadata().getSuperClassName();
			if (superclass != null && !superclass.startsWith("java") &&
					!this.knownSuperclasses.containsKey(superclass)) {
				this.knownSuperclasses.put(superclass, configClass);
				// Superclass found, return its annotation metadata and recurse
				return sourceClass.getSuperClass();
			}
		}

		// No superclass -> processing is complete
		return null;
	}

同样跟踪AutowiredAnnotationBeanPostProcessor类,可以看到该类完成@Autowired @Value的解析,如下图:

public AutowiredAnnotationBeanPostProcessor() {
		this.autowiredAnnotationTypes.add(Autowired.class);
		this.autowiredAnnotationTypes.add(Value.class);
		try {
			this.autowiredAnnotationTypes.add((Class<? extends Annotation>)
					ClassUtils.forName("javax.inject.Inject", AutowiredAnnotationBeanPostProcessor.class.getClassLoader()));
			logger.trace("JSR-330 'javax.inject.Inject' annotation found and supported for autowiring");
		}
		catch (ClassNotFoundException ex) {
			// JSR-330 API not available - simply skip.
		}
	}

类似跟踪CommonAnnotationBeanPostProcessor类,可以看到该类完成@Resource @PostConstruct @PreDestroy的解析,如下图:

static {
		webServiceRefClass = loadAnnotationType("javax.xml.ws.WebServiceRef");
		ejbClass = loadAnnotationType("javax.ejb.EJB");

		resourceAnnotationTypes.add(Resource.class);
		if (webServiceRefClass != null) {
			resourceAnnotationTypes.add(webServiceRefClass);
		}
		if (ejbClass != null) {
			resourceAnnotationTypes.add(ejbClass);
		}
	}
......
 
public CommonAnnotationBeanPostProcessor() {
		setOrder(Ordered.LOWEST_PRECEDENCE - 3);
		setInitAnnotationType(PostConstruct.class);
		setDestroyAnnotationType(PreDestroy.class);
		ignoreResourceType("javax.xml.ws.WebServiceContext");
	}

4. 示例

创建一个BeanDefinitionTest类,实现BeanDefinitionRegistryPostProcessor接口,并在方法中完成设置Bean的类型为BeanClass,然后设置BeanClass对象的username属性与值,最后注册到容器中,代码如下

@Component
    public class BeanDefinitionTest implements BeanDefinitionRegistryPostProcessor {

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();
        genericBeanDefinition.setBeanClass(BeanClass.class);

        MutablePropertyValues propertyValues = genericBeanDefinition.getPropertyValues();
        propertyValues.addPropertyValue("username","wzj");

        registry.registerBeanDefinition("beanClass",genericBeanDefinition);
    }

BeanClass类如下:

@Data
public class BeanClass {

    private String username;
}

测试类如下:

public class TestSpring {

    @Autowired
    private ApplicationContext applicationContext;

    @Test
    public void testComponentScan() {
        applicationContext = new AnnotationConfigApplicationContext("com.wzj");
        BeanClass beanClass = (BeanClass)applicationContext.getBean("beanClass");
        BeanDefinitionTest beanDefinitionTest = (BeanDefinitionTest)applicationContext.getBean("beanDefinitionTest");
        System.out.println("BeanClass-->" + beanClass.getUsername());
        System.out.println("BeanDefinitionTest-->" + beanDefinitionTest.getClass());
    }

代码目录结构如下与运行结果如下

5. 总结

本文以conmponent-scan标签为例,分析了主要流程,并结合源码讲述了BeanDefinition属性的解析、封装、以及最后注册到容器中,最后以一个思维导图总结每个流程中的大致步骤

另外,静态看源码可关注主流程,并做注释,动态debug示例进入源码可直观感受运行期间的值,源码分析不易,搞清楚主流程与思想比源码本身更重要。

posted @ 2021-05-08 07:12  小猪爸爸  阅读(661)  评论(0编辑  收藏  举报