首页  

Spring的BeanFactoryPostProcessor和BeanPostProcessor     所属分类 spring 浏览量 865
BeanFactoryPostProcessor
BeanPostProcessor


public interface BeanFactoryPostProcessor {
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
 

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.
     
在bean创建之前,修改bean的定义
把bean的scope从singleton改为prototype
修改property的值

BeanDefinition bd = beanFactory.getBeanDefinition("xxxbean");
MutablePropertyValues pv =  bd.getPropertyValues();  
if (pv.contains("xxx")) {  
    pv.addPropertyValue("xxx", "abc");  
} 
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
        

内置的一些BeanFactoryPostProcessor实现类 
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
org.springframework.beans.factory.config.PropertyOverrideConfigurer
org.springframework.beans.factory.config.CustomEditorConfigurer       注册自定义的属性编辑器


BeanPostProcessor public interface BeanPostProcessor { // before InitializingBean's afterPropertiesSet or custom init-method Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException; // after InitializingBean's afterPropertiesSet or custom init-method Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException; } 实例化bean之后,初始化方法前后执行 InitializingBean接口 afterPropertiesSet init-method 方法 一些内置的BeanPostProcessor实现类 org.springframework.context.annotation.CommonAnnotationBeanPostProcessor org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor org.springframework.context.support.ApplicationContextAwareProcessor CommonAnnotationBeanPostProcessor 处理 @Resource
spring bean 生命周期实例 spring bean生命周期及扩展点

上一篇     下一篇
关于流计算的若干想法

SpringBoot admin 简介

spring如何解决循环依赖

yaml 和 properties 互相转换

springboot应用首次访问慢解决方法

SpringMVC consumes 和 produces 用法