繁体   English   中英

配置 Eclipse Web 项目以测试 Spring/JPA 的理想方法是什么

[英]What's the ideal way to configure an Eclipse Web Project to test Spring/JPA

我最近使用 eclipse(实际上是 myeclipse)通过 new->web 项目向导创建了一个新的 Web 项目。 项目的默认目录结构如下所示:

src
WebRoot
WebRoot/META-INF
WebRoot/META-INF/MANIFEST.MF
WebRoot/WEB-INF
WebRoot/WEB-INF/lib
WebRoot/WEB-INF/classes (not visible via the Package/Project Explorer)
WebRoot/WEB-INF/web.xml

对于 jpa 我添加了:

WebRoot/META-INF/persistence.xml  

对于 spring 我添加了:

WebRoot/META-INF/spring/spring-master.xml

当尝试运行一个非常基本的 junit 测试来加载 spring 上下文和 hibernate 实体管理器时,我遇到了类路径问题。 最初为了解决我遇到的第一个 class 路径问题,当 Spring 找不到我的 Z2A2D595E6ED9A0B234F027F2B663 文件时,我将 WebRoot WebRoot/META-INF/spring添加到 junit->Run Configurations 类路径中。

但是当 jpa 找不到我的实体 bean 定义时,我遇到了另一个类路径错误。 Reading this article (dated 2007) it states that META-INF/persistence.xml should be contained within WEB-INF/classes because JPA searches for entity bean class files in the parent directory of META-INF/persistence.xml

当我复制META-INF (通过 Windows 资源管理器,因为 eclipse 隐藏了类目录)并将其所有内容复制到WEB-INF/classes中时,我的类路径错误消失了( 以及需要使用 Z587FEFE304B8E3505DE8 配置来运行之前的补救措施C5DDC8 problem of spring not knowing where my spring config file is located turns out I still need to add the spring folder to the junit classpath option).

但是现在我在 Eclipse 的包/项目资源管理器中没有显示的文件夹中有配置文件。

在这种情况下设置 Eclipse 的最佳方法是什么? 告诉它以某种方式显示类文件夹? 为什么 Eclipse 创建META-INF作为 web 项目的WEB-INF的兄弟?

有没有办法通知 JPA 在默认以外的地方寻找实体类?

感谢您的任何意见!

您应该知道 eclipse 混合了<WebRoot>/WEB-INF/classes目录中的src (以及任何其他源代码目录)中的已编译类和资源。

因此,在 Test 和 Webapp(例如 META-INF/persistence.xml)中访问同一文件的一种方法是将其放入src/META-INF/persistence.xml中。 Then it is available in <WebRoot>/WEB-INF/classes/META-INF/persistence.xml for the web app, and in <classpath>/META-INF/persistence.xml for EVERY application or test.

So my recommendation is to put all the stuff that is addressed via classpath (persistence.xml, spring.xml) in a source directory, and let eclipse do the rest. - 如果您通过类路径指定资源的位置,那么它应该以相同的方式用于 webapp、test、app。

BTW:eclipse 可以创建多个源代码目录,一个用于普通应用程序类,一个用于 class 路径相关资源,一个用于测试类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM