Loading

&【04】invokeBeanFactoryPostProcessors方法调用

invokeBeanFactoryPostProcessors 方法调用

image-20220604200915640

在xmlBeanDefinition解析后,实例化之前,可用此接口完成对beanDefinition的动态修改

image-20220604190114767

BeanDefinitionRegistryPostProcessor 方法调用

代码示例:

import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
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.GenericBeanDefinition;
import org.springframework.stereotype.Component;

@Component
public class BeanPro implements BeanDefinitionRegistryPostProcessor {

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();
        genericBeanDefinition.setBeanClass(TestBean.class);
        MutablePropertyValues propertyValues = genericBeanDefinition.getPropertyValues();
        propertyValues.add("myName","xxxx");
        registry.registerBeanDefinition("testBean",genericBeanDefinition);
    }

    /**
     * 需要用到容器对象 来修改某些东西
     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }
}
  1. 调用实现了 PriorityOrdered 排序接口

    image-20220604190332978

  2. 调用实现了 Ordered 排序接口

    image-20220604190624321

  3. 没有实现接口的调用

    image-20220604190757122

BeanFactoryPostProcessor 方法调用

image-20220604201053739

image-20220604201109809

总结

对上面接口的理解:

获取 BeanDefinitionRegistry 对象,获取到这个对象就可以获取这个对象中注册的所有BeanDefinition 对象,我们拥有这个对象就可以完成里面所有 BeanDefinition 对象的修改、新增操作。





posted @ 2022-07-15 16:28  yescctv6  阅读(37)  评论(0)    收藏  举报