[英]Define context param from web.xml and web-fragment.xml separately
I have a main webapp project that uses multiple web-fragments. 我有一个使用多个Web片段的主要Webapp项目。 It runs on Tomcat 8.
它在Tomcat 8上运行。
Here in my web.xml file, I have a reference to main Spring context like: 在我的web.xml文件中,我有对Spring主上下文的引用,例如:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:META-INF/spring/mainCtx.xml
</param-value>
</context-param>
And in WEB-INF/lib
folder I have a componentA.jar
that contains a web-fragment.xml under its own META-INF
folder (different from the main app's META-INF
folder): 在
WEB-INF/lib
文件夹中,我有一个componentA.jar
,它在其自己的META-INF
文件夹(与主应用程序的META-INF
文件夹不同)下包含web-fragment.xml :
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:META-INF/spring/componentA_Ctx.xml
</param-value>
</context-param>
However, it seems that Spring cannot find my compomentA_Ctx.xml inside the jar. 但是,似乎Spring无法在jar中找到我的compomentA_Ctx.xml。 Upon reading further, it seems that because there is a name clash
contextConfigLocation
in 2 context files. 进一步阅读后,似乎是因为在2个上下文文件中有一个名称冲突
contextConfigLocation
。
My goal is to give compoment A plugability so that it is independent from the main webapp. 我的目标是赋予compoment A可插入性,使其独立于主要的Web应用程序。 Hence I want to load its context seprately.
因此,我想单独加载其上下文。
Is there anyway to make Spring look for contexts in both locations? 无论如何,Spring能否在这两个位置中查找上下文? Or a way to define contexts separately in web.xml and web-fragment.xml ?
还是在web.xml和web-fragment.xml中分别定义上下文的方法?
I have faced the similar issue, defining the two contexts in web.xml and web-fragment.xml did not work. 我遇到了类似的问题,在web.xml和web-fragment.xml中定义两个上下文不起作用。 I had managed by importing componentA_Ctx.xml in mail context like below.
我已经通过在如下所示的邮件上下文中导入componentA_Ctx.xml进行管理。
<import resource="classpath*:/META-INF/yourpath/componentA_Ctx.xml"/>
The above configuration is still like a pluggable thing. 上面的配置仍然像可插拔的东西。 Loading of main context wont fail even if there is no such file in classpath.
即使classpath中没有这样的文件,主上下文的加载也不会失败。
Here goes my 50 cents. 这是我的50美分。
I had a similar issue: needed to keep main product (WAR) clean, with the possibility to extend/replace its application context via custom dependency in classpath (JAR). 我有一个类似的问题:需要保持主产品(WAR)整洁,并有可能通过类路径(JAR)中的自定义依赖项来扩展/替换其应用程序上下文。
WebInitializer was not an option at the time and as you've already notice, it is not possible to use contextConfigLocation in web.xml and web-fragment.xml at same time. WebInitializer当时不是一个选项,并且您已经注意到,不可能同时在web.xml和web-fragment.xml中使用contextConfigLocation 。
Importing custom JAR resources into main product context might be an option, but you need to define very clear rules for names and paths, thus its not that clean. 将自定义JAR资源导入到主要产品上下文中可能是一种选择,但是您需要为名称和路径定义非常清晰的规则,因此它不是那么干净。
Long story short, I've made use of spring default applicationContext to achieve what I want: 长话短说,我已经利用spring的默认applicationContext实现了我想要的:
web.xml has no contextConfigLocation , but there is a WEB-INF/applicationContext.xml in the web project. web.xml没有contextConfigLocation ,但是Web项目中有一个WEB-INF / applicationContext.xml 。 So, main product is clean and working fine.
因此,主要产品清洁且工作正常。
web-fragment.xml could be set as the options below: web-fragment.xml可以设置为以下选项:
2.1. 2.1。 Replacing applicationContext:
替换applicationContext:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:META-INF/spring/custom-AppContext.xml</param-value>
</context-param>
2.2. 2.2。 Complementing applicationContext:
补充applicationContext:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml classpath:META-INF/spring/custom-AppContext.xml</param-value>
</context-param>
Just note here, that for my case (customization), being complementary and sharing the same applicationContext with the main product was desired. 在这里仅需注意,对于我的情况(定制),需要与主要产品互补并共享相同的applicationContext 。
Finally, this will work for single custom JAR (my business case: one custom jar per client), but if you have more "jar components" and need to stick with xml, importing resources with "classpath*" and add some rules for names might be the only option. 最后,这将适用于单个自定义JAR(我的业务案例:每个客户端一个自定义jar),但是如果您有更多的“ jar组件”并且需要坚持使用xml,请使用“ classpath *”导入资源并添加一些名称规则可能是唯一的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.