春天 + Hibernate 会话工厂:映射 JAR 内的文件目录
我有基于 Hibernate 和 Spring 的 Web 项目(编程配置)。 Hibernate 映射在一个包中提供,该包存档在 JAR 中。
当谈到初始化会话工厂时,我曾经调用:
sessionFactory.
setMappingDirectoryLocations(new Resource[]{
new ClassPathResource("org/all/theway/to/hibernatemappings")});
为了告诉 Hibernate 在哪里寻找映射文件。 “org/all/theway/to/hibernatemappings”是包含 hbm.xml 文件的包。这在 Eclipse(GWT 开发模式)中运行良好,因为包含映射的项目也被签出并链接到我的 Web 项目。但是,一旦我创建了一个war并将其部署到Tomcat,它就无法获取类路径资源。
:“如果类路径资源驻留在文件系统中,则支持解析为 java.io.File,但不支持 JAR 中的资源。始终支持解析为 URL。”
Spring 的 ClasspathResource javadoc 暗示了这一点 该怎么办?我也可以使用 setMappingJarLocation 代替,但我不喜欢在 Spring 上下文中硬编码 jar 文件名。此外,当我尝试它时,它也只能在IDE中工作,但在Tomcat中相同的文件路径(WEB-INF/lib/file.jar)不起作用。这也让我相信这将是一个丑陋的解决方案。
是否有一种无需使用 jar 文件即可工作的解决方法?
I have web project based on Hibernate and Spring (programmatic configuration). The Hibernate mappings are provided in a package which is archived in a JAR.
When it comes to initialize the session factory I used to call:
sessionFactory.
setMappingDirectoryLocations(new Resource[]{
new ClassPathResource("org/all/theway/to/hibernatemappings")});
in order to tell Hibernate where to look for mapping files. "org/all/theway/to/hibernatemappings" is the package which contains the hbm.xml files. This worked fine within Eclipse (GWT dev mode), as the mapping-containing project is also checked out and linked to my web project. However, once I create a war and deploy it to Tomcat, it fails to get the class path resource.
Spring's ClasspathResource javadoc implies this: "Supports resolution as java.io.File if the class path resource resides in the file system, but not for resources in a JAR. Always supports resolution as URL. "
But what to do instead? I could also use setMappingJarLocation instead, but I do not like to hardcode a jar file name in my Spring context. Further, when I tried it, it also only worked within IDE, but inside Tomcat the same file path (WEB-INF/lib/file.jar) did not work. This also makes me believe that this would be an ugly solution.
Is there a workaround which works without using the jar file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这有效
//所有应用程序上下文实现 ResourcePatternResolver
编辑:用 ResourcePatternResolver 替换 DefaultResourceLoader。
This works
//All application contexts implements ResourcePatternResolver
EDIT: Replaced DefaultResourceLoader with ResourcePatternResolver.
这并不是原始问题的真正解决方案,而是避免在配置代码中处理 jar 文件的文件系统文件名的解决方法:
我只是告诉 ant 将有问题的 JAR 解压到 WEB-INF/classes 文件夹中
现在,Hibernate 映射驻留在其中在文件系统中,可以作为 ClassPathResource 进行访问(如问题的示例中所示)。
Not really a solution of the original problem but a workarounnd which avoids dealing with filesystem file names of jar files within the configuration code:
I simply told ant to unpack the JAR in question into the WEB-INF/classes folder
Now, the Hibernate mappings reside in the file system and can be accessed as ClassPathResource (like in the questions's example).