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. [Google Guava] 3-缓存

    原文地址  译文地址    译者:许巧辉  校对:沈义扬 范例 01 LoadingCache<Key, Graph> graphs = CacheBuilder.newBuilder() ...

  2. FWT 等总结 题解

    目录 与卷积: 代码: 或卷积: 代码: 异或卷积: 代码: FST:子集卷积 代码: 例题: CF914G 代码: uoj310[UNR #2]黎明前的巧克力 代码: CF662C Binary T ...

  3. 007_STM32程序移植之_多通道ADC转换

    1. 测试环境:STM32C8T6 2. 测试:使用DMA进行多通道ADC转换 3. 描述:用 ADC 连续采集 12 路模拟信号,并由 DMA 传输到内存.ADC 配置为扫描 并且连续转换模式,AD ...

  4. Linux中查看和修改分区的uuid方便挂载使用

    查看硬盘UUID: 两种方法: ls -l /dev/disk/by-uuid blkid /dev/sda1 修改分区UUID: 1.修改分区的UUID Ubuntu 使用 uuid命令 生成新的u ...

  5. 爬虫之解析库Xpath

    简介 XPath即为XML路径语言(XML Path Language),它是一种用来确定XML文档中某部分位置的语言. XPath基于XML的树状结构,提供在数据结构树中找寻节点的能力.起初XPat ...

  6. eclipse将web项目部署到tomcat

    在 eclipse 中,选择 Window--->Preferences--->Server--->Runtime Environments,选择 Add 按钮 在弹出的对话框中,选 ...

  7. Go语言 之TCP文件传输

    服务端实现流程大致如下: 创建监听listener,程序结束时关闭. 阻塞等待客户端连接,程序结束时关闭conn. 读取客户端发送文件名.保存fileName. 回发“ok”给客户端做应答 封装函数 ...

  8. spring事务配置异常

    spring事务配置不回滚spring事务管理配置,一般来说都是可以回滚的,最近在开发的过程中遇到了一个异常不回滚的问题,最终找到了原因,贴出来一下 1.首先这里定义一个接口 在接口中定义几个方法 2 ...

  9. 小程序开发:用原生还是选框架(wepy/mpvue/uni-app/taro)?

    小程序开发:用原生还是选框架(wepy/mpvue/uni-app/taro)? 自 2017-1-9微信小程序诞生以来,历经2年多的迭代升级,已有数百万小程序上线,成为继Web.iOS.Androi ...

  10. 走进JavaWeb技术世界2:JSP与Servlet的曾经与现在

    转载自:码农翻身 转自: 刘欣 码农翻身 1周前 我是Servlet, 由于很多框架把我深深地隐藏了起来,我变得似乎无关紧要了,很多人也选择性的把我给遗忘了. 其实,我还活得好好的呢, 只不过是从前台 ...