标签:容器 BeanFactory beanFactory spring02 Bean bean 处理器
容器的实现
DefaultListableBeanFactory :BeanFactory 最重要的实现,控制反转,依赖注入都由它实现 ,ApplicationContext 组合了它
beanFactory 可以通过 registerBeanDefinition 注册一个 bean definition 对象
我们平时使用的配置类、xml、组件扫描等方式都是生成 bean definition 对象注册到 beanFactory 当中
bean definition 描述了这个 bean 的创建蓝图:scope 是什么、用构造还是工厂创建、初始化销毁方法是什么,等等
代码:
点击查看代码
//获得 BeanFactory 对象 控制反转,依赖注入功能
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
//获得 BeanDefinition 对象,定义 Bean 的信息,BeanFactory 根据这些信息创建 Bean
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder
.genericBeanDefinition(Config.class) //设置哪个类的 Bean 信息
.setScope("singleton") //设置 Bean 的类型
.getBeanDefinition(); //得到 BeanDefinition
//将 BeanDefinition 注册到 BeanFactory 中
beanFactory.registerBeanDefinition("config",beanDefinition); //第一个参数 Bean 的名字
//遍历 BeanFactory 中的 BeanDefinition 可以看到其中的注解并没有被解析,说明 BeanFactory 没有这个功能,需要添加 BeanFactory 后处理器
for (String name : beanFactory.getBeanDefinitionNames()) {
System.out.println(name);
}
beanFactory 需要手动调用 beanFactory 后处理器对它做增强
例如通过解析 @Bean、@ComponentScan 等注解,来补充一些 bean definition
代码:
点击查看代码
//通过注解工具类给 BeanFactory 注册一些常见的后处理器(不全是 BeanFactory 后处理器)此时并没有与 BeanFactory 建立联系
/*
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
*/
AnnotationConfigUtils.registerAnnotationConfigProcessors(beanFactory);
for (String name : beanFactory.getBeanDefinitionNames()) {
System.out.println(name);
}
System.out.println("==================================");
//添加解析 @Configuration 与 @Bean 注解的 BeanFactoryPostProcess (都继承了 BeanFactoryPostProcess)
// org.springframework.context.annotation.internalConfigurationAnnotationProcessor
beanFactory.getBeansOfType(BeanFactoryPostProcessor.class)
.values().forEach(b ->{
//执行后处理器(与 BeanFactory 建立联系)
b.postProcessBeanFactory(beanFactory);
});
for (String name : beanFactory.getBeanDefinitionNames()) {
System.out.println(name);
}
beanFactory 需要手动添加 bean 后处理器,以便对后续 bean 的创建过程提供增强
例如 @Autowired,@Resource 等注解的解析都是 bean 后处理器完成的
代码:
点击查看代码
//添加解析 @Autowired @Resource 等注解的 Bean 后处理器 (针对 bean 的生命周期的各个阶段提供扩展)
beanFactory.getBeansOfType(BeanPostProcessor.class)
.values().forEach(beanFactory::addBeanPostProcessor);
beanFactory.preInstantiateSingletons(); // 准备好所有单例 初始化
bean 后处理的添加顺序会对解析结果有影响,见视频中同时加 @Autowired,@Resource 的例子
beanFactory 需要手动调用方法来初始化单例
beanFactory 需要额外设置才能解析 ${} 与 #{}
ApplicationContext相关的实现
ClassPathXmlApplicationContext:从类路径查找 xml 配置文件创建容器
FileSystemXmlApplicationContext:从磁盘路径查找 xml 配置文件创建容器
XmlWebApplicationContext:传统 SSM 整合时,基于 xml 配置文件的容器
AnnotationConfigWebApplicationContext:传统 SSM 整合时,基于 java 配置类的容器
AnnotationConfigApplicationContext:SpringBoot 中非 Web 环境的容器(新)
AnnotationConfigServletwebServerApplicationContext:SpringBoot 中servlet web 环境容器(新)
AnnotationConfigReactiveWebServerApplicationContext:SpringBoot 中 reactive web(Spring WebFlux,基于响应式的 Web 容器) 环境容器(新)
标签:容器,BeanFactory,beanFactory,spring02,Bean,bean,处理器 来源: https://www.cnblogs.com/xy7112/p/16371129.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。