Spring中父子容器的实现实例

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();

	}
}

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

posted on 2019-01-31 14:58  有没有成功  阅读(1290)  评论(0编辑  收藏  举报

导航