3

I have a main webapp project that uses multiple web-fragments. It runs on Tomcat 8.

Here in my web.xml file, I have a reference to main Spring context like:

<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):

<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. Upon reading further, it seems that because there is a name clash contextConfigLocation in 2 context files.

My goal is to give compoment A plugability so that it is independent from the main webapp. Hence I want to load its context seprately.

Is there anyway to make Spring look for contexts in both locations? Or a way to define contexts separately in web.xml and web-fragment.xml?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
hydradon
  • 1,316
  • 1
  • 21
  • 52

2 Answers2

0

I have faced the similar issue, defining the two contexts in web.xml and web-fragment.xml did not work. I had managed by importing componentA_Ctx.xml in mail context like below.

 <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.

TechVara
  • 1
  • 1
0

Here goes my 50 cents.

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).

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.

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.

Long story short, I've made use of spring default applicationContext to achieve what I want:

  1. web.xml has no contextConfigLocation, but there is a WEB-INF/applicationContext.xml in the web project. So, main product is clean and working fine.

  2. web-fragment.xml could be set as the options below:

2.1. Replacing applicationContext:

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:META-INF/spring/custom-AppContext.xml</param-value>
  </context-param>

2.2. Complementing 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.

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.

Reginaldo Santos
  • 840
  • 11
  • 24