1 介绍
public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {
/**
* Modify the application context's internal bean definition registry after its
* standard initialization. All regular bean definitions will have been loaded,
* but no beans will have been instantiated yet. This allows for adding further
* bean definitions before the next post-processing phase kicks in.
* @param registry the bean definition registry used by the application context
* @throws org.springframework.beans.BeansException in case of errors
*/
void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;
}
继承了BeanFactoryPostProcessor, 又声明了一个方法postProcessBeanDefinitionRegistry
该方法的执行时机:
All regular bean definitions will have been loaded,but no beans will have been instantiated yet
所有的合法的bean定义将要被加载,但是bean还没有被实例化。
可见这个方法是优先postProcessBeanFactory
方法执行的
可以通过这个接口向容器中注册一些组件
2 演示
2.1 演示执行顺序
postProcessBeanDefinitionRegistry
优先postProcessBeanFactory
方法执行
定义一个BeanDefinitionRegistryPostProcessor实现类
package study.wyy.spring.extend.beanDefinitionRegistryPostProcessor.spi;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.stereotype.Component;
/**
* @author wyaoyao
* @data 2020-11-23 13:22
* BeanFactory后置处理器:
* BeanFactory标准初始化之后调用,此时所有的bean定义已经保存加载BeanFactory
* 但是Bean的实例还未创建
*
*/
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
/****
* BeanDefinitionRegistryPostProcessor接口中定义的方法
* @param registry
* @throws BeansException
*/
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
System.out.println("MyBeanDefinitionRegistryPostProcessor => postProcessBeanDefinitionRegistry。。。。。");
}
/****
* BeanFactoryPostProcessor接口中定义的方法
* BeanFactory标准初始化之后调用,此时所有的bean定义已经保存加载BeanFactory
* @param beanFactory
* @throws BeansException
*/
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("MyBeanDefinitionRegistryPostProcessor => postProcessBeanFactory。。。。。");
}
}
配置类
package study.wyy.spring.extend.beanDefinitionRegistryPostProcessor.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import study.wyy.spring.extend.beanDefinitionRegistryPostProcessor.spi.MyBeanDefinitionRegistryPostProcessor;
/**
* @author by wyaoyao
* @Description
* @Date 2020/11/23 9:14 下午
*/
@Configuration
public class SpringConfig {
@Bean
public static MyBeanDefinitionRegistryPostProcessor myBeanDefinitionRegistryPostProcessor(){
return new MyBeanDefinitionRegistryPostProcessor();
}
}
测试
@org.junit.Test
public void test01(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
}
输出
MyBeanDefinitionRegistryPostProcessor => postProcessBeanDefinitionRegistry。。。。。
MyBeanDefinitionRegistryPostProcessor => postProcessBeanFactory。。。。。
2.2 演示注册一个bean
实现BeanDefinitionRegistryPostProcessor
package study.wyy.spring.extend.beanDefinitionRegistryPostProcessor.spi;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.stereotype.Component;
import study.wyy.spring.extend.beanDefinitionRegistryPostProcessor.bean.LifeBean;
/**
* @author wyaoyao
* @data 2020-11-23 13:22
* BeanFactory后置处理器:
* BeanFactory标准初始化之后调用,此时所有的bean定义已经保存加载BeanFactory
* 但是Bean的实例还未创建
*
*/
@Component
public class MyBeanDefinitionRegistryPostProcessor2 implements BeanDefinitionRegistryPostProcessor {
/****
* BeanDefinitionRegistryPostProcessor接口中定义的方法
* @param registry
* @throws BeansException
*/
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
int beanDefinitionCount = registry.getBeanDefinitionCount();
System.out.println("MyBeanDefinitionRegistryPostProcessor => postProcessBeanDefinitionRegistry bean的数量: " + beanDefinitionCount);
// 注册一个bean
RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(LifeBean.class);
// 设置为单例
rootBeanDefinition.setScope("singleton");
// 设置initMethodName
//rootBeanDefinition.setInitMethodName();
// 可见之前通过注解设置的bean的特点,都可以在这进行设置
registry.registerBeanDefinition("lifeBean",rootBeanDefinition);
}
/****
* BeanFactoryPostProcessor接口中定义的方法
* BeanFactory标准初始化之后调用,此时所有的bean定义已经保存加载BeanFactory
* @param beanFactory
* @throws BeansException
*/
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
int beanDefinitionCount = beanFactory.getBeanDefinitionCount();
System.out.println("MyBeanDefinitionRegistryPostProcessor => postProcessBeanFactory bean 的数量: " + beanDefinitionCount);
}
}
配置类
package study.wyy.spring.extend.beanDefinitionRegistryPostProcessor.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @author by wyaoyao
* @Description
* @Date 2020/11/23 9:14 下午
*/
@Configuration
@ComponentScan("study.wyy.spring.extend.beanDefinitionRegistryPostProcessor.spi")
public class SpringConfig1 {
}
定义一个bean
package study.wyy.spring.extend.beanDefinitionRegistryPostProcessor.bean;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
* @author wyaoyao
* @data 2020-11-23 08:14
*/
public class LifeBean implements InitializingBean, DisposableBean {
public LifeBean() {
System.out.println("LifeBean constructor。。。");
}
/**
* 销毁的时候执行
* @throws Exception
*/
@Override
public void destroy() throws Exception {
System.out.println("LifeBean4 destroy。。。");
}
/**
* bean的属性设置完成之后执行
* @throws Exception
*/
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("LifeBean4 afterPropertiesSet。。。");
}
@PostConstruct
public void postConstruct(){
System.out.println("LifeBean4 postConstruct。。。");
}
@PreDestroy
public void preDestroy(){
System.out.println("LifeBean4 preDestroy。。。");
}
public void init(){
System.out.println("LifeBean4 init。。。");
}
public void destroyOnBean(){
System.out.println("LifeBean4 destroyOnBean。。。");
}
}
测试
@org.junit.Test
public void test02(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig1.class);
LifeBean lifeBean = context.getBean("lifeBean", LifeBean.class);
context.close();
}
输出
MyBeanDefinitionRegistryPostProcessor => postProcessBeanDefinitionRegistry bean的数量: 7
MyBeanDefinitionRegistryPostProcessor => postProcessBeanFactory bean 的数量: 8
LifeBean constructor。。。
LifeBean4 postConstruct。。。
LifeBean4 afterPropertiesSet。。。
LifeBean4 preDestroy。。。
LifeBean4 destroy。。。