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();

spring中的BeanFactoryPostProcessor的更多相关文章

  1. Spring点滴十一:Spring中BeanFactoryPostProcessor和BeanPostProcessor区别

    Spring中BeanFactoryPostProcessor和BeanPostProcessor都是Spring初始化bean时对外暴露的扩展点.两个接口从名字看起来很相似,但是作用及使用场景却不同 ...

  2. Spring中文文档

    前一段时间翻译了Jetty的一部分文档,感觉对阅读英文没有大的提高(*^-^*),毕竟Jetty的受众面还是比较小的,而且翻译过程中发现Jetty的文档写的不是很好,所以呢翻译的兴趣慢慢就不大了,只能 ...

  3. spring中的BeanFactory与ApplicationContext的作用和区别?

    BeanFactory类关系继承图 1. BeanFactory类结构体系: BeanFactory接口及其子类定义了Spring IoC容器体系结构,由于BeanFactory体系非常的庞大和复杂, ...

  4. Spring中BeanPostProcessor

    Spring中BeanPostProcessor 前言: 本文旨在介绍Spring动态配置数据源的方式,即对一个DataSource的配置诸如jdbcUrl,user,password,driverC ...

  5. spring中context:property-placeholder/元素 转载

    spring中context:property-placeholder/元素  转载 1.有些参数在某些阶段中是常量 比如 :a.在开发阶段我们连接数据库时的连接url,username,passwo ...

  6. spring中基础核心接口总结

    spring中基础核心接口总结理解这几个接口,及其实现类就可以快速了解spring,具体的用法参考其他spring资料 1.BeanFactory最基础最核心的接口重要的实现类有:XmlBeanFac ...

  7. Spring中配置DataSource的六种方式

    第一种:beans.xml <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource ...

  8. 不使用SpringBoot如何将原生Feign集成到Spring中来简化http调用

    在微服务架构中,如果使用得是SpringCloud,那么只需要集成SpringFeign就可以了,SpringFeign可以很友好的帮我们进行服务请求,对象解析等工作. 然而SpingCloud是依赖 ...

  9. Spring中你可能不知道的事(二)

    在上一节中,我介绍了Spring中极为重要的BeanPostProcessor BeanFactoryPostProcessor Import ImportSelector,还介绍了一些其他的零碎知识 ...

随机推荐

  1. P5312 [Ynoi2011]D2T1

    思路:01trie 按位维护 提交:5边 错因:爆int + 少处理询问时的右端点 题解: 见代码(已经不想说什么了) 代码 //I have my own flg; #include<bits ...

  2. show([speed,[easing],[fn]])

    show([speed,[easing],[fn]]) 概述 显示隐藏的匹配元素. 这个就是 'show( speed, [callback] )' 无动画的版本.如果选择的元素是可见的,这个方法将不 ...

  3. 012_Linux驱动之_wait_event_interruptible

    1. 首先这篇博客讲解得挺好的,推荐 wait_event_interruptible 使用方法 2 .函数原型: #define wait_event_interruptible(wq, condi ...

  4. Java+web+上传文件夹

    用JAVA实现大文件上传及显示进度信息 ---解析HTTP MultiPart协议 (本文提供全部源码下载,请访问 https://github.com/1269085759/up6-jsp-mysq ...

  5. OI 常用模板 手写

    线性筛素数 (例题 洛谷P3383) bool p[50000010]; int cnt = 0; int prime[10000010]; inline void init() { int N = ...

  6. 解决python中文乱码的方法

    首先需要说明的是,windows下的文件路径,cmd窗口等默认编码都是gbk 但在windows下编写python程序的时候,我们一般采用的编码是utf-8 二者不一致是导致乱码的根本原因! 在pyc ...

  7. ORM SQLAlchemy - 建立一个关系 relationship

    relationship函数是sqlalchemy对关系之间提供的一种便利的调用方式, backref参数则对关系提供反向引用的声明 1 背景 如没有relationship,我们只能像下面这样调用关 ...

  8. MapReduce Combiner

    Combiner编程(可选步骤,视情况而定!) combiner最基本是实现本地key的归并,combiner具有类似本地的reduce功能. 如果不用combiner,那么所有的结果都是reduce ...

  9. Uncaught TypeError: l.push is not a function

    layui.use([ 'jquery', 'layer', 'element' ], function() {} 而不是 layui.use( 'jquery', 'layer', 'element ...

  10. 漫谈Objective-C在语法上的改进

    Objective-C 2.0从2006年正式发布至今已经有10年了.Apple在此期间也不断地为其注入新的语法特性,比如Blocks.NSNumber literal.NSArray literal ...