Spring精讲:什么是BeanFactory?(附源码)( 三 )

处理BeanDefinition
protected void postProcessBeanDefinition(AbstractBeanDefinition beanDefinition, String beanName) { //设置默认值beanDefinition.applyDefaults(this.beanDefinitionDefaults);//这里默认为空if (this.autowireCandidatePatterns != null) {beanDefinition.setAutowireCandidate(PatternMatchUtils.simpleMatch(this.autowireCandidatePatterns, beanName));}}设置默认值
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);}处理普通的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"));} }attributesFor(metadata, Lazy.class)
static AnnotationAttributes attributesFor(AnnotatedTypeMetadata metadata, Class annotationClass) {return attributesFor(metadata, annotationClass.getName());}static AnnotationAttributes attributesFor(AnnotatedTypeMetadata metadata, String annotationClassName) {//metadata为beanClass的注解元数据 , 存放了该类所配置的所有注解//annotationClassName为需要寻找的注解名称return AnnotationAttributes.fromMap(metadata.getAnnotationAttributes(annotationClassName, false));}default Map getAnnotationAttributes(String annotationName,boolean classValuesAsString) {//遍历元数据中的所有注解MergedAnnotation annotation = getAnnotations().get(annotationName,null, MergedAnnotationSelectors.firstDirectlyDeclared());//不存在则返回null,否则返回一个mapif (!annotation.isPresent()) {return null;}return annotation.asAnnotationAttributes(Adapt.values(classValuesAsString, true));}> 以上为扫描@Component注解类进行解析元数据填充属性的逻辑 , 在配置类中以@Bean方式注册的BeanDefinition填充属性过程在ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(configClasses)


推荐阅读