Springboot的bean注册机制(⼀)
我们在写springboot项⽬时,@Controller、@Service、@Configuration、@Component注解的类是如何被扫描到的呢?本⽂将逐步揭开springboot组件扫描机制的⾯纱。
⽰例项⽬
先以⼀个demo展⽰⼀个普通的springboot项⽬在启动的过程中,注册了哪些bean。项⽬⽰例图如下:
主要看下⾃定义的bean⼯⼚后置处理器类
@Component
public class CustomizedBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
String[] beanDefinitionNames = BeanDefinitionNames();
System.out.println("⾃定义BeanFactoryPostProcessor查看已注册的beanDef");
for (String beanDef : beanDefinitionNames) {
System.out.println(beanDef);
}
System.out.println("beanDef共计" + beanDefinitionNames.length);
}
}
⾃定义bean⼯⼚后置处理器的postProcessBeanFactory⽅法在fresh()====> PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory,beanFactoryPostProcessors)
处调⽤
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors){
......
// ⼀般地,⾃定义的BeanFactoryPostProcessor的重写⽅法会在此处调⽤
invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
/
/ Clear cached merged bean definitions since the post-processors might have
// modified the original metadata, e.g. replacing placeholders
beanFactory.clearMetadataCache();
}
我们的⾃定义bean⼯⼚后置处理器中的⽅法就是在invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory)这⾥执⾏的。
需要注意的是,代码执⾏到此处时,我们demo⽰例中的bean已经全被注册。在⽇志中我们可以看到⽬前已经注册了哪些bean
⾃定义BeanFactoryPostProcessor查看已注册的beanDef
t.annotation.internalConfigurationAnnotationProcessor
t.annotation.internalAutowiredAnnotationProcessor
t.annotation.internalRequiredAnnotationProcessor
t.annotation.internalCommonAnnotationProcessor
t.event.internalEventListenerProcessor
t.event.internalEventListenerFactory
application
org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory
customizedBeanFactoryPostProcessor
appConfig
testController
staffService
testService
user
com.gsonkeno.springbootbean.bean.Staff
org.springframework.boot.autoconfigure.AutoConfigurationPackages
org.springframework.t.PropertyPlaceholderAutoConfiguration
org.springframework.dition.BeanTypeRegistry
propertySourcesPlaceholderConfigurer
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration
mbeanExporter
objectNamingStrategy
mbeanServer
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration
springApplicationAdminRegistrar
org.springframework.t.ConfigurationPropertiesAutoConfiguration
org.t.properties.ConfigurationPropertiesBindingPostProcessor
org.t.properties.ConfigurationBeanFactoryMetadata
org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration
spring.info-org.springframework.boot.autoconfigure.info.ProjectInfoProperties
beanDef共计30
bean注册流程简析
refresh⽅法内部会调⽤invokeBeanFactoryPostProcessors(beanFactory)⽅法,该⽅法会调⽤PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors())⽅法。
这⾥就要简短说明⼀下PostProcessorRegistrationDelegate这个类的作⽤,这是⼀个委托类,专门为AbstractApplicationContext类的xxxPostProcessorsxxx⽅法逻辑提供代理。
那么我们再次看下invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors())⽅法做了什么事?
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors){
// Invoke BeanDefinitionRegistryPostProcessors first, if any.
Set<String> processedBeans =new HashSet<>();
// 1 如果beanFactory是BeanDefinitionRegistry类型,也就是关于bean注册的bean⼯⼚时,
// 会进⼊该分⽀,进⾏bean的扫描、解析、注册
// SpringBoot启动时,beanFactory是DefaultListableBeanFactory类型,会进⼊该分⽀
if(beanFactory instanceof BeanDefinitionRegistry){
BeanDefinitionRegistry registry =(BeanDefinitionRegistry) beanFactory;
// 1.1 这⾥分离出两种BeanFactoryPostProcessor,⼀种是关于bean注册的registryProcessors,
// 剩下的是另外⼀种正规的,regularPostProcessors
List<BeanFactoryPostProcessor> regularPostProcessors =new ArrayList<>();
List<BeanDefinitionRegistryPostProcessor> registryProcessors =new ArrayList<>();
for(BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors){
if(postProcessor instanceof BeanDefinitionRegistryPostProcessor){
BeanDefinitionRegistryPostProcessor registryProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
registryProcessor.postProcessBeanDefinitionRegistry(registry);
registryProcessors.add(registryProcessor);
}
else{
regularPostProcessors.add(postProcessor);
}
}
// Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let the bean factory post-processors apply to them!
// Separate between BeanDefinitionRegistryPostProcessors that implement
// PriorityOrdered, Ordered, and the rest.
List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors =new ArrayList<>();
/
/ 1.2 First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
// 实现BeanDefinitionRegistryPostProcessors接⼝与PriorityOrdered接⼝的bean注册⼯⼚后置处理器
// 先发挥作⽤,这⾥获取到的就是ConfigurationClassPostProcessor
String[] postProcessorNames =
for(String ppName : postProcessorNames){
//如果ppName实现了PriorityOrdered接⼝,则优先于Ordered接⼝处理
if(beanFactory.isTypeMatch(ppName, PriorityOrdered.class)){
currentRegistryProcessors.Bean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
// 这⾥就是ConfigurationClassPostProcessor发挥作⽤解析配置类的时候invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
// 1.3 Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
postProcessorNames = BeanNamesForType(BeanDefinitionRegistryPostProcessor.class,true,false);
for(String ppName : postProcessorNames){
//如果ppName实现了Ordered接⼝,则次于PriorityOrdered接⼝处理
if(!ains(ppName)&& beanFactory.isTypeMatch(ppName, Ordered.class)){
currentRegistryProcessors.Bean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
// 1.4 Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
// 与上⾯类似,到剩下的所有的BeanDefinitionRegistryPostProcessors,执⾏其重写⽅法 postProcessBeanDefinitionRegistry boolean reiterate =true;
boolean reiterate =true;
while(reiterate){
reiterate =false;
postProcessorNames = BeanNamesForType(BeanDefinitionRegistryPostProcessor.class,true,false); for(String ppName : postProcessorNames){
适合新手的spring bootif(!ains(ppName)){
currentRegistryProcessors.Bean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
reiterate =true;
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
}
// 1.5 Now, invoke the postProcessBeanFactory callback of all processors handled so far.
// 执⾏BeanFactoryPostProcessor接⼝的postProcessBeanFactory⽅法,执⾏该⽅法前,上下⽂中的
// bean应该都已经被加载,但是还未初始化,在这个⽅法中,你可以对这些bean增加或者重写属性invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
}
// 2
else{
// 2.1 Invoke factory processors registered with the context instance.
// 执⾏BeanFactoryPostProcessor接⼝的postProcessBeanFactory⽅法,执⾏该⽅法前,上下⽂中的
// bean应该都已经被加载,但是还未初始化,在这个⽅法中,你可以对这些bean增加或者重写属性invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
}
// 3 Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let the bean factory post-processors apply to them!
// 将还未处理的BeanFactoryPostProcessor分为三类,实现PriorityOrdered,Ordered接⼝的,和剩下的
String[] postProcessorNames =
// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
// Ordered, and the rest.
List<BeanFactoryPostProcessor> priorityOrderedPostProcessors =new ArrayList<>();
List<String> orderedPostProcessorNames =new ArrayList<>();
List<String> nonOrderedPostProcessorNames =new ArrayList<>();
for(String ppName : postProcessorNames){
ains(ppName)){
// skip - already processed in first phase above
}
else if(beanFactory.isTypeMatch(ppName, PriorityOrdered.class)){
priorityOrderedPostProcessors.Bean(ppName, BeanFactoryPostProcessor.class));
}
else if(beanFactory.isTypeMatch(ppName, Ordered.class)){
orderedPostProcessorNames.add(ppName);
}
else{
nonOrderedPostProcessorNames.add(ppName);
}
}
// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
/
/ Next, invoke the BeanFactoryPostProcessors that implement Ordered.
List<BeanFactoryPostProcessor> orderedPostProcessors =new ArrayList<>();
for(String postProcessorName : orderedPostProcessorNames){
orderedPostProcessors.Bean(postProcessorName, BeanFactoryPostProcessor.class));
}
}
sortPostProcessors(orderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
// Finally, invoke all other BeanFactoryPostProcessors.
List<BeanFactoryPostProcessor> nonOrderedPostProcessors =new ArrayList<>();
for(String postProcessorName : nonOrderedPostProcessorNames){
nonOrderedPostProcessors.Bean(postProcessorName, BeanFactoryPostProcessor.class)); }
// ⼀般地,⾃定义的BeanFactoryPostProcessor会在此处调⽤
invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
// 4 Clear cached merged bean definitions since the post-processors might have
// modified the original metadata, e.g. replacing placeholders
beanFactory.clearMetadataCache();
}
这个⽅法主要做了以下⼏件事。
输⼊参数beanFactory, beanFactoryPostProcessors
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论