• (二)扩展原理:【1】BeanFactoryPostProcessor


    一、BeanFactoryPostProcessor 接口

      BeanFactoryPostProcessor:BeanFactory 的后置处理器

      作用
        在 BeanFactory 标准初始化之后调用,来定制和修改BeanFactory的内容;
        所有的 bean 定义信息已经保存加载到 beanFactory,但是 bean 的实例还未创建;

    二、案例

      1、自定义 BeanFactoryPostProcessor 实现类:

    @Component
    public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    
        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
            System.out.println("MyBeanFactoryPostProcessor...postProcessBeanFactory...");
            int count = beanFactory.getBeanDefinitionCount();
            String[] names = beanFactory.getBeanDefinitionNames();
            System.out.println("当前BeanFactory中有"+count+" 个Bean");
            System.out.println(Arrays.asList(names));
        }
    }

      2、创建任意的 Java 类

    public class Blue {
    
        public Blue() {
            System.out.println("Blue constructor");
        }
    }

      3、创建配置类并加入组件

    @ComponentScan(value = {"com.njf.ext", "com.njf.bean"})
    @Configuration
    public class ExtConfig {
    
        @Bean
        public Blue Blue() {
            return new Blue();
        }
    }

      4、测试

        @Test
        public void test01() {
            AnnotationConfigApplicationContext ioc = new AnnotationConfigApplicationContext(ExtConfig.class);
    
            System.out.println("IOC容器创建完毕!");
    
            ioc.close();
        }

      运行结果:

    三、Debug

    四、小结

      BeanFactoryPostProcessor:BeanFactory 的后置处理器

      作用
        在 BeanFactory 标准初始化之后调用,来定制和修改BeanFactory的内容;
        所有的 bean 定义信息已经保存加载到 beanFactory,但是 bean 的实例还未创建

      原理:
        1、ioc容器创建对象
        2、invokeBeanFactoryPostProcessors(beanFactory);
          如何找到所有的 BeanFactoryPostProcessor 并执行他们的方法;
          ① 直接在 BeanFactory 中找到所有类型是 BeanFactoryPostProcessor 的组件,并执行他们的方法;
          ② 在初始化创建其他组件前面执行;


      类似:BeanPostProcessor:bean后置处理器,bean创建对象初始化前后进行拦截工作的

  • 相关阅读:
    Azure HPC Pack Cluster添加辅助节点
    Azure HPC Pack 辅助节点模板配置
    Azure HPC Pack配置管理系列(PART6)
    Windows HPC Pack 2012 R2配置
    Azure HPC Pack 节点提升成域控制器
    Azure HPC Pack VM 节点创建和配置
    Azure HPC Pack 部署必要条件准备
    Azure HPC Pack 基础拓扑概述
    Azure VM 性能计数器配置
    Maven私仓配置
  • 原文地址:https://www.cnblogs.com/niujifei/p/15560025.html
Copyright © 2020-2023  润新知