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. [HTML5] Add Semantic Styling to the Current Page of a Navigation Item with aria-current

    In this lesson, we are going to use aria-current to give a screen reader user more context about wha ...

  2. JQuery 实践--让页面动起来

    获取和设置元素特性特性属性:是指DOM元素中能够和HTML元素中某个特性对应得上的属性.通常JS特性属性的名称与对应的特性一一匹配,但class <=>className操作特性还是操作属 ...

  3. devstack cinder-volume服务状态为down

    cinder-manage service list 查看到有一个 xxx状态 Binary Host Zone Status State Updated At RPC Version Object ...

  4. win10 开启全局代理

    1. 打开设置 2. 点击“网络和Internet” 3.设置手动代理 . 设置完成后就可以愉快的玩耍啦

  5. Python学习日记(九)—— 模块二(logging、json&pickle、xml、requests、configparser、shutil、subprocess)

    logging模块 用于便捷记录日志且线程安全的模块(便捷的写文件的模块,不允许多个人同时操作文件) 1.单文件日志 import logging logging.basicConfig(filena ...

  6. oracle自定义排序和NULL值排序

    1.自定义顺序 当我们希望将某个查询结果指定的显示顺序展示的时候 order by case when column1=1 then 0 case when column1=1 then 1 else ...

  7. np.random.choices的使用

    在看莫烦python的RL源码时,他的DDPG记忆库Memory的实现是这样写的: class Memory(object): def __init__(self, capacity, dims): ...

  8. arcgis python获得别名

    import arcpy # Create a Describe object from the GDB table. # desc = arcpy.Describe(r"C:\Users\ ...

  9. 安装SQL server 提示重新启动计算机失败

    SQL Server2008是一款功能强大.实用性强的mysql数据库管理系统,因此很多用户都会在Win7系统中安装SQL Server2008,但是不少用户在安装过程中遇到问题,安装SQL Serv ...

  10. windows/Linux 下安装coreseek/sphinx

    2013年12月8日 17:26:26 注意的地方: 1.配置文件的 数据源, 索引, 服务 这3处配置的路径要写成windows识别的路径,最好是绝对路径 2.安装windows服务的时候,可以不带 ...