Home > Java > javaTutorial > Spring container extension point: Bean post-processor

Spring container extension point: Bean post-processor

php是最好的语言
Release: 2018-08-10 15:07:11
Original
2483 people have browsed it

BeanPostProcessor (Bean post-processor) is often used to modify the internal values ​​​​of the bean; implement the dynamic proxy of the bean, etc.

BeanFactoryPostProcessor and BeanPostProcessor are extension points exposed when spring initializes beans. But what's the difference?
It can be seen from the picture in "Understanding Bean Life Cycle": BeanFactoryPostProcessor is the earliest called in the life cycle, much earlier than BeanPostProcessor. It is executed after the spring container loads the bean definition file and before the bean is instantiated. In other words, Spring allows BeanFactoryPostProcessor to read and modify bean configuration metadata before the container creates the bean. For example, add the properties and values ​​of the bean, reset whether the bean is a candidate for autowiring, reset the bean's dependencies, etc.

You can configure multiple BeanFactoryPostProcessors at the same time in the srping configuration file, and control the execution order of each BeanFactoryPostProcessor by setting the 'order' attribute when registering in xml.

BeanFactoryPostProcessor interface is defined as follows:

public interface BeanFactoryPostProcessor {
    void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
}
Copy after login

The interface has only one method postProcessBeanFactory. The parameter of this method is of ConfigurableListableBeanFactory type. In actual development, we often use its getBeanDefinition() method to obtain the metadata definition of a certain bean: BeanDefinition. It has these methods:
Spring container extension point: Bean post-processor

Look at an example:
A bean is defined in the configuration file:

<bean id="messi" class="twm.spring.LifecycleTest.footballPlayer">
    <property name="name" value="Messi"></property>
    <property name="team" value="Barcelona"></property>
</bean>
Copy after login

Create the class beanFactoryPostProcessorImpl and implement the interface BeanFactoryPostProcessor:

public class beanFactoryPostProcessorImpl implements BeanFactoryPostProcessor{

    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("beanFactoryPostProcessorImpl");
        BeanDefinition bdefine=beanFactory.getBeanDefinition("messi");
        System.out.println(bdefine.getPropertyValues().toString());
        MutablePropertyValues pv =  bdefine.getPropertyValues();  
            if (pv.contains("team")) {
                PropertyValue ppv= pv.getPropertyValue("name");
                TypedStringValue obj=(TypedStringValue)ppv.getValue();
                if(obj.getValue().equals("Messi")){
                    pv.addPropertyValue("team", "阿根延");  
                }
        }  
            bdefine.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    }
}
Copy after login

Calling class:

public static void main(String[] args) throws Exception {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
    footballPlayer obj = ctx.getBean("messi",footballPlayer.class);
    System.out.println(obj.getTeam());
}
Copy after login

Output:

PropertyValues: length=2; bean property 'name'; bean property 'team'
Agenyan

The PropertyPlaceholderConfigurer class mentioned in "PropertyPlaceholderConfigurer Application" is an implementation of the BeanFactoryPostProcessor interface. It will replace the placeholders in the class definition (such as ${jdbc.url}) with the corresponding content of the properties file before the container creates the bean.

Related recommendations:

Introduction to the methods of Spring Bean extension interface

ResourceLoaderAware interface - [Spring Chinese Manual]

The above is the detailed content of Spring container extension point: Bean post-processor. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template