Spring persistence and entitymanager archive is null when accessing a web application

I created a jar file through spring roo (maven project - persistence archive) unit tests work fine, the corresponding files are in the following location

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

Performance tests work fine.

Since his maven project, I added it to the local repository by running the "mvn install" command, after which I added it as a dependency on another maven-based web application.

I am launching a web application using the mvn jetty: run command. corresponding files in the web application.

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

Problem * Its loading is webapp / WEB-INF / applicationContext.xml, but how can I check its loading of the child jarFile / META-INF / applicationContext.xml or not? in fact, when I try to access service class methods from a persistence archive, entityManager is NULL. * If I try to put the contextConfigLocation directive (I tried various parameters) in web.xml, it does not even load webapp / WEB-INF / applicationContext.xml.

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

Thanks in advance.

+1
source share
1 answer

Found the answer, digging a bit. Actually I was confused with different ways / syntax to include the context file, tried with all syntax types classpath *: xxx, but in fact WEB-INF is not in the class path, so the following should be added to web.xml for download main file webApp / WEB-INF / applicationContext.xml

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

Then it was necessary to add the following to webApp / WEB-INF / applicationContext.xml

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

Now the web application also loads the context file from the jar file. And everything works.

0
source

Source: https://habr.com/ru/post/944429/


All Articles