zoukankan      html  css  js  c++  java
  • spring-BeanFactoryPostProcessor

    BeanFactoryPostProcessor 也是spring的一个扩展点,可以插手bean实例化的过程。
    示例:
    package com.test3;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @ComponentScan("com.test3")
    public class AppConfig {
    }
    package com.test3;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class Bean2 {
    }
    package com.test3;
    
    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 MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
        public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
          BeanDefinition beanDefinition =  configurableListableBeanFactory.getBeanDefinition("bean2");
           beanDefinition.setScope("prototype");
        }
    }
    package com.test3;
    
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    public class MyTest {
    
        public static void main(String[] args) {
            AnnotationConfigApplicationContext context =
                    new AnnotationConfigApplicationContext(AppConfig.class);
            System.out.println(context.getBean(Bean2.class));
            System.out.println(context.getBean(Bean2.class));
        }
    }

    执行结果:

    com.test3.Bean2@80ec1f8
    com.test3.Bean2@1445d7f
  • 相关阅读:
    Building a RESTful Web Service
    Proxy setting
    同步机制 note
    C++: Virtual Table and Shared Memory
    2018中国大学生程序设计竞赛
    2018 MUltiU 9 dp / 8 upper_bound ; 构造?/
    2018/8/10 部分枚举(类似尺取)
    2018/8/9 MultiU 6 并查集+dfs,反向建边提高查询效率 !!! / 最大字段和n维(降维)/ 状压+中途相遇法
    2018/7/29 cf 499 div 2(1011)
    2018/7/28 欧拉路径
  • 原文地址:https://www.cnblogs.com/yintingting/p/6231910.html
Copyright © 2011-2022 走看看