Davinder Singh
Davinder Singh

Reputation: 31

How to load spring beans for multiple jar file in a war based application

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.

Upvotes: 2

Views: 4908

Answers (3)

Jujhar
Jujhar

Reputation: 21

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.

Upvotes: 1

tmarwen
tmarwen

Reputation: 16354

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>

Upvotes: 2

thedoctor
thedoctor

Reputation: 1508

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

Upvotes: 0

Related Questions