Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring上下文继承_Java_Spring - Fatal编程技术网

Java Spring上下文继承

Java Spring上下文继承,java,spring,Java,Spring,我在一个文件中有配置:applicationContext.xml。我有数据源、会话工厂和另外两个bean 我希望更改如下配置: applicationContext.xml->parent,其中包含数据源、会话工厂 bean1.xml->children,它继承自applicationContext,只包含关于bean1的信息 bean2.xml->children,它继承自applicationContext,只包含关于bean2的信息 我该怎么做?我找到了有关inherit beans

我在一个文件中有配置:applicationContext.xml。我有数据源、会话工厂和另外两个bean

我希望更改如下配置:

  • applicationContext.xml->parent
    ,其中包含数据源、会话工厂
  • bean1.xml->children
    ,它继承自applicationContext,只包含关于bean1的信息
  • bean2.xml->children
    ,它继承自applicationContext,只包含关于bean2的信息

我该怎么做?我找到了有关inherit beans的信息,但我还需要另外三个文件,而不是一个文件。请查看Spring中的。您可以在bean1.xml和bean2.xml中使用它来导入顶级applicationContext.xml配置文件,因此这两种配置在加载时是分开的。

原则上,应用程序上下文可以有父上下文,并从父上下文继承所有bean。不完全无关紧要的(除非最近有所改变)是如何构建关系

这帮助了我:

(我与作者没有任何关系)

另一种方法是编写代码来实现这一点。如果您有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;
    }

}