学习Spring注解的笔记
若有不当之处,请您指出,您的指点是我的荣幸
我们也可以通过BeanDefinitionRegistryPostProcessor给容器注册bean
自定义BeanDefinitionRegistryPostProcessor类
值得注意的是MyBeanDefinitionRegistryPostProcessor
实现了BeanDefinitionRegistryPostProcessor
接口,而BeanDefinitionRegistryPostProcessor
继承了BeanFactoryPostProcessor
@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
//BeanDefinitionRegistry就是bean定义信息的保存中心
//以后的BeanFactory就是根据BeanDefinitionRegistry里面保存的每一个bean的定义信息创建bean实例的(例如bean是单例的还是多例的,bean的类型,bean的id是什么等)
@Override
//该方法打上断点
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
System.out.println("MyBeanDefinitionRegistryPostProcessor-》postProcessBeanDefinitionRegistry...bean的数量"+registry.getBeanDefinitionCount());
//除此之外还可以自己注册一个组件
//首先要获取bean的定义信息,这里以注册一个blue为例
//有两种方式获取BeanDefinition
//RootBeanDefinition beanDefinition = new RootBeanDefinition(Blue.class);
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Blue.class).getBeanDefinition();
//这里是想容器中注册bean的定义信息,第一个参数是bean的名称,第二个是bean的定义信息
registry.registerBeanDefinition("hello",beanDefinition);
}
@Override
//该方法打上断点
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("MyBeanDefinitionRegistryPostProcessor-》postProcessBeanFactory...bean的数量:"+beanFactory.getBeanDefinitionCount());
}
}
自定义BeanFactoryPostProcessor类
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("MyBeanFactoryPostProcessor-》BeanFactoryPostProcessor...");
int count = beanFactory.getBeanDefinitionCount();
String[] names = beanFactory.getBeanDefinitionNames();
System.out.println("当前beanFactory中有"+count+"个Bean");
System.out.println(Arrays.asList(names));
}
}
配置类
@ComponentScan("com.cb414.ext")
@Configuration
public class ExtConfig {
@Bean
public Blue blue(){
return new Blue();
}
}
测试类
public class IOCTestExt {
@Test
public void test01(){
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(ExtConfig.class);
annotationConfigApplicationContext.close();
}
}
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...");
}
}
运行结果
MyBeanDefinitionRegistryPostProcessor-》postProcessBeanDefinitionRegistry...bean的数量10
MyBeanDefinitionRegistryPostProcessor-》postProcessBeanFactory...bean的数量:11
MyBeanFactoryPostProcessor-》BeanFactoryPostProcessor...
当前beanFactory中有11个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,
myBeanDefinitionRegistryPostProcessor,
myBeanFactoryPostProcessor,
blue,
hello]
blue..constructor
blue..constructor
其实从运行结果上,我们不难发现,BeanDefinitionRegistryPostProcessor
要比BeanFactoryPostProcessor
先执行,因为BeanDefinitionRegistryPostProcessor
是在bean的定义信息将要被加载时,进行运作的。而BeanFactoryPostProcessor
则是bean的定义信息已经被加载了,但是bean的实例还没有被创建的时候运作的。这一步在后面的源码中也有体现。
debug运行之后,会来到refresh()
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
......
try {
......
// Invoke factory processors registered as beans in the context.
//这里其实也是执行BeanFactory后置处理的方法
invokeBeanFactoryPostProcessors(beanFactory);
......
}
catch (BeansException ex) {
......
}
finally {
......
}
}
}
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
//继续查看invokeBeanFactoryPostProcessors的源码
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
......
}
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
Set<String> processedBeans = new HashSet<String>();
//如果这个beanFactory是BeanDefinitionRegistry类型的话
if (beanFactory instanceof BeanDefinitionRegistry) {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList<BeanFactoryPostProcessor>();
List<BeanDefinitionRegistryPostProcessor> registryProcessors = new LinkedList<BeanDefinitionRegistryPostProcessor>();
.....
//创建一个用来存BeanDefinitionRegistryPostProcessor的List
List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<BeanDefinitionRegistryPostProcessor>();
//拿到所有的BeanDefinitionRegistryPostProcessor类型的后置处理器
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
//先拿到实现了PriorityOrdered接口的
//拿到后执行
//遍历
for (String ppName : postProcessorNames) {
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
//再拿到实现了Ordered接口的
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
//遍历
for (String ppName : postProcessorNames) {
if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
//执行BeanDefinitionRegistryPostProcessor()
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
// 最后拿到所有剩下的BeanDefinitionRegistryPostProcessor
boolean reiterate = true;
while (reiterate) {
reiterate = false;
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
//遍历
for (String ppName : postProcessorNames) {
if (!processedBeans.contains(ppName)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
reiterate = true;
}
}
//排序
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
//执行BeanDefinitionRegistryPostProcessor()
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
}
//执行BeanFactoryPostProcessor()
//这里也就能解释为什么MyBeanDefinitionRegistryPostProcessor中的postProcessBeanFactory()要比MyBeanFactoryPostProcessor中的postProcessBeanFactory()先执行。
invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
}
else {
// Invoke factory processors registered with the context instance.
invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
}
//这部分是用来执行BeanFactoryPostProcessor的操作的。
//MyBeanFactoryPostProcessor也是在这里运行的
......
}