目录
功能与作用
使用案例
spring提供的常见BeanFactoryPostProcessor
1.EventListenerMethodProcessor
2.BeanDefinitionRegistryPostProcessor
功能与作用
使用案例
spring提供的唯一BeanDefinitionRegistryPostProcessor
总结
功能与作用
参考BeanFactoryPostProcessor方法注释可知:该方法是在所有bean被加载,未被实例化前执行。对所有bean的属性可以修改也可以添加
@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修改bean的属性,比如如下举例修改该UserService类的属性由单例改为原型
@Component
public class UserService {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}
}package com.cyl.beanFactoryDemo;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;@Component
public class CylBeanFactoryBeanPostProcessor implements BeanFactoryPostProcessor {@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {BeanDefinition beanDefinition = beanFactory.getBeanDefinition("userService");//修改该bean的属性为原型beanDefinition.setScope("prototype");//添加该bean的属性name默认值为cylMutablePropertyValues propertyValues = beanDefinition.getPropertyValues();propertyValues.add("name","cyl");}}
spring提供的常见BeanFactoryPostProcessor
1.EventListenerMethodProcessor
处理添加了EventListener注解的逻辑,它的原理参见我的博文:
Spring注解@EventListener实现监听原理-CSDN博客文章浏览阅读903次,点赞21次,收藏14次。@EventListener实现原理主要通过EventListenerMethodProcessor和DefaultEventListenerFactory这两个类实现。EventListenerMethodProcessor的作用是识别所有使用eventListener注解的方法DefaultEventListenerFactory将EventListenerMethodProcessor识别出的方法封装成为监听器类以代码new AnnotationConfigApplicationContext为入https://blog.csdn.net/changyinling520/article/details/137205662?spm=1001.2014.3001.5502
2.BeanDefinitionRegistryPostProcessor
功能与作用
BeanDefinitionRegistryPostProcessor,该接口继承了BeanFactoryPostProcessor,拥有修改bean属性的功能,又拓展postProcessBeanDefinitionRegistry方法,通过代码注释理解:允许在bean被加载后,继续添加新的bean
public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {/*** Modify the application context's internal bean definition registry after its* standard initialization. All regular bean definitions will have been loaded,* but no beans will have been instantiated yet. This allows for adding further* bean definitions before the next post-processing phase kicks in.* @param registry the bean definition registry used by the application context* @throws org.springframework.beans.BeansException in case of errors*/void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;}
postProcessBeanDefinitionRegistry方法先执行,postProcessBeanFactory后执行。
可以从详见:org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors(org.springframework.beans.factory.config.ConfigurableListableBeanFactory, java.util.List<org.springframework.beans.factory.config.BeanFactoryPostProcessor>)
使用案例
用户可以自定义一个实现BeanDefinitionRegistryPostProcessor的类,注册OrderService类到spring容器中,并修改它的属性值
1.创建一个不被spring管理的OrderService,内部有UserService属性
package com.cyl.beanFactoryDemo;public class OrderService {private UserService userService;public UserService getUserService() {return userService;}public void setUserService(UserService userService) {this.userService = userService;}
}
2.创建CylBeanRegistryPostProcessor实现了BeanDefinitionRegistryPostProcessor,在postProcessBeanDefinitionRegistry方法内注册OrderService类到容器中,在postProcessBeanFactory中给OrderService添加属性userService
package com.cyl.beanFactoryDemo;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.stereotype.Component;@Component
public class CylBeanRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {@Overridepublic void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();rootBeanDefinition.setBeanClass(OrderService.class);registry.registerBeanDefinition("orderService", rootBeanDefinition);}@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {BeanDefinition userServiceBean = beanFactory.getBeanDefinition("userService");//修改该userService的属性name默认值为cyluserServiceBean.getPropertyValues().addPropertyValue("name", "cyl");BeanDefinition beanDefinition = beanFactory.getBeanDefinition("orderService");//修改该orderService的添加属性userServicebeanDefinition.getPropertyValues().add("userService", beanFactory.getBean(UserService.class));}}
最终输出结果
public static void main(String[] args) {// 创建一个Spring容器AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();context.register(AppConfig.class);context.refresh();OrderService orderService = context.getBean(OrderService.class);System.out.println(orderService.getUserService().getName());
}
spring提供的唯一BeanDefinitionRegistryPostProcessor
ConfigurationClassPostProcessor,这个类扫描所有component,service,import,configuration等注解,加载所有的bean定义到容器内。后续博文会详细介绍它的处理逻辑
总结
BeanFactoryProcessor是spring的一个拓展点,它的作用时机是在加载完所有的beanDefinition,还未实例化前。可以对beanDefinition进行加工,但是不可以加载beanDefinition。BeanDefinitionRegistryPostProcessor是BeanFactoryProcessor的子接口,增强了功能,既可以对beanDefinition加工,还可以加载自定义的beanDefinition。