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();
spring中的BeanFactoryPostProcessor的更多相关文章
- Spring点滴十一:Spring中BeanFactoryPostProcessor和BeanPostProcessor区别
Spring中BeanFactoryPostProcessor和BeanPostProcessor都是Spring初始化bean时对外暴露的扩展点.两个接口从名字看起来很相似,但是作用及使用场景却不同 ...
- Spring中文文档
前一段时间翻译了Jetty的一部分文档,感觉对阅读英文没有大的提高(*^-^*),毕竟Jetty的受众面还是比较小的,而且翻译过程中发现Jetty的文档写的不是很好,所以呢翻译的兴趣慢慢就不大了,只能 ...
- spring中的BeanFactory与ApplicationContext的作用和区别?
BeanFactory类关系继承图 1. BeanFactory类结构体系: BeanFactory接口及其子类定义了Spring IoC容器体系结构,由于BeanFactory体系非常的庞大和复杂, ...
- Spring中BeanPostProcessor
Spring中BeanPostProcessor 前言: 本文旨在介绍Spring动态配置数据源的方式,即对一个DataSource的配置诸如jdbcUrl,user,password,driverC ...
- spring中context:property-placeholder/元素 转载
spring中context:property-placeholder/元素 转载 1.有些参数在某些阶段中是常量 比如 :a.在开发阶段我们连接数据库时的连接url,username,passwo ...
- spring中基础核心接口总结
spring中基础核心接口总结理解这几个接口,及其实现类就可以快速了解spring,具体的用法参考其他spring资料 1.BeanFactory最基础最核心的接口重要的实现类有:XmlBeanFac ...
- Spring中配置DataSource的六种方式
第一种:beans.xml <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource ...
- 不使用SpringBoot如何将原生Feign集成到Spring中来简化http调用
在微服务架构中,如果使用得是SpringCloud,那么只需要集成SpringFeign就可以了,SpringFeign可以很友好的帮我们进行服务请求,对象解析等工作. 然而SpingCloud是依赖 ...
- Spring中你可能不知道的事(二)
在上一节中,我介绍了Spring中极为重要的BeanPostProcessor BeanFactoryPostProcessor Import ImportSelector,还介绍了一些其他的零碎知识 ...
随机推荐
- harbor1.9仓库同步迁移
harbor 1.9 实战的仓库迁移,过程实际上就是从A push 到B.16个tag 不到100G,挺快的 1分钟多. 假设我们从A迁移到B. 1.先在A上面建立一个目标仓库.
- 浏览器顶部设置margin-top时存在的bug
浏览器bug<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8 ...
- MySQL 5.7 GTID OOM bug案例分析 --大量压测后主从不同步
转载自:http://www.sohu.com/a/231766385_487483 MySQL 5.7是十年内最为经典的版本,这个观点区区已经表示过很多次.然而,经典也是由不断地迭代所打造的传奇.5 ...
- nodejs中http服务器,如何使用GET,POST请求发送数据、npm、以及一些插件的介绍
浏览器给服务器传递参数,最常用的是地址栏传参(get),以及表单提交(post) 先说get传参,就是在url后跟上?key=value&key2=value2...... 但是按照前几篇的h ...
- el-form 表单校验
<el-form ref="dataForm" :model="dataForm" :rules="rules" label-widt ...
- shell 统计行数,单词个数,字符个数
如果我们想知道1.txt中有多少行,多少个单词,多少个字符.我们可以使用wc命令.选项与参数-l:今列出行-w:今列出多少字(英文单词)-m:多少字符[zhang@localhost ~]$ cat ...
- CF1208A
CF1208A 题意: 就是把斐波那契数列的+改成异或,求第n项的值. 解法: 又是一个人类智慧题,打表找规律. 可以发现答案在 $ a,b,a⊕b $ 三个数中循环 CODE: #include&l ...
- instr和like的使用区别
1.instr函数 instr函数是一个字符串处理函数,它在Oracle/PLSQL中是返回子字符串在源字符串中的位置,如果在源串中没有找到子串,则返回0. instr函数定义如下: /* * 返回子 ...
- Docker安装mysql5.6
1.docker hub 上查找mysql镜像 2.在docker hub上(阿里云加速器)拉取mysql镜像到本地标签为5.6 3.使用mysql5.6创建容器(也叫运行镜像) 4.交互运行,进入m ...
- Hadoop环境搭建|第二篇:hadoop环境搭建
硬件配置:1台NameNode节点.2台DataNode节点 一.Linux环境配置 这里我只配置NameNode节点,DataNode节点的操作相同. 1.1.修改主机名 命令:vi /etc/sy ...