2

I have a war based spring web application project which internally has multiple jar files. I am using maven setup to build jars and war file. Each jar file has a set of beans that needs to be loaded and i am not able to do so.

In each of the jar file i have defined a beans.xml file . But the beans are not getting loaded automatically. I have tried loading the beans.xml file from: a) src/main/resources b) src/main/resources/META-INF c) src/main/resources/META-INF/spring It doesnt work.

My Question: How to prepare the application context for such scenarios? War based app with multiple jars.

  • How do you start your spring context? Do the spring-files have all the same name? In your service.jar, name your context appContext-service.xml, in your web-app, name in appContext-web.xml, place them all in the same location. In your context-loader, specify the location with classpath:appContext-*.xml. I think this should work, but didn't try it. – Martin Baumgartner Feb 09 '15 at 11:30
  • Do you use tags to import other XMLs from the main application context? – Avi Feb 09 '15 at 11:31
  • You can check this question: http://stackoverflow.com/questions/600095/splitting-applicationcontext-to-multiple-files – Efe Kahraman Feb 09 '15 at 14:30

3 Answers3

2

If your are packaging your application as a webapp one, then you can simply add a file named yourservletname-servlet.xml and include all resources from your jar files using the <import /> element.

Spring, behind the scenes, will scan the file mentioned above by default including all beans declared in the files imported.

Here is how your servletname-servlet.xml should look like (xml namespace and schemas declaration are ommited for brevity sake):

<beans>
  <import resource="classpath:/META-INF/beans.xml"/>
</beans>

I suggest the use of the META-INF as your context config files location.

This will scan all bean declaration files named beans.xml under META-INF folder under the root of your classpath, which assumes that those files must be under src/main/resources/META-INF/ in your project structure when using Maven as your build tool (so they can get copied directely under jar_root_path/META-INF/).

Otherwise, if you are not using the default -servlet.xml file, you can specify a custom application context descriptor using the contextConfigLocation as follows:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>application-context.xml</param-value>
</context-param>
tmarwen
  • 15,750
  • 5
  • 43
  • 62
1

Try simple import resource="classpath*:/META-INF/beans.xml"/

Where each jar contains beans.xml file under META-INF folder.It will scan each jar and load beans.xml file and creates beans based on these XMLs files.

Jujhar
  • 21
  • 4
0

You mention beans.xml, is this a CDI project or a standard Spring project ?

Using maven, everything under src/main/resources gets packaged at the top level of your JAR. So if you had a file in src/main/resources/META-INF/beans.xml, then you should load it using "/META-INF/beans.xml" or define it in your spring context as "classpath:/META-INF/beans.xml".

thedoctor
  • 1,498
  • 1
  • 12
  • 12