spring源码02——registerBean源码解析
程序员文章站
2022-05-21 19:26:02
...
类名:AnnotatedBeanDefinitionReader
registerBean(java.lang.Class<?>, java.lang.String, java.lang.Class<? extends java.lang.annotation.Annotation>…)
public void registerBean(Class<?> annotatedClass, String name, Class<? extends Annotation>... qualifiers) {
AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
AnnotationMetadata metadata = abd.getMetadata();
//profile注解处理
if (metadata.isAnnotated(Profile.class.getName())) {
AnnotationAttributes profile = MetadataUtils.attributesFor(metadata, Profile.class);
if (!this.environment.acceptsProfiles(profile.getStringArray("value"))) {
return;
}
}
//对bean的scope注解和proxymode属性进行处理
//https://blog.csdn.net/u013423085/article/details/82872533
ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
//将作用域添加到数据结构中
abd.setScope(scopeMetadata.getScopeName());
String beanName = name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry);
//处理Primary、Lazy、DependsOn等注解;并添加进入数据结构中
AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
//处理qualifiers属性
if (qualifiers != null) {
Class[] arr$ = qualifiers;
int len$ = qualifiers.length;
for(int i$ = 0; i$ < len$; ++i$) {
Class<? extends Annotation> qualifier = arr$[i$];
if (Primary.class.equals(qualifier)) {
abd.setPrimary(true);
} else if (Lazy.class.equals(qualifier)) {
abd.setLazyInit(true);
} else {
abd.addQualifier(new AutowireCandidateQualifier(qualifier));
}
}
}
//存放BeanDefinition的一种结构,多了beanName和aliases
//https://blog.csdn.net/qq924862077/article/details/73558848
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
//如果scope属性使用了proxymode,并且proxymode不是NO。涉及到把多实例bean注入到单例bean中的代理创建
definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
//把definitionHolder注册到registry,这个AnnotationConfigApplicationContext实现了registry接口,其实就是注册到AnnotationConfigApplicationContext中;
BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
}