zoukankan      html  css  js  c++  java
  • spring中的BeanFactoryPostProcessor

    spring中的BeanFactoryPostProcessor和BeanPostProcessor有些像,BeanPostProcessor是在bean的初始化前后进行一些操作,

    BeanFactoryPostProcessor是在所有的bean定义信息已经加载但还没有实例化时,执行方法postProcessBeanFactory()

    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 的实现类MyBeanFactoryPostProcessor 

    
    
    @Component

    public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
            System.out.println("MyBeanFactoryPostProcessor >>>postProcessBeanFactory   ");
          //获取已经加载的bean数
    int beanDefinitionCount = beanFactory.getBeanDefinitionCount();
          //获取已经加载的bean名称 String[] beanDefinitionNames
    = beanFactory.getBeanDefinitionNames(); System.out.println("beanFactory中的bean数>>>"+beanDefinitionCount); System.out.println(Arrays.asList(beanDefinitionNames)); } }

    配置类:

    @Configuration
    @Import({MyBeanFactoryPostProcessor.class})
    public class ExtConfig {
    
        @Bean
        public Foo foo(){
            return new Foo();
        }
    }

    Foo类:
    public class Foo {
    
        public Foo(){
            System.out.println("bean的创建");
        }
    
    
        @PostConstruct
        public void init(){
            System.out.println("bean的初始化");
        }
    
        @PreDestroy
        public void destroy(){
            System.out.println("bean的销毁");
        }
    }

    打印结果:

    MyBeanFactoryPostProcessor >>>postProcessBeanFactory   
    beanFactory中的bean数>>>8

    [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,

    org.springframework.context.annotation.internalAutowiredAnnotationProcessor,

    org.springframework.context.annotation.internalRequiredAnnotationProcessor,

    org.springframework.context.annotation.internalCommonAnnotationProcessor,

    org.springframework.context.event.internalEventListenerProcessor,

    org.springframework.context.event.internalEventListenerFactory,

    extConfig, com.springExt.MyBeanFactoryPostProcessor]

    bean的创建
    bean的初始化

    进行dubug也以看到:先执行BeanFactoryPostProcessors,后实例化非懒加载的bean

    // Allows post-processing of the bean factory in context subclasses.
                    postProcessBeanFactory(beanFactory);
    
                    // Invoke factory processors registered as beans in the context.
                    invokeBeanFactoryPostProcessors(beanFactory);
    
                    // Register bean processors that intercept bean creation.
                    registerBeanPostProcessors(beanFactory);
    
                    // Initialize message source for this context.
                    initMessageSource();
    
                    // Initialize event multicaster for this context.
                    initApplicationEventMulticaster();
    
                    // Initialize other special beans in specific context subclasses.
                    onRefresh();
    
                    // Check for listener beans and register them.
                    registerListeners();
    
                    // Instantiate all remaining (non-lazy-init) singletons.
                    finishBeanFactoryInitialization(beanFactory);
    
                    // Last step: publish corresponding event.
                    finishRefresh();
  • 相关阅读:
    树莓派交叉编译环境搭建
    手机购买怎样识别假货——一点心得体会分享!
    Ubuntu 网站服务器环境搭建
    转载:Raspberry Pi 树莓派入门
    Python中的条件选择和循环语句
    关于VMare中安装Ubuntu的一些说明
    如何去掉系统快捷方式的箭头和更改登录界面背景图片
    重装系统后,硬盘分区丢失的解决办法
    Python中的字符串
    Python的基础语法
  • 原文地址:https://www.cnblogs.com/tdyang/p/12063814.html
Copyright © 2011-2022 走看看