struts2+spring+hibernate整合需注意的问题

1,spring的配置文件:applicationContext.xml

  <!-- 注册LocalSessionFactoryBean -->
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />

    <!-- 指定hibernate中的映射资源文件(xxx.hbm.xml),mappingResources对应的值不应该加 classpath-->
    <property name="mappingResources">
       <list>

        <!--不要加classpath:-->
          <value>com/wzh/oa/domain/User.hbm.xml</value>
       </list>
    </property>

    <!-- 指定hibernate配置文件(hibernate.cfg.xml),需在路径前加classpath: -->
    <property name="configLocation" value="classpath:hibernate.cfg.xml" > 
 </bean>
 一般让你指定文件的路径(location)时,他是按照配置文件的要求来加载文件(配置文件不一定就在类路径下),需要指定该配置文件是不是在类路径下

如果是让你指定是哪个具体的资源文件时(resource),他是按照资源文件的要求来加载文件的,这样的文件一般都是放在类路径下的(不用加classppath),默认是通过类加载器来加强

2,在hibernate.cfg.xml中也可以加载映射文件xxx.cfg.xml:

<hibernate-configuration>
 <session-factory>
 
    <!-- 可以在这里加载映射资源文件 (即xxx.hbm.xml),不用在路径前加上classpath:,默认是在类路径下 -->
  <mapping resource="com/wzh/oa/domain/User.hbm.xml"/>
   
 </session-factory>
</hibernate-configuration>

3,struts2与spring整合后,还需要在web.xml中配置监听器,用来监听spring容器的创建

 <!-- 配置ContextLoaderListener监听器,用来监听在服务器重启时就初始化spring容器的创建 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>   // 名称是固定的
    <param-value>classpath:applicationContext.xml</param-value>   // 如spring有多个配置文件如a.xml,b.xml。那么:classpath:a.xml , classpath:b.xml
 </context-param>
 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 

转载于:https://www.cnblogs.com/wzhBlog/archive/2013/03/11/2954854.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值