2012-09-24 31 views
3

我在一个文件中配置了:applicationContext.xml。我有数据源,会话工厂和其他两个bean。Spring上下文继承

我想更改配置是这样的:

  • applicationContext.xml -> parent,其中包含数据源,会话工厂
  • bean1.xml -> children,从的applicationContext继承和包含bean1
  • bean2.xml -> children只有信息,从的applicationContext继承并只包含有关bean2的信息

我该怎么做?我发现有关继承beans的信息,但我想要另外三个文件不是一个

回答

1

看看Spring中的import tag。你可以在bean1.xml和bean2.xml中使用它来导入你的顶级applicationContext.xml配置文件,这样两个配置在加载时就会分开。

0

原则上,应用程序上下文可以有一个父上下文,并且只是从那里继承所有的bean。不完全微不足道的(除非最近发生变化)是如何构建关系。

帮我:

http://blog.springsource.org/2007/06/11/using-a-shared-parent-application-context-in-a-multi-war-spring-application/

(我在与笔者没有隶属关系)。

另一种方法是实际编写代码来执行此操作。如果您有Web应用场景,则可以扩展Spring的ContextLoaderListener并覆盖loadParentContext:

public class ComponentParentContextContextLoaderListener extends ContextLoaderListener { 

    @Override 
    protected ApplicationContext loadParentContext(ServletContext servletContext) { 
     // load parent context .e.g from class path/static member 
     return ac; 
    } 

}