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();
  • 相关阅读:
    上传文件是常要处理的事情,使用ajaxFileUpload.js处理比较方便,这里的ajaxFileUpload.js文件修改过的,
    文件上传控件bootstrap-fileinput的使用
    常用开发中使用到的作图工具(开发向)
    mybatis-generator + mysql/ptsql
    表单嵌套问题的解决方法
    C++和QML混合的QT程序调试方法
    windows下,Qt Creator 中javascript调试器安装并使用
    Qt浮动按钮的实现(使用窗口背景透明、实现只显示浮动按钮的目的)
    不能继承于QObject的类就一定不能使用信号槽?(用一个代理类进行发射就行了)
    关于SetLength报Out of memory的研究及解决办法
  • 原文地址:https://www.cnblogs.com/tdyang/p/12063814.html
Copyright © 2011-2022 走看看