1,前言
若有不当之处,请您指出,您的指点是我的荣幸
2,BeanFactoryPostProcessor
配置类
@ComponentScan("com.cb414.ext")
@Configuration
public class ExtConfig {
@Bean
public Blue blue(){
return new Blue();
}
}
Blue类
//Blue
public class Blue {
public Blue(){
System.out.println("blue..constructor");
}
public void init(){
System.out.println("blue..init...");
}
public void destory(){
System.out.println("blue...destory...");
}
}
自定义BeanFactoryPostProcessor类
//自定义BeanFactoryProcessor
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
//在方法名打上断点
//这里我们自定义实现了一个BeanFactoryPostProcessor
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
//打印一下这是哪个方法的
System.out.println("MyBeanFactoryPostProcessor...BeanFactoryPostProcessor...");
//获取此时容器中的bean的数量
int count = beanFactory.getBeanDefinitionCount();
//获取此时容器中的所有bean的名称
String[] names = beanFactory.getBeanDefinitionNames();
//打印
System.out.println("当前beanFactory中有"+count+"个Bean");
//打印
System.out.println(Arrays.asList(names));
}
}
测试类
//测试类
public class IOCTestExt {
@Test
public void test01(){
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(ExtConfig.class);
annotationConfigApplicationContext.close();
}
}
运行结果
MyBeanFactoryPostProcessor...BeanFactoryPostProcessor...
当前beanFactory中有9个Bean
[org.springframework.context.annotation.internalConfigurationAnnotationProcessor,
org.springframework.context.annotation.internalAutowiredAnnotationProcessor,
org.springframework.context.annotation.internalRequiredAnnotationProcessor,
org.springframework.context.annotation.internalCommonAnnotationProcessor,
org.springframework.context.event.internalEventListenerProcessor,
org.springframework.context.event.internalEventListenerFactory, extConfig,
myBeanFactoryPostProcessor,
blue]
blue..constructor
2.1,原理
2.1.1,refresh
debug运行,会来到refresh()
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
......
try {
......
// Invoke factory processors registered as beans in the context.
//这里是执行BeanFactory的后置处理方法
//联系到下面的 finishBeanFactoryInitialization,可以得知BeanFactoryPostProcessor是会在bean实例创建之前先被调用的
//接下来就是查看一下这个方法做了什么
invokeBeanFactoryPostProcessors(beanFactory);
......
// Instantiate all remaining (non-lazy-init) singletons.
//这里才是创建bean实例
finishBeanFactoryInitialization(beanFactory);
......
}
catch (BeansException ex) {
......
}
finally {
......
}
}
}
2.1.2,invokeBeanFactoryPostProcessors
接下来就需要研究一下invokeBeanFactoryPostProcessors()
做了什么
//invokeBeanFactoryPostProcessors
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
//自然而然的我们需要去查看一下invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors())这个方法
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
......
}
2.1.3,invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors())
那么invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors())
又做了什么
//invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors())
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
// Invoke BeanDefinitionRegistryPostProcessors first, if any.
Set<String> processedBeans = new HashSet<String>();
//判断beanFactory是否是BeanDefinitionRegistry这个类型的
if (beanFactory instanceof BeanDefinitionRegistry) {
......
}
else {
//这里注意的是,这里的beanFactoryPostProcessors是一个List,用来存储BeanFactoryPostProcessor,但实际上运行到这一步,这个List为空,所以没有啥意义
//重点看下面
invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
}
//获取容器中的所有BeanFactoryPostProcessor,并进行存储,
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
//这里在学习AOP时有讲过,分离那些实现了PriorityOrdered,Ordered接口的和没有实现接口的
List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
List<String> orderedPostProcessorNames = new ArrayList<String>();
List<String> nonOrderedPostProcessorNames = new ArrayList<String>();
//遍历获得的所有的BeanFactoryPostProcessors
for (String ppName : postProcessorNames) {
if (processedBeans.contains(ppName)) {
// skip - already processed in first phase above
}
//如果是实现了PriorityOrdered接口的话,添加到priorityOrderedPostProcessors之中
else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
}
//如果是实现了Ordered接口的话,添加到orderedPostProcessors之中
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
}
//剩下的就加到这个nonOrderedPostProcessorNames之中
else {
nonOrderedPostProcessorNames.add(ppName);
}
}
//分离完之后,就来执行
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
//接着看看它是怎么执行的
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
//再执行实现了Ordered接口的
List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
for (String postProcessorName : orderedPostProcessorNames) {
orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
//排序
sortPostProcessors(orderedPostProcessors, beanFactory);
//接着看看它是怎么执行的
invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
//最后执行没有实现接口的PostProcessor
List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>();
for (String postProcessorName : nonOrderedPostProcessorNames) {
nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
//接着看看它是怎么执行的
invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
beanFactory.clearMetadataCache();
}
2.1.4,invokeBeanFactoryPostProcessors()
private static void invokeBeanFactoryPostProcessors(
Collection<? extends BeanFactoryPostProcessor> postProcessors, ConfigurableListableBeanFactory beanFactory) {
//拿到所有的后置处理器
for (BeanFactoryPostProcessor postProcessor : postProcessors) {
//执行它们的postProcessBeanFactory()
postProcessor.postProcessBeanFactory(beanFactory);
}
}
2.1.5,总结
这也就不难理解,在容器刷新的时候,会有一步:invokeBeanFactoryPostProcessors()
会来执行容器中所有的BeanFactoryPostProcessor
的postProcessBeanFactory()
,包括自定义的BeanFactoryPostProcessor。