如何将 spring 应用程序上下文导入另一个应用程序上下文?

Jav*_*cal 5 java spring spring-mvc maven applicationcontext

我有两个 spring 应用程序上下文。一个是我的应用程序本地的,另一个来自 maven 依赖项之一。

现在,我的applicationContext.xml文件看起来像这样。

<import resource="classpath*:**/sample-applicationContext.xml" />
Run Code Online (Sandbox Code Playgroud)

<context:component-scan>sample-applicationContext.xml扫描组件的文件中。

现在,当我执行以下操作时..

ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
MyClass m=ctx.getBean(MyClass.class);
Run Code Online (Sandbox Code Playgroud)

不幸的是,当我得到MyClass对象时,对象被创建,但是,我看到MyClass没有注入依赖项。

自动装配的依赖项MyClass是使用<context:component-scan>insample-applicationContext.xml文件扫描的 bean 。

有没有办法在我的项目类中使用 Maven 依赖项中存在的多个应用程序上下文并自动装配它们?

Luk*_*ker 0

我不确定 Maven 依赖项,但您正在尝试使用 spring 执行诸如跨上下文之类的操作。看看这个链接:http://blog.imaginea.com/cross-context-communication- Between-web-applications/

编辑:似乎有一种方法,看看这篇文章:Loading spring application context files that are inside a jar in classpath