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条)

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条