我有一个使用多个网络片段的主网络应用项目。它在 Tomcat 8 上运行。
在我的 web.xml 文件中,我引用了主要的 Spring 上下文,例如:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:META-INF/spring/mainCtx.xml
</param-value>
</context-param>
并且在 WEB-INF/lib
文件夹我有一个 componentA.jar
在它自己的 META-INF
下包含一个 web-fragment.xml文件夹(不同于主应用程序的 META-INF
文件夹):
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:META-INF/spring/componentA_Ctx.xml
</param-value>
</context-param>
但是,Spring 似乎无法在 jar 中找到我的 compomentA_Ctx.xml。进一步阅读,似乎是因为名称冲突 contextConfigLocation
在 2 个上下文文件中。
我的目标是为组件提供可插拔性,使其独立于主 Web 应用程序。因此我想单独加载它的上下文。
有没有办法让 Spring 在两个位置都查找上下文?或者在 web.xml 和 web-fragment.xml 中分别定义上下文的方法?
最佳答案
我遇到过类似的问题,在 web.xml 和 web-fragment.xml 中定义两个上下文不起作用。我通过在邮件上下文中导入 componentA_Ctx.xml 来管理,如下所示。
<import resource="classpath*:/META-INF/yourpath/componentA_Ctx.xml"/>
上面的配置还是像个可插拔的东西。即使类路径中没有这样的文件,主上下文的加载也不会失败。
关于spring - 分别从 web.xml 和 web-fragment.xml 定义上下文参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43753239/