spring配置中mappingResources和mappingLocations

本文详细介绍了在Spring和Hibernate集成过程中,由于路径配置错误导致FileNotFoundException的问题及其解决方法。通过修改配置项从'mappingResources'改为'mappingLocations',问题得以解决。文章还提供了区分两者使用场景的解释。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天真是可笑啊,在spring和hibernate集成的时候,把hibernate的映射文件路径放到”sessionFactory”配置项中。结果报错,说是:

Error creating bean with name ‘sessionFactory’ defined in file [E:\soft\Apache Software Foundation\Tomcat 7.0\webapps\ExamSystem\WEB-INF\classes\applicationContext-db.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [classpath:com/exam/model/*.hbm.xml] cannot be opened because it does not exist

我检查了很久,路径没写错啊,怎么会FileNotFound 呢?于是查看jar包有没有少,web.xml 配置有没有写错,折腾了很久都没发现什么问题。百度、google很久也没找到答案。

最后却发现这是一个脑残的错误!我是这么写的:



classpath:com/exam/model/*.hbm.xml

我完全没注意啊,我以为 mappingResources 里面可以这么写的。后来把“mappingResources ”改成了“mappingLocations”就没问题了。至于它们的区别,摘录一篇文章,地址:http://wukaxi.iteye.com/blog/1401753

内容如下:

mappingResources、mappingLocations、mappingDirectoryLocations、mappingJarLocations
他们的区别:
1. mappingResources:指定classpath下具体映射文件名

petclinic.hbm.xml

2. mappingLocations:可以指定任何文件路径,并且可以指定前缀:classpath、file等

/WEB-INF/petclinic.hbm.xml


classpath:/com/company/domain/petclinic.hbm.xml

也可以用通配符指定,’‘指定一个文件(路径)名,’*‘指定多个文件(路径)名,例如:

classpath:/com/company/domainmaps/*.hbm.xml

上面的配置是在com/company/domain包下任何maps路径下的hbm.xml文件都被加载为映射文件

  1. mappingDirectoryLocations:指定映射的文件路径


WEB-INF/HibernateMappings


也可以通过classpath来指出


classpath:/XXX/package/

转载于:https://my.oschina.net/huluobotx/blog/499928

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值