spring-hibernate从类路径资源加载* .hbm.xml
我在位于src/main/resources maven文件夹中的classpath资源中有一些hbm.xml文件。我用Spring的LocalSessionFactoryBean的加载与下面bean配置这些文件:spring-hibernate从类路径资源加载* .hbm.xml
<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSourceOracle"/>
<property name="mappingResources">
<list>
<value>mapping/SystemUser.hbm.xml</value>
<value>mapping/SystemCredential.hbm.xml</value>
<value>mapping/SystemProvince.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
</value>
</property>
</bean>
但它给我的FileNotFoundException异常。请告诉我我做错了什么 谢谢。
这对我来说看起来相当不错。因此我不认为问题在于配置。我宁愿认为这些文件不在类路径中。你是如何开始申请的?
如果您使用eclipse,请确保src/main/resources被用作源文件夹,并且资源被复制到目标/类。
是的,我有默认的maven的src/main /资源作为源文件夹,我的应用程序是一个web应用程序。我已将这些文件复制到web-inf/classes文件夹,但它不起作用 – robinmag 2009-12-22 17:23:10
当您将“这些文件写入web-inf/classes”时,您的意思是'WEB-INF /班/ SystemUser.hbm.xml'?如果是,那么你应该把它移到'web-inf/classes/mapping/SystemUser.hbm.xml'。我现在没有任何其他想法。 – sfussenegger 2009-12-22 17:39:44
在Web应用程序中,当您编写无前缀的资源路径时,Spring会从上下文根(即,从包含WEB-INF的文件夹)加载它。若要从类路径中加载资源,你应该使用“classpath:”的前缀:
<value>classpath:mapping/SystemUser.hbm.xml</value>
文件使用Maven与war
类型的项目时位于src/main/resources
最终在WEB-INF/classes
(和resources
目录结构保存)。因此,要么把你的映射文件中src/main/resources/mapping
或使用以下配置:
<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSourceOracle"/>
<property name="mappingResources">
<list>
<value>SystemUser.hbm.xml</value>
<value>SystemCredential.hbm.xml</value>
<value>SystemProvince.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
</value>
</property>
</bean>
@Autowired
private ResourceLoader rl;
@Bean
public LocalSessionFactoryBean sessionFactory() throws IOException {
LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
sessionFactoryBean.setMappingLocations(loadResources());
}
public Resource[] loadResources() {
Resource[] resources = null;
try {
resources = ResourcePatternUtils.getResourcePatternResolver(rl)
.getResources("classpath:/hibernate/*.hbm.xml");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resources;
}
如果从web应用程序加载你的Spring应用程序上下文,你可能会看到这样的错误:
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: ServletContext resource [/hibernate.cfg.xml] cannot be resolved to URL because it does not exist
解决方案是明确告诉Spring从类路径加载配置,如下所示:
classpath:mypath/myfile.xml
类似于[WAR有两个META-INF文件夹](http://*.com/questions/17997731/maven-war-has-meta-inf-folder-in-two-places)。 – 2015-03-25 10:54:45