Spring中父子容器的实现实例
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
Spring中父子容器的实现实例Spring的父子容器可以通过ConfigurableApplicationContext或ConfigurableBeanFactory来实现,这两个接口中分别有setParent及setParentBeanFactory方法,可以与当前的子容器进行父子容器关联,这个时候子容器就可以引用父容器中的bean,但是父容器是不能够引用子容器中的bean的,并且各个子容器中定义的bean是互不可见的,这样也可以避免因为不同的插件定义了相同的bean而带来的麻烦。应用场景包括插件或组件的接入,只需要对方提供JAR即可,由父容器进行引导,各个子容器再完成自己的应该完成的工作即可。
以下是一个通过ConfigurableApplicationContext来实现的实例。
1、父容器需要的几个文件:
1)、Runner.java
//这个类负责启动父容器public class Runner { public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:/spring1.xml"); }}
2)、ParentClass 这个类主要用于测试子容器是否可以获取父容器中定义的bean
public class ParentClass{ public void print(){ System.out.println("This is parent class."); }}
3)、PluginLoader 这个类用于对子容器的加载,建立父子容器关系
public class PluginLoader implements ApplicationContextAware { ApplicationContext parentApplicationContext; ConfigurableApplicationContext childContext; public void load() { //扫描所有classpath下面的以plugin_开头spring的配置文件进行装配,这里约定所有的子容器插件都必须有一个以plugin_开头的配置文件,并通过这个文件被父容器加载 childContext = new ClassPathXmlApplicationContext("classpath*:/plugin_*.xml"); childContext.setParent(parentApplicationContext); childContext.refresh(); } public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.parentApplicationContext = applicationContext; }}
4)、spring1.xml
<bean id="parentClass" class="com.test1.ParentClass"></bean><bean id="pluginLoader" class="com.test1.PluginLoader" init-method="load"></bean>
简单的父容器就只需要这么几个类了。
2、子容器1需要的几个文件
我准备了两个子容器,并加了互相测试调用的的测试,看子容器是否可以引用另外一个子窗口中的bean,不过因为两个子容器的实现完全相同,只是由1改成2就可以了,以下就只贴出子容器1需要的代码,子容器2的代码类似。
1)、plugin_1.xml 这个类是约定好必须存在的,由容器进行引导
<pre name="code" class="html"><bean id="childContext1" class="com.test1.child.ChildContext1"></bean>
2)、ChildContext1.java 加载自己容器中所有的配置,并与父容器建立父子容器关系
public class ChildContext1 implements ApplicationContextAware { ApplicationContext parentApplicationContext; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { //父容器中没有建立父子容器关系之前,是获取不到parent的,只有父容器执行refresh方法后,第二次初使化子容器才会获取得到 //也就是第一次的初使化就不执行了,等父容器中建立好父子容器关系后再进行初使化,因为子容器需要引用父容器中的parentClass if(applicationContext.getParent()==null){ return; } //Get parent application context this.parentApplicationContext = applicationContext.getParent(); ConfigurableApplicationContext childContext = new ClassPathXmlApplicationContext("classpath:/child1.xml"); childContext.setParent(this.parentApplicationContext); childContext.refresh(); ParentClass parentClass = childContext.getBean(ParentClass.class); Assert.assertNotNull(parentClass); }}
3)、child1.xml
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"> <bean id="childClass1" class="com.test1.child.ChildClass1">
4)、ChildClass1
public class ChildClass1 implements InitializingBean { //这里required加上false,是因为是没有建立父子容器关系之前,这个parentClass是注入不了了的 @Autowired(required = false) ParentClass parentClass; //这里required加上false,是因为子容器之前是不能够相互引用的,只是测试使用。另注:这个类没有放到这里,在子容器2中,这里不贴代码了 @Autowired(required = false) ChildClass2 childClass2; public void print() { if (parentClass != null) { parentClass.print(); } System.out.println("This is child class 1"); if (childClass2 != null) { childClass2.print(); } } public void afterPropertiesSet() throws Exception { print(); }}
给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
Spring中父子容器的实现实例相关推荐
- boot spring 没有父子容器_Spring 系列(二):Spring MVC的父子容器
1.背景 在使用Spring MVC时候大部分同学都会定义两个配置文件,一个是Spring的配置文件spring.xml,另一个是Spring MVC的配置文件spring-mvc.xml. 在这里给 ...
- spring的父子容器
在创建ssm项目工程时,经常需要读取properties资源配置文件,传统的方法当然可以. 但是spring提供了更简便的方法,@value注解. 在page.properties文件中,配置分页信息 ...
- java图片填充父容器_java相关:spring的父子容器及配置详解
java相关:spring的父子容器及配置详解 发布于 2020-5-26| 复制链接 本篇文章主要介绍了spring的父子容器及配置详解,详细的介绍了spring父子容器的概念.使用场景和用法,有兴 ...
- java手工注入bean_java相关:Spring中如何动态注入Bean实例教程
java相关:Spring中如何动态注入Bean实例教程 发布于 2020-3-8| 复制链接 摘记: 前言在Spring中提供了非常多的方式注入实例,但是由于在初始化顺序的不同,基于标注的注入方式, ...
- boot spring 没有父子容器_Spring父子容器问题
这个问题老早就存在了,只是今天组长让我看AOP不生效的时候,才真实遇到这个问题,之前都是用的Spring Boot开发,不会存在这个问题. 问题描述 如果使用传统的方式来开发Spring项目,要部署在 ...
- Spring中的容器
1.Spring容器 Spring容器最基本的接口就是BeanFactory, 负责配置,创建和管理bean.我们通常不直接使用BeanFactory接口,而是使用其子接口ApplicationCon ...
- boot spring 没有父子容器_理解 MyBatis 是如何在 Spring 容器中初始化的
MyBatis 初始化过程就是 生成一些必须的对象放到 Spring 容器中 .问题是这个过程到底生成了哪些对象?当遇到 MyBatis 初始化失败时,如何正确的找到分析问题的切入点?本文将针对这些问 ...
- Spring中IOC容器
IOC入门案例思路分析 1.管理什么(Service和Dao) 2.如何将管理的对象存放到IOC容器(配置applicationContext.xml)第二步 3.将管理的对象存放到IOC容器,如何获 ...
- 关于Spring中IOC容器和AOP协同工作的原理理解
1.在详细介绍 IOC 容器的工作原理前,这里先介绍一下实现 IOC 所用到的一些辅助类,包括BeanDefinition.BeanReference.PropertyValues.PropertyV ...
最新文章
- java中velocity定义宏标签_velocity自定义标签和指令(转:zwj)
- JDeveloper开发环境设置
- php header下载中文名称,PHP Header下载文件在IE文件名中文乱码问题
- 一文深入理解协同过滤
- iOS快速开发框架Bee-Framework应用和解析(三) - Message, Model, Signal
- 【开源工程】之YUVPlayer1.6
- 算法应用-百钱买百鸡
- 使用阿里云镜像仓库构建国外 Docker 镜像
- EditPlus for python
- 三、Serializer序列化器
- 主板24pin接口详图_特殊装机:24pin主板用20pin的供电
- axure如何导出原件_axure怎么导出为pdf格式
- 中医大2020年7月网考计算机应用基础,2020年7月网络教育统考《计算机应用基础》操作系统应用模拟题试卷2...
- 自己搭建安卓app结合苹果CMS后台视频管理
- VAX v10.9.2062.0-52pj vDie 分析 【转载请注明出处】
- win10 android 共享文件,手把手教您win10系统通过局域网共享文件的解决方案
- linux vim vi 区别,Linux下 vi vim vim-gnome vim-tiny vim-gtk vim-nox的区别
- 虚拟机不可恢复错误-vcpu0的解决办法
- Fail Fast与Fail Safe的区别
- pythoncad_Pycad: Python Extension for AutoCad
热门文章
- 十八般武艺玩转GaussDB(DWS)性能调优(二):坏味道SQL识别
- 开源数据库这么香,为什么我们还要下功夫自研?
- 多值类别特征加入CTR预估模型的方法
- 深入比特币原理(五)——高级交易与脚本
- 【云速建站】页面产品维护简述
- 华为云服务器实战 之 Gitlab安装与配置使用
- mysql dba命令_mysql DBA:mysqladmin常用命令总结
- obj模型 vue_uni-app npm 包手机端运行报错(vue-3d-model)
- Java jdk的安装 与 环境变量的配置
- Android笔记 Android艺术探索笔记 Pacel VS Serialize Demo