第一种实现方案
第一种:通过嵌套注册自定义的BeanDefinitionRegistryPostProcessor 类BEAN的方式,这种方式实现思路是:PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors会先执行已获得BeanDefinitionRegistryPostProcessor BEAN集合,执行完这些BEAN集合后(这里我称为第一轮或第一层),会再次尝试获取第二轮、第三轮一直到获取的BeanDefinitionRegistryPostProcessor BEAN集合全部处理完成为止,框架相关代码片段如下:
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);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
}
实现方式代码如下:
//如下是第一层自定义的BeanDefinitionRegistryPostProcessor BEAN,内部再注册真正用于替换BEAN目的NewBeanDefinitionRegistryPostProcessor BEAN
//author:zuowenjun
@Component
public class FirstDynamicBeanPostProcessor implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
BeanDefinitionBuilder beanDefinitionBuilder=BeanDefinitionBuilder.genericBeanDefinition(NewBeanDefinitionRegistryPostProcessor.class);
beanDefinitionRegistry.registerBeanDefinition(“newBeanDefinitionRegistryPostProcessor”,beanDefinitionBuilder.getBeanDefinition());
System.out.printf("【%1
t
F
tF %1
tFtT.%1$tL】%s,FirstDynamicBeanPostProcessor.postProcessBeanDefinitionRegistry%n", new Date());
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
System.out.printf("【%1$tF %1$tT.%1$tL】%s,FirstDynamicBeanPostProcessor.postProcessBeanFactory%n", new Date());
}
}
//用于将原依赖的问题Bean替换为同名的新的适配器Bean(下文中所有替换方式最终都要使用该类)
public class NewBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
System.out.printf("【%1
t
F
tF %1
tFtT.%1$tL】%s,NewBeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry%n",new Date());
boolean isContainsSpecialBean = ((DefaultListableBeanFactory) beanDefinitionRegistry).containsBean(“old问题Bean名称”);
if (isContainsSpecialBean) {
beanDefinitionRegistry.removeBeanDefinition(“old问题Bean名称”);
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(DemoCompentB.class);
beanDefinitionBuilder.addConstructorArgValue(((DefaultListableBeanFactory) beanDefinitionRegistry).getBean(NewBeanAdapter.class)); //NewBeanAdapter为继承自old问题Bean的装饰者、适配器类
AbstractBeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
beanDefinition.setPrimary(true);
beanDefinitionRegistry.registerBeanDefinition(“old问题Bean名称”, beanDefinition);
}
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
System.out.printf("【%1$tF %1$tT.%1$tL】%s,NewBeanDefinitionRegistryPostProcessor.postProcessBeanFactory%n",new Date());
}
}
最终执行的顺序如下:(可以看到NewBeanDefinitionRegistryPostProcessor是在OldBeanDefinitionRegistryPostProcessor之后执行的,这样就可以正常替换Bean定义了)
FirstDynamicBeanPostProcessor.postProcessBeanDefinitionRegistry (第一轮)
OldBeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry (第一轮)
NewBeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry (第二轮)
FirstDynamicBeanPostProcessor.postProcessBeanFactory
OldBeanDefinitionRegistryPostProcessor.postProcessBeanFactory
NewBeanDefinitionRegistryPostProcessor.postProcessBeanFactory
第二种实现方案
第二种:通过额外定义一个BeanDefinitionRegistryPostProcessor BEAN并实现PriorityOrdered、BeanFactoryAware接口,确保该BEAN最先被执行(Order=0),然后在postProcessBeanDefinitionRegistry方法中通过applicationContext.setDependencyComparator设置自定义的排序器,达到排序BeanDefinitionRegistryPostProcessor BEAN集合的执行顺序,这种方式实现思路是:在执行BeanDefinitionRegistryPostProcessor BEAN集合前会调用sortPostProcessors方法进行排序,而排序规则又依赖于DependencyComparator,通过控制排序规则实现间接控制执行顺序,先看框架的代码片段:
private static void sortPostProcessors(List<?> postProcessors, ConfigurableListableBeanFactory beanFactory) {
Comparator<Object> comparatorToUse = null;
if (beanFactory instanceof DefaultListableBeanFactory) {
comparatorToUse = ((DefaultListableBeanFactory) beanFactory).getDependencyComparator();
}
if (comparatorToUse == null) {
comparatorToUse = OrderComparator.INSTANCE;
}
postProcessors.sort(comparatorToUse);
}
//如下是invokeBeanFactoryPostProcessors方法片段:
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
实现方式代码如下:
@Component
public static class FirstBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor, PriorityOrdered
, BeanFactoryAware {
private BeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory=beanFactory;
}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
((DefaultListableBeanFactory) beanFactory).setDependencyComparator(new OrderComparator(){
@Override
protected int getOrder(Object obj) {
if (obj instanceof NewBeanDefinitionRegistryPostProcessor){ //如果是NewBeanDefinitionRegistryPostProcessor则将它的排序序号设置为最大
return Integer.MAX_VALUE;
}
return super.getOrder(obj)-1; //其余的全部设为比它小1
}
});
System.out.printf("【%1$tF %1$tT.%1$tL】%s,FirstBeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry%n", new Date());
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
System.out.printf("【%1$tF %1$tT.%1$tL】%s,FirstBeanDefinitionRegistryPostProcessor.postProcessBeanFactory%n", new Date());
}
@Override
public int getOrder() {
return 0;//确保
}
}
最终执行的顺序如下:(NewBeanDefinitionRegistryPostProcessor在OldBeanDefinitionRegistryPostProcessor后面执行)
FirstBeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry (第1批:实现PriorityOrdered执行)
OldBeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry (第3批:普通BEAN执行)
NewBeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry (第3批:普通BEAN执行)
FirstBeanDefinitionRegistryPostProcessor.postProcessBeanFactory
OldBeanDefinitionRegistryPostProcessor.postProcessBeanFactory
NewBeanDefinitionRegistryPostProcessor.postProcessBeanFactory
第三种实现方案
第三种:通过自定义DeferredImportSelector类并配合@Import注解,实现NewBeanDefinitionRegistryPostProcessor最后才被注册成为BEAN,最后才有机会执行,这种方式实现思路是:因为DeferredImportSelector的执行时机是在所有@Configuration类型bean解析之后。
实现方式代码如下:
public static class BeansImportSelector implements DeferredImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{NewBeanDefinitionRegistryPostProcessor.class.getName()};
}
}
@Configuration
@Import(BeansImportSelector.class)
public class BeansConfig {
}
最终执行的顺序如下:(NewBeanDefinitionRegistryPostProcessor在OldBeanDefinitionRegistryPostProcessor后面执行)
OldBeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry
NewBeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry
OldBeanDefinitionRegistryPostProcessor.postProcessBeanFactory
NewBeanDefinitionRegistryPostProcessor.postProcessBeanFactory
四、引发的思考
如上就是三种实现方式,至于哪种方式最好,这要看具体的场景,第一种、第三种影响面相对较小,而第二种因为涉及更换DependencyComparator,可能影响的是全局。另外之所以会研究如上实现方式,主要原因还是因为我们的项目框架代码没有考虑扩展性及规范性,比如要动态注册BEAN,至少应实现PriorityOrdered或Order接口或指明@Order注解,这样当我们在某些特定场景需要做一下优化或替换时,则可以直接采取相同的方式但指定Order在前或在后即可,也就不用这么复杂了,比如:
@Component
public class OldBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor,Order {
@Override
public int getOrder() {
return 100;
}
...
}
@Component
public class NewBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor,Order {
@Override
public int getOrder() {
return 101;//只需序号在OldBeanDefinitionRegistryPostProcessor.getOrder之后即可
}
...
}
USB Microphone https://www.soft-voice.com/
Wooden Speakers https://www.zeshuiplatform.com/
亚马逊测评 www.yisuping.cn
深圳网站建设www.sz886.com