weixin_40561987 2017-10-16 05:32 采纳率: 100%
浏览 870
已采纳

spring整合hibernate中的web.xml问题

下面是网上看的别人spring整合hibernate的web。xml文件
可我搞不懂最后那个几个是什么意思:配置spring声明式事务,配置事务事务属性,配置事务切点,并把切点和事务属性关联起来。

求大神讲解讲解!

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/sch
ema/tx/spring-tx-4.1.xsd">

<context:component-scan base-package="com.demo.ssm"></context:component-scan>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost/test" />
    <property name="username" value="root"></property>
    <property name="password" value="281889"></property>
</bean>  
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" lazy-init="false">
    <!-- 注入datasource,给sessionfactoryBean内setdatasource提供数据源 -->
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    <!-- //加载实体类的映射文件位置及名称 -->
    <property name="mappingLocations" value="classpath:com/demo/ssm/po/*.hbm.xml"></property>
</bean>  

<!-- 配置Spring声明式事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean> 
<!-- 配置事务事务属性 -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="get*" read-only="true"/>
        <tx:method name="*"/>
    </tx:attributes>
</tx:advice>
<!-- 配置事务切点,并把切点和事务属性关联起来 -->
<aop:config>
    <aop:pointcut expression="execution(* com.demo.ssm.daoImpl.*.*(..))" id="txPointcut"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>

  • 写回答

5条回答

  • paditang 2017-10-16 06:10
    关注

    这么说吧,hibernate使用时肯定也会涉及到事务。事务的使用保证了操作一致性,错误的回滚等数据库操作基本属性。
    那么Spring整合Hibernate时,配置的事务+事务属性+事务切点就是通过aop的方式将所有需要事务管理的方法统一用事务包裹起来。在实际代码中
    只需要关注业务逻辑的实现,而不需要重复transaction.commit(),transaction.rollback()等操作。
    所以
    1、配置Spring声明式事务 就是通过配置的sessionFactory获取事务管理器。等同于Hibernate中session.beginTransaction()。
    2、同样在Hibernate中使用事务,会在每个方法中重复写transaction.commit()之类的事务操作代码。
    而使用Spring+Hibernate配置好事务管理器后,需要知道那些地方需要事务管理。
    就是由
    确定适用方法是com.demo.ssm.daoImpl下所有类的所有方法,再根据

    tx:attributes


    /tx:attributes
    /tx:advice
    确定是以get开头的方法使用只读事务,其他方法都正常使用事务。
    所以是在切面com.demo.ssm.daoImpl下所有类的切点所有方法使用事务管理。
    以上主要关于Spring aop和Spring 事务管理的知识,可以查阅相关资料了解。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办