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

孤街醉人 提交于 2020-01-05 04:41:06

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/9856721/spring-persistence-archive-and-entitymanager-is-null-when-accessing-from-web-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!