当前位置:   article > 正文

Spring之BeanFactoryPostProcessor详解

Spring之BeanFactoryPostProcessor详解

目录

功能与作用

使用案例

spring提供的常见BeanFactoryPostProcessor

1.EventListenerMethodProcessor

2.BeanDefinitionRegistryPostProcessor

功能与作用

使用案例

spring提供的唯一BeanDefinitionRegistryPostProcessor

总结


功能与作用

参考BeanFactoryPostProcessor方法注释可知:该方法是在所有bean被加载,未被实例化前执行。对所有bean的属性可以修改也可以添加

  1. @FunctionalInterface
  2. public interface BeanFactoryPostProcessor {
  3. /**
  4. * Modify the application context's internal bean factory after its standard
  5. * initialization. All bean
  6. definitions will have been loaded, but no beans
  7. * will have been instantiated yet. This allows for overriding or adding
  8. * properties even to eager-initializing beans.
  9. * @param beanFactory the bean factory used by the application context
  10. * @throws org.springframework.beans.BeansException in case of errors
  11. */
  12. void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
  13. }

使用案例

用户可以自定义一个实现BeanFactoryPostProcessor的类,获取某个bean修改bean的属性,比如如下举例修改该UserService类的属性由单例改为原型

  1. @Component
  2. public class UserService {
  3. private String name;
  4. public String getName() {
  5. return name;
  6. }
  7. public void setName(String name) {
  8. this.name = name;
  9. }
  10. }
  11. package com.cyl.beanFactoryDemo;
  12. import org.springframework.beans.BeansException;
  13. import org.springframework.beans.factory.config.BeanDefinition;
  14. import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
  15. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
  16. import org.springframework.stereotype.Component;
  17. @Component
  18. public class CylBeanFactoryBeanPostProcessor implements BeanFactoryPostProcessor {
  19. @Override
  20. public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
  21. BeanDefinition beanDefinition = beanFactory.getBeanDefinition("userService");
  22. //修改该bean的属性为原型
  23. beanDefinition.setScope("prototype");
  24. //添加该bean的属性name默认值为cyl
  25. MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
  26. propertyValues.add("name","cyl");
  27. }
  28. }

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

  1. public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {
  2. /**
  3. * Modify the application context's internal bean definition registry after its
  4. * standard initialization. All regular bean definitions will have been loaded,
  5. * but no beans will have been instantiated yet. This allows for adding further
  6. * bean definitions before the next post-processing phase kicks in.
  7. * @param registry the bean definition registry used by the application context
  8. * @throws org.springframework.beans.BeansException in case of errors
  9. */
  10. void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;
  11. }

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属性

  1. package com.cyl.beanFactoryDemo;
  2. public class OrderService {
  3. private UserService userService;
  4. public UserService getUserService() {
  5. return userService;
  6. }
  7. public void setUserService(UserService userService) {
  8. this.userService = userService;
  9. }
  10. }

2.创建CylBeanRegistryPostProcessor实现了BeanDefinitionRegistryPostProcessor,在postProcessBeanDefinitionRegistry方法内注册OrderService类到容器中,在postProcessBeanFactory中给OrderService添加属性userService

  1. package com.cyl.beanFactoryDemo;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.beans.factory.config.BeanDefinition;
  4. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
  5. import org.springframework.beans.factory.support.BeanDefinitionRegistry;
  6. import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
  7. import org.springframework.beans.factory.support.RootBeanDefinition;
  8. import org.springframework.stereotype.Component;
  9. @Component
  10. public class CylBeanRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
  11. @Override
  12. public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
  13. RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();
  14. rootBeanDefinition.setBeanClass(OrderService.class);
  15. registry.registerBeanDefinition("orderService", rootBeanDefinition);
  16. }
  17. @Override
  18. public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
  19. BeanDefinition userServiceBean = beanFactory.getBeanDefinition("userService");
  20. //修改该userService的属性name默认值为cyl
  21. userServiceBean.getPropertyValues().addPropertyValue("name", "cyl");
  22. BeanDefinition beanDefinition = beanFactory.getBeanDefinition("orderService");
  23. //修改该orderService的添加属性userService
  24. beanDefinition.getPropertyValues().add("userService", beanFactory.getBean(UserService.class));
  25. }
  26. }

最终输出结果

  1. public static void main(String[] args) {
  2. // 创建一个Spring容器
  3. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
  4. context.register(AppConfig.class);
  5. context.refresh();
  6. OrderService orderService = context.getBean(OrderService.class);
  7. System.out.println(orderService.getUserService().getName());
  8. }

spring提供的唯一BeanDefinitionRegistryPostProcessor

ConfigurationClassPostProcessor,这个类扫描所有component,service,import,configuration等注解,加载所有的bean定义到容器内。后续博文会详细介绍它的处理逻辑

总结

    BeanFactoryProcessor是spring的一个拓展点,它的作用时机是在加载完所有的beanDefinition,还未实例化前。可以对beanDefinition进行加工,但是不可以加载beanDefinition。BeanDefinitionRegistryPostProcessor是BeanFactoryProcessor的子接口,增强了功能,既可以对beanDefinition加工,还可以加载自定义的beanDefinition。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/374002
推荐阅读
相关标签
  

闽ICP备14008679号