Config xxx 工厂,修改 AAA
DefaultXXX 工厂 ,注册和移除BeanDefinition
BeanFactoryPostProcessor
-
接口1
1 .所以有postProcessBeanFactory接口,可以拿到ConfigurableListableBeanFactory.,
获得BeanDefinitioin,修好或者添加属性。 -
接口2
-
BeanDefinitionRegistryPostProcessor
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
registry.removeBeanDefinition();
registry.getBeanDefinition();
registry.registerBeanDefinition();
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
ConfigurableListableBeanFactory是不能注册BeanDefinition的,只能获取BeanDefinition和修噶BeanDefinition. (修改的Bean工厂)
Screen Shot 2022-07-06 at 1.05.41 PM.pngDefaultListableBeanFactory可以注册Bean.(注册的Bean工厂)
DefaultListableBeanFactory:
针对的是Bean或者BeanDefinition
image.png左侧是修改的功能,右侧是注册的功能
子类的BeanDefinitionRegistryPostProcessor比父类更加优先的执行,
并且具备removeBeanDefinition的功能,但是BeanFactoryPostProcessor只具备查找BeanDefinition的功能. 两者都可以对BeanDefinition
static class CCC implements BeanDefinitionRegistryPostProcessor{
//修改属性
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
beanFactory.getBeanDefinitionCount();
//可以获取信息
BeanDefinition definition = beanFactory.getBeanDefinition("aaa");
// beanFactory.re
// definition.setAttribute();
definition.setFactoryBeanName("aaa");
//这里不用注册,因为还是浅拷贝
// beanFactory.registerSingleton();
}
//删除Bean ,优先级高于postProcessBeanFactory
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
BeanDefinition beanDefinition = new RootBeanDefinition("aaa");
registry.registerBeanDefinition("aaa",beanDefinition);
registry.removeBeanDefinition("beanName");
}
}
网友评论