Spring Persistence archive and entitymanager is null when accessing from web-application
Asked Answered
P

1

1

I've created a jar file through spring roo (maven project - persistence archive) unit tests are running fine, the concerned files are on the following location

jarFile/META-INF/persistence.xml
jarFile/META-INF/applicationContext.xml
jarFile/META-INF/applicationContext-jpa.xml
jarFile/META-INF/database.properties

Unit tests are running fine.

Because its a maven project I added it to local repository by executing the command "mvn install" and after that I added it as a dependency to another maven based web-application.

I am running the web application using mvn jetty:run command. the concerned files in web application are.

webApp/WEB-INF/web.xml
webApp/WEB-INF/applicationContext.xml

The Problem * Its loading the webapp/WEB-INF/applicationContext.xml but how can I verify its loading the child jarFile/META-INF/applicationContext.xml or not? actually when i try to access the service class methods from persistence archive the entityManager is NULL. * If i try to put contextConfigLocation directive (tried various options) within web.xml, Its not even loading the webapp/WEB-INF/applicationContext.xml.

What I want Use the service methods (which uses entitymanager) from persistence archive from within my web application.

Thanks in advance.

Piggy answered 24/3, 2012 at 23:47 Comment(1)
How are you importing the child application context - is it this way: <import resource="classpath:/META-INF/applicationContext.xml"/>, if not that could be the reason for the resource not being foundHarkey
P
0

Found the answer by digging around a bit. Actually I was confused with various ways/syntax to include the context file, was trying with all sort of classpath*:xxx syntax but actually the WEB-INF is not on the class path so following have to be added to web.xml to load the main webApp/WEB-INF/applicationContext.xml

  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>

Then had to add the following to the webApp/WEB-INF/applicationContext.xml

<import resource="classpath*:META-INF/spring/applicationContext*.xml" />

Now the webapplication is loading the context file from jar file as well. And things are working.

Piggy answered 25/3, 2012 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.