关于spring:spring里的BeanFactoryPostProcessor

先看下接口定义:

/**
 * Factory hook that allows for custom modification of an application context's
 * bean definitions, adapting the bean property values of the context's underlying
 * bean factory.
 * ......
 * @author Juergen Hoeller
 * @author Sam Brannen
 * @since 06.07.2003
 * @see BeanPostProcessor
 * @see PropertyResourceConfigurer
 */
@FunctionalInterface
public interface BeanFactoryPostProcessor {

    /**
     * Modify the application context's internal bean factory after its standard
     * initialization. All bean definitions will have been loaded, but no beans
     * will have been instantiated yet. This allows for overriding or adding
     * properties even to eager-initializing beans.
     * @param beanFactory the bean factory used by the application context
     * @throws org.springframework.beans.BeansException in case of errors
     */
    void postProcessBeanFactory办法上的形容:(ConfigurableListableBeanFactory beanFactory) throws BeansException;

}

对于BeanFactoryPostProcessor这个接口,正文是这样说的:这是一个扩大点,它提供了使用者批改利用上下文里的bean definitions,改写bean factory上下文里的bean属性值。这里额定贴一下BeanDefinition这个类上的正文:

/**
 * A BeanDefinition describes a bean instance, which has property values,
 * constructor argument values, and further information supplied by
 * concrete implementations.
 * .....
 **/

而后咱们再来看一下BeanFactoryPostProcessor的postProcessBeanFactory办法上的形容:在bean definition已被加载(如将从xml文件解析进去的bean入到了beanDefinitionMap里)但还没被实例化的时候,能够批改这些bean definition的属性。
接着再看下BeanFactoryPostProcessor的子类:

咱们来写个示例来批改下bean definition,这里有个Person类,它有一个name属性,看配置文件:
<id="person" "name"="Zhao"/>
当初咱们想给这个Person对象动静增加一个属性:

class CustomBeanDefinitionRegistryPostProcessor implements BeanFactoryPostProcessor {

    /**
     * @param configurableListableBeanFactory
     * @throws BeansException
     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        BeanDefinition beanDefinition = configurableListableBeanFactory.getBeanDefinition("person");
        PropertyValue propertyValue = new PropertyValue("interest","writing");
        beanDefinition.getPropertyValues().addPropertyValue(propertyValue);
    }
    
}
class Person{
        private String name;
        //get(),set()
    }

下面这个是对已有的bean definition进行属性批改,那是否动静新增一个bean到spring上下文里呢?应用BeanFactoryPostProcessor的子类BeanDefinitionRegistryPostProcessor是能够的:

class CustomBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

    /**
     * @param beanDefinitionRegistry
     * @throws BeansException
     */
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        RootBeanDefinition beanDefinition = new RootBeanDefinition(Person.class);
        beanDefinitionRegistry.registerBeanDefinition("person",beanDefinition);
    }

}

class Person{
        private String name;
        //get(),set()
    }

对于一个一般的Person对象,咱们通过重写postProcessBeanDefinitionRegistry办法将其退出到了spring IOC容器里了。

【腾讯云】云产品限时秒杀,爆款1核2G云服务器,首年50元

阿里云限时活动-2核2G-5M带宽-60G SSD-1000G月流量 ,特惠价99元/年(原价1234.2元/年,可以直接买3年),速抢

本文由乐趣区整理发布,转载请注明出处,谢谢。

You may also like...

发表评论

邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据