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

问题描述 投票:4回答:2

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

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

<import resource="classpath*:**/sample-applicationContext.xml" />

并且我在<context:component-scan>文件中有sample-applicationContext.xml,该文件用于扫描组件。

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

ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
MyClass m=ctx.getBean(MyClass.class);

[不幸的是,当我获得MyClass对象时,创建了该对象,但我发现没有注入MyClass中的依赖项。

MyClass中自动关联的依赖项是使用<context:component-scan>文件中的sample-applicationContext.xml扫描的bean。

有没有办法利用Maven依赖项中存在的多个应用程序上下文,并在我的项目类中自动关联其中的bean?

java spring maven spring-mvc applicationcontext
2个回答
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


0
投票

通过以下方式,您可以加载多个spring applicationContext文件。

ApplicationContext context = 
new ClassPathXmlApplicationContext(new String[] {"sample-applicationContext.xml",   "applicationContext.xml"});

如果可以在此处发布applicationContext.xml,sample-applicationContext.xml和MyClass代码段,那就很好。

© www.soinside.com 2019 - 2024. All rights reserved.