先看下接口定义:

/**
 * 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的子类:
image.png

我们来写个示例来修改下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容器里了。


步履不停
38 声望14 粉丝

好走的都是下坡路