spring配置中mappingResources和mappingLocations

来源:互联网 发布:vscode spring boot 编辑:程序博客网 时间:2024/05/29 19:50

今天真是可笑啊,在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/

0 0