BeanFactory体系结构探析( 二 )

创建BeanDefinitionpublic RootBeanDefinition(@Nullable Class beanClass) {//初始化父类super();//将beanClass赋值给this.BeanClasssetBeanClass(beanClass);}123456初始化父类
//将其中一部分属性赋予默认值autowireCandidate = true;primary = false;protected AbstractBeanDefinition() {this(null, null);}protected AbstractBeanDefinition(@Nullable ConstructorArgumentValues cargs, @Nullable MutablePropertyValues pvs) {this.constructorArgumentValues = cargs;this.propertyValues = pvs;}1234567891011将BeanDefinition注册到容器中//除去校验逻辑 , 注册时只做了这两步操作this.beanDefinitionMap.put(beanName, beanDefinition);this.beanDefinitionNames.add(beanName);123看到这里 , 大家伙可能会充满疑问?what? 就这样?没有填充属性的步骤吗?嘿嘿 , BeanFactory是一个纯粹的工厂 , 只负责生产Bean , 是没有装配(设计)BeanDefinition的功能的 , 专业的事还是交给专业的人做 , 设计的事情还是交由ApplicationContext完成的 。
那在ApplicationContext中是怎么完成一个BeanDefinition的呢?还记得预启动将配置类注册到容器中时有这样一段代码吗?以下代码为AnnotatedBeanDefinitionReader#doRegisterBean中的摘要部分:
void doRegisterBean(Class annotatedClass, @Nullable Supplier instanceSupplier, @Nullable String name,@Nullable Class[] qualifiers, BeanDefinitionCustomizer... definitionCustomizers) { //.......代码//处理普通的bean定义注解 , @Lazy @Primary @DependsOn @Role @DescriptionAnnotationConfigUtils.processCommonDefinitionAnnotations(abd);//......代码} 12345678而非配置的Bean是通过在预启动时注册的配置类后置处理器ConfigurationClassPostProcessor#processConfigBeanDefinitions中完成的 , 以下代码为ClassPathBeanDefinitionScanner#doScan中的摘要部分 , 详细调用链将在后面的文章进行说明
//传入我们配置类的包路径protected Set doScan(String... basePackages) {Set beanDefinitions = new LinkedHashSet<>();for (String basePackage : basePackages) {//寻找到所有标识了@Component注解的BeanDefinitionSet candidates = findCandidateComponents(basePackage);for (BeanDefinition candidate : candidates) {//....省略代码if (candidate instanceof AbstractBeanDefinition) {//处理BeanDefinitionpostProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);}if (candidate instanceof AnnotatedBeanDefinition) { //处理普通的bean定义注解 , @Lazy @Primary @DependsOn @Role @DescriptionAnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);}//...省略代码//将BeanDefinition注册到容器中registerBeanDefinition(definitionHolder, this.registry);} }123456789101112131415161718192021处理BeanDefinition
protected void postProcessBeanDefinition(AbstractBeanDefinition beanDefinition, String beanName) { //设置默认值beanDefinition.applyDefaults(this.beanDefinitionDefaults);//这里默认为空if (this.autowireCandidatePatterns != null) {beanDefinition.setAutowireCandidate(PatternMatchUtils.simpleMatch(this.autowireCandidatePatterns, beanName));}}12345678设置默认值
public void applyDefaults(BeanDefinitionDefaults defaults) {//默认为nullBoolean lazyInit = defaults.getLazyInit();if (lazyInit != null) {setLazyInit(lazyInit);}//默认为0setAutowireMode(defaults.getAutowireMode());//默认为0setDependencyCheck(defaults.getDependencyCheck());//默认为nullsetInitMethodName(defaults.getInitMethodName());setEnforceInitMethod(false);//默认为nullsetDestroyMethodName(defaults.getDestroyMethodName());setEnforceDestroyMethod(false);}1234567891011121314151617处理普通的bean定义注解
public static void processCommonDefinitionAnnotations(AnnotatedBeanDefinition abd) {processCommonDefinitionAnnotations(abd, abd.getMetadata());}static void processCommonDefinitionAnnotations(AnnotatedBeanDefinition abd, AnnotatedTypeMetadata metadata) {//从元数据中取出该注解的属性列表 , 不为空说明有标识该注解AnnotationAttributes lazy = attributesFor(metadata, Lazy.class);if (lazy != null) {abd.setLazyInit(lazy.getBoolean("value"));}else if (abd.getMetadata() != metadata) {lazy = attributesFor(abd.getMetadata(), Lazy.class);if (lazy != null) {abd.setLazyInit(lazy.getBoolean("value"));}}//判断元数据中是否有该注解if (metadata.isAnnotated(Primary.class.getName())) {abd.setPrimary(true);}AnnotationAttributes dependsOn = attributesFor(metadata, DependsOn.class);if (dependsOn != null) {abd.setDependsOn(dependsOn.getStringArray("value"));}AnnotationAttributes role = attributesFor(metadata, Role.class);if (role != null) {abd.setRole(role.getNumber("value").intValue());}AnnotationAttributes description = attributesFor(metadata, Description.class);if (description != null) {abd.setDescription(description.getString("value"));} }123456789101112131415161718192021222324252627282930313233