Spring BeanFactoryPostProcessor use interfaces in detail

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/TreeShu321/article/details/102649263

BeanFactoryPostProcessor Interface Overview

Spring container before initialization, allows to define custom extension we change the bean, BeanFactoryPostProcessor just to meet our requirements, postProcessBeanFactory BeanFactoryPostProcessor interface method, we can modify the bean definition information, such as modifying the value of the property, the scope is modified bean single or multiple cases of embodiment.

BeanFactoryPostProcessor
bean plant bean attribute processing vessel, said some of the popular is the ability to manage all beandefinition within our bean plant (not instantiated) data, modify the properties can be arbitrary.

Instructions:

@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        AbstractBeanDefinition abstractBeanDefinition = (AbstractBeanDefinition) beanFactory.getBeanDefinition("userService");

        MutablePropertyValues pv =  abstractBeanDefinition.getPropertyValues();
        pv.addPropertyValue("desc", "Desc is changed from bean factory post processor");
        abstractBeanDefinition.setScope(BeanDefinition.SCOPE_SINGLETON);

    }
}

Details BeanFactoryPostProcessor refer spring4.1.8 extended combat five: changing the definition of the bean (BeanFactoryPostProcessor Interface)

Guess you like

Origin blog.csdn.net/TreeShu321/article/details/102649263