Spring中父子容器的实现实例

来源:互联网 发布:欧姆龙plc编程语言 编辑:程序博客网 时间:2024/05/17 03:28

Spring中父子容器的实现实例Spring的父子容器可以通过ConfigurableApplicationContext或ConfigurableBeanFactory来实现,这两个接口中分别有setParent及setParentBeanFactory方法,可以与当前的子容器进行父子容器关联,这个时候子容器就可以引用父容器中的bean,但是父容器是不能够引用子容器中的bean的,并且各个子容器中定义的bean是互不可见的,这样也可以避免因为不同的插件定义了相同的bean而带来的麻烦。应用场景包括插件或组件的接入,只需要对方提供JAR即可,由父容器进行引导,各个子容器再完成自己的应该完成的工作即可。

以下是一个通过ConfigurableApplicationContext来实现的实例。

1、父容器需要的几个文件:

1)、Runner.java

[java] view plain copy
  1. //这个类负责启动父容器  
  2. public class Runner {  
  3.     public static void main(String[] args){  
  4.         ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:/spring1.xml");  
  5.     }  
  6. }  
2)、ParentClass 这个类主要用于测试子容器是否可以获取父容器中定义的bean
[java] view plain copy
  1. public class ParentClass{  
  2.     public void print(){  
  3.         System.out.println("This is parent class.");  
  4.     }  
  5. }  
3)、PluginLoader 这个类用于对子容器的加载,建立父子容器关系
[java] view plain copy
  1. public class PluginLoader implements ApplicationContextAware {  
  2.     ApplicationContext parentApplicationContext;  
  3.     ConfigurableApplicationContext childContext;  
  4.   
  5.     public void load() {  
  6.       //扫描所有classpath下面的以plugin_开头spring的配置文件进行装配,这里约定所有的子容器插件都必须有一个以plugin_开头的配置文件,并通过这个文件被父容器加载  
  7.         childContext = new ClassPathXmlApplicationContext("classpath*:/plugin_*.xml");  
  8.         childContext.setParent(parentApplicationContext);  
  9.         childContext.refresh();  
  10.     }  
  11.   
  12.     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {  
  13.         this.parentApplicationContext = applicationContext;  
  14.     }  
  15. }  

4)、spring1.xml

[html] view plain copy
  1. <bean id="parentClass" class="com.test1.ParentClass"></bean>  
  2. <bean id="pluginLoader" class="com.test1.PluginLoader" init-method="load"></bean>  


简单的父容器就只需要这么几个类了。

2、子容器1需要的几个文件

我准备了两个子容器,并加了互相测试调用的的测试,看子容器是否可以引用另外一个子窗口中的bean,不过因为两个子容器的实现完全相同,只是由1改成2就可以了,以下就只贴出子容器1需要的代码,子容器2的代码类似。

1)、plugin_1.xml 这个类是约定好必须存在的,由容器进行引导

[html] view plain copy
  1. <pre name="code" class="html"><bean id="childContext1" class="com.test1.child.ChildContext1"></bean>  

2)、ChildContext1.java 加载自己容器中所有的配置,并与父容器建立父子容器关系
[java] view plain copy
  1. public class ChildContext1 implements ApplicationContextAware {  
  2.       
  3.     ApplicationContext parentApplicationContext;  
  4.   
  5.     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {  
  6.       //父容器中没有建立父子容器关系之前,是获取不到parent的,只有父容器执行refresh方法后,第二次初使化子容器才会获取得到  
  7.       //也就是第一次的初使化就不执行了,等父容器中建立好父子容器关系后再进行初使化,因为子容器需要引用父容器中的parentClass  
  8.         if(applicationContext.getParent()==null){  
  9.             return;  
  10.         }  
  11.         //Get parent application context  
  12.         this.parentApplicationContext = applicationContext.getParent();  
  13.   
  14.         ConfigurableApplicationContext  childContext = new ClassPathXmlApplicationContext("classpath:/child1.xml");  
  15.         childContext.setParent(this.parentApplicationContext);  
  16.         childContext.refresh();  
  17.         ParentClass parentClass = childContext.getBean(ParentClass.class);  
  18.         Assert.assertNotNull(parentClass);  
  19.     }  
  20.   
  21. }  

3)、child1.xml

[html] view plain copy
  1. <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">   
  2. <bean id="childClass1" class="com.test1.child.ChildClass1">  

4)、ChildClass1
[java] view plain copy
  1. public class ChildClass1 implements InitializingBean {  
  2.   //这里required加上false,是因为是没有建立父子容器关系之前,这个parentClass是注入不了了的  
  3.     @Autowired(required = false)  
  4.     ParentClass parentClass;  
  5.   
  6.   //这里required加上false,是因为子容器之前是不能够相互引用的,只是测试使用。另注:这个类没有放到这里,在子容器2中,这里不贴代码了  
  7.     @Autowired(required = false)  
  8.     ChildClass2 childClass2;  
  9.   
  10.     public void print() {  
  11.         if (parentClass != null) {  
  12.             parentClass.print();  
  13.         }  
  14.         System.out.println("This is child class 1");  
  15.         if (childClass2 != null) {  
  16.             childClass2.print();  
  17.         }  
  18.     }  
  19.   
  20.     public void afterPropertiesSet() throws Exception {  
  21.         print();  
  22.   
  23.     }  
  24. }  
    来源: http://blog.csdn.net/fenglibing/article/details/8597789

0 0