Andrea Girardi
Andrea Girardi

Reputation: 4427

Spring context configuration file issue

I build a library (a bounch of classes) that uses spring library and a context-application.xml file (to parse REST response using Marshalling).

At this point I included the jar on my project and I'm trying to call one of this classes, but it's telling me that context-application.xml file is missing.

I'm trying to load the application context using :

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml");

It's not so clear (I'm a newbie in Spring framework) where to put this file used by my jar.

Is it possible? Could anyone help me?

Upvotes: 0

Views: 461

Answers (2)

Peter Szanto
Peter Szanto

Reputation: 7722

If I understand correctly you want to use an XML from jar file that is outside of your jar and in the main project that contains the jar file? If so then it is not really a good practice as in this case your jar file will be dependent of the application therefore wont be really redistributable, so what is the point of having a jar file at all? If you look at the CXF JAX-RS implementation it is a good example of how to solve it nicely. It has a rather opposite approach. CXF has a number of it's own spring xml files inside that needs to be included from the main webapp like this.

  <import resource="classpath:META-INF/cxf/cxf.xml"/>
  <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>
  <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

that way you can define your main webapp classes and your jar classes in the same XML file. A complete example is here:

http://cxf.apache.org/docs/jax-rs.html#JAX-RS-JAXRSandSpringAOP

Upvotes: 1

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340733

It needs to be in the root of your CLASSPATH. In your case - the root directory of the JAR file. If you are using maven - it is the /src/main/resources folder.

Upvotes: 2

Related Questions