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. 题解[NOIP2017] 列队

    题解[NOIP2017] 列队 题面 解析 看到这题时感觉这个编号很难维护啊? 后来看了lzf大佬的题解才会.. 首先,考虑一个稍微暴力的做法, 维护每一行的前\(m-1\)个人和最后一列的\(n\) ...

  2. HDU 6129 - Just do it | 2017 Multi-University Training Contest 7

    比赛时脑子一直想着按位卷积... 按题解的思路: /* HDU 6129 - Just do it [ 规律,组合数 ] | 2017 Multi-University Training Contes ...

  3. Dynamic Data linq to SQL Web Application

    微软提供了一个数据驱动网站模板,可以自动生成CRUD页面,使用过程中碰到些问题 1.首先是如何应用,只需要创建个context并且在Global.asax里面加入下面这一句就可以了 DefaultMo ...

  4. [Luogu] 最大收益

    题面:https://www.luogu.org/problemnew/show/P2647 题解:https://www.zybuluo.com/wsndy-xx/note/1142685

  5. form 表单提交数据和文件(fromdata的使用方法)

    <!-- 数据和文件一次性提交 --> <form class="form_meren" id="mainForm" name="m ...

  6. gitlab配置邮箱postfix(新用户激活邮件)

    亲测可用 https://www.cnblogs.com/yoyoketang/p/10287345.html

  7. 【java中的static关键字】

    文章转自:https://www.cnblogs.com/dolphin0520/p/3799052.html 一.static关键字的用途 在<Java编程思想>P86页有这样一段话: ...

  8. PostgreSQL SELECT INTO和INSERT INTO SELECT 两种表复制语句

    SELECT INTO和INSERT INTO SELECT两种表复制语句都可以用来复制表与表之间的数据,但是它们之间也有区别. 建表语句: bas_custom_rel表 CREATE TABLE ...

  9. Java-ZipUtil

    Zip 压缩工具类,不支持压缩空文件夹. 简单版 import java.io.FileOutputStream; import java.io.IOException; import java.ni ...

  10. 移动端—— 兼容PC端,移动端的点击事件

    移动设备上不支持鼠标事件,好在webkit内核的移动浏览器支持 touch 事件,所以触摸事件是移动应用中所必须的.touchstart.touchmove.touchend事件可以类比于moused ...