Causes for java.io.FileNotFoundException: class path resource [hibernat.xml] cannot be opened becaus

新手在做Spring和Hibernate的整合,我的Spring的配置文件是这么写的:



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
   
    <!-- 配置C3P0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql:///springHibernate"/>
        <property name="user" value="root"/>
        <property name="password" value="123"/>
    </bean>
   
    <!-- 把SessionFactory交给Spring管理 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- 因为 hibernate.cfg.xml中没有配置数据库信息,数据库配置在Spring里面,所以要注入dataSource-->
        <property name="dataSource" ref="dataSource"/>
       
        <!-- 指定使用Hibernate核心配置文件 -->
        <property name="configLocations" value="classpath:hibernate.cfg.xml"/>
    </bean>
   

    <bean id="userAction" class="com.action.UserAction"></bean>
</beans>


百度之后发现了问题,看到高人的指点后改了这么一个配置,马上正常运行。

原来:
        <property name="configLocations" value="classpath:hibernate.cfg.xml"/>

改为:

        <!-- 指定使用Hibernate核心配置文件 -->
        <property name="configLocations" value="classpath:/hibernate.cfg.xml"/>


原话是这么说的:

Did you try classpath:/services.xml? The leading slash means to pull the resource from the root of the classpath. Without it, the file is expected to be in the package of whatever class is loading it (which I admit I don't know what that would be in this case).

主要的斜杠意味着将资源从类路径的根中拉出。没有它这个文件被期望放在装入它的任何类的包中(我承认我不知道在这种情况下会是什么)。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值