从Web应用程序访问时,Spring Persistence存档和entitymanager为null [英] Spring Persistence archive and entitymanager is null when accessing from web-application

查看:67
本文介绍了从Web应用程序访问时,Spring Persistence存档和entitymanager为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过Spring Roo(Maven项目-持久性归档)创建了一个jar文件,单元测试运行良好,相关文件位于以下位置

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

单元测试运行良好.

因为它是一个Maven项目,所以我通过执行命令"mvn install"将其添加到本地存储库中,然后我将其作为对另一个基于maven的Web应用程序的依赖项添加.

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.

我正在使用mvn jetty:run命令运行Web应用程序.Web应用程序中的相关文件.

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

问题*它加载了webapp/WEB-INF/applicationContext.xml,但是如何验证是否加载了子jarFile/META-INF/applicationContext.xml?实际上,当我尝试从持久性存档访问服务类方法时,entityManager为NULL.*如果我尝试将contextConfigLocation指令(尝试了各种选项)放在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.

我想要的在我的Web应用程序中使用持久性存档中的服务方法(使用entitymanager).

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

谢谢.

推荐答案

通过仔细研究找到了答案.实际上,我对包含上下文文件的各种方式/语法感到困惑,尝试使用各种classpath *:xxx语法,但实际上WEB-INF不在类路径上,因此必须将以下内容添加到web.xml中进行加载主要的webApp/WEB-INF/applicationContext.xml

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>

然后必须将以下内容添加到webApp/WEB-INF/applicationContext.xml

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

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

现在,Web应用程序也正在从jar文件中加载上下文文件.而且一切正常.

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

这篇关于从Web应用程序访问时,Spring Persistence存档和entitymanager为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆