1. tx 配置方法, 代码示例

javabean及其映射文件省略,和上篇的一样

CustomerDao.java, dao层接口

  1. public interface CustomerDao {
  2. public void insertCustomer(Customer c);
  3. public void updateCustomer(Customer c);
  4. public List<Customer> findCustomerByName(String name);
  5.  
  6. //批量保存
  7. //public void saveCustomers(List<Customer> list);
  8. }

CustomerDaoImpl.java ,dao层实现

  1. public class CustomerDaoImpl implements CustomerDao {
  2.  
  3. private SessionFactory sf ;
  4. public void setSf(SessionFactory sf) {
  5. this.sf = sf;
  6. }
  7.  
  8. public List<Customer> findCustomerByName(String name) {
  9. String hql = "from Customer c where c.name = ?";
  10. Query q = sf.getCurrentSession().createQuery(hql);
  11. q.setString(0, name);
  12. return q.list();
  13. }
  14.  
  15. public void insertCustomer(Customer c) {
  16. sf.getCurrentSession().save(c);
  17. }
  18.  
  19. public void updateCustomer(Customer c) {
  20. sf.getCurrentSession().update(c);
  21. }
  22. }

CustomerService.java ,service层接口

  1. public interface CustomerService {
  2. public void insertCustomer(Customer c);
  3. public void updateCustomer(Customer c);
  4. public List<Customer> findCustomerByName(String name);
  5.  
  6. //批量保存
  7. public void saveCustomers(List<Customer> list);
  8. }

CustomerServiceImpl.java,service层实现

  1. public class CustomerServiceImpl implements CustomerService {
  2. //dao
  3. private CustomerDao dao ;
  4.  
  5. //注入dao
  6. public void setDao(CustomerDao dao) {
  7. this.dao = dao;
  8. }
  9.  
  10. public List<Customer> findCustomerByName(String name) {
  11. return dao.findCustomerByName(name);
  12. }
  13.  
  14. public void insertCustomer(Customer c) {
  15. dao.insertCustomer(c);
  16. }
  17.  
  18. /**
  19. * 批量保存, 编程式事务管理
  20. */
  21. public void saveCustomers(final List<Customer> list) {
  22. for(Customer c : list){
  23. this.insertCustomer(c);
  24. }
  25. }
  26.  
  27. public void updateCustomer(Customer c) {
  28. dao.updateCustomer(c);
  29. }
  30. }

jdbc.properties

  1. jdbc.driverclass=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/spring
  3. jdbc.username=root
  4. jdbc.password=root
  5.  
  6. c3p0.pool.size.max=10
  7. c3p0.pool.size.min=2
  8. c3p0.pool.size.ini=3
  9. c3p0.pool.size.increment=2
  10.  
  11. hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
  12. hibernate.show_sql=true
  13. hibernate.hbm2ddl.auto=none

sh.xml,spring配置文件

  1. <?xml version="1.0"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  11. http://www.springframework.org/schema/tx
  12. http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  13. http://www.springframework.org/schema/aop
  14. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
  15. <!-- 指定分散配置的文件的位置 -->
  16. <context:property-placeholder location="classpath:cn/itcast/spring/hibernate/tx25/jdbc.properties" />
  17. <!-- 配置c3p0数据源 -->
  18. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  19. <property name="driverClass" value="${jdbc.driverclass}" />
  20. <property name="jdbcUrl" value="${jdbc.url}" />
  21. <property name="user" value="${jdbc.username}" />
  22. <property name="password" value="${jdbc.password}" />
  23.  
  24. <property name="maxPoolSize" value="${c3p0.pool.size.max}" />
  25. <property name="minPoolSize" value="${c3p0.pool.size.min}" />
  26. <property name="initialPoolSize" value="${c3p0.pool.size.ini}" />
  27. <property name="acquireIncrement" value="${c3p0.pool.size.increment}" />
  28. </bean>
  29.  
  30. <!-- 本地回话工厂bean,spring整合hibernate的核心入口 -->
  31. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  32. <property name="dataSource" ref="dataSource" />
  33. <!-- 指定hibernate自身的属性 -->
  34. <property name="hibernateProperties">
  35. <props>
  36. <prop key="hibernate.dialect">${hibernate.dialect}</prop>
  37. <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
  38. <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
  39. </props>
  40. </property>
  41. <property name="mappingDirectoryLocations">
  42. <list>
  43. <value>classpath:cn/itcast/spring/domain</value>
  44. </list>
  45. </property>
  46. </bean>
  47.  
  48. <!-- customerDao -->
  49. <bean id="customerDao" class="cn.itcast.spring.hibernate.tx25.CustomerDaoImpl">
  50. <property name="sf" ref="sessionFactory" />
  51. </bean>
  52.  
  53. <!-- hibernate事务管理器,在service层面上实现事务管理,而且达到平台无关性 -->
  54. <bean id="htm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  55. <property name="sessionFactory" ref="sessionFactory" />
  56. </bean>
  57.  
  58. <!-- customerService -->
  59. <bean id="customerService" class="cn.itcast.spring.hibernate.tx25.CustomerServiceImpl">
  60. <property name="dao" ref="customerDao" />
  61. </bean>
  62.  
  63. <!-- 事务通知 -->
  64. <tx:advice id="txAdvice" transaction-manager="htm">
  65. <!-- 事务属性 -->
  66. <tx:attributes>
  67. <tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT"/>
  68. <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>
  69. <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>
  70. <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/>
  71. <tx:method name="batch*" propagation="REQUIRED" isolation="DEFAULT"/>
  72.  
  73. <tx:method name="load*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
  74. <tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
  75. <tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
  76.  
  77. <tx:method name="*" propagation="SUPPORTS" isolation="DEFAULT" read-only="false"/>
  78. </tx:attributes>
  79. </tx:advice>
  80.  
  81. <!-- aop配置(配置切入点) -->
  82. <aop:config>
  83. <aop:pointcut id="txPointcut" expression="execution(* *..*Service.*(..))"/>
  84. <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
  85. </aop:config>
  86. </beans>

App.java 测试代码

  1. public class App {
  2.  
  3. public static void main(String[] args) throws SQLException {
  4. ApplicationContext ac = new ClassPathXmlApplicationContext(
  5. "cn/itcast/spring/hibernate/tx25/sh.xml");
  6. CustomerService cs = (CustomerService) ac.getBean("customerService");
  7. List<Customer> list = new ArrayList<Customer>();
  8. Customer c = null ;
  9. for(int i = 0 ; i < 10 ; i++){
  10. c = new Customer();
  11. if(i == 9){
  12. c.setName(null);
  13. }
  14. else{
  15. c.setName("tom" + i);
  16. }
  17. c.setAge(20 + i);
  18. list.add(c);
  19. }
  20. cs.saveCustomers(list);
  21. }
  22.  
  23. }

2. aspectj 配置方法 代码示例

和(1)tx方式相比只有两个文件有变动

CustomerServiceImpl.java, service实现 以加注解的方式

  1. /**
  2. * 通过注解方法实现事务管理
  3. */
  4. @Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT)
  5. public class CustomerServiceImpl implements CustomerService {
  6. //dao
  7. private CustomerDao dao ;
  8.  
  9. //注入dao
  10. public void setDao(CustomerDao dao) {
  11. this.dao = dao;
  12. }
  13.  
  14. /**
  15. * 只读
  16. */
  17. @Transactional(readOnly=true)
  18. public List<Customer> findCustomerByName(String name) {
  19. return dao.findCustomerByName(name);
  20. }
  21.  
  22. public void insertCustomer(Customer c) {
  23. dao.insertCustomer(c);
  24. }
  25.  
  26. /**
  27. * 批量保存, 编程式事务管理
  28. */
  29. public void saveCustomers(final List<Customer> list) {
  30. for(Customer c : list){
  31. this.insertCustomer(c);
  32. }
  33. }
  34.  
  35. public void updateCustomer(Customer c) {
  36. dao.updateCustomer(c);
  37. }
  38. }

sh.xml 配置文件

  1. <?xml version="1.0"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  11. http://www.springframework.org/schema/tx
  12. http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  13. http://www.springframework.org/schema/aop
  14. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
  15. <!-- 指定分散配置的文件的位置 -->
  16. <context:property-placeholder location="classpath:cn/itcast/spring/hibernate/tx25/jdbc.properties" />
  17. <!-- 配置c3p0数据源 -->
  18. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  19. <property name="driverClass" value="${jdbc.driverclass}" />
  20. <property name="jdbcUrl" value="${jdbc.url}" />
  21. <property name="user" value="${jdbc.username}" />
  22. <property name="password" value="${jdbc.password}" />
  23.  
  24. <property name="maxPoolSize" value="${c3p0.pool.size.max}" />
  25. <property name="minPoolSize" value="${c3p0.pool.size.min}" />
  26. <property name="initialPoolSize" value="${c3p0.pool.size.ini}" />
  27. <property name="acquireIncrement" value="${c3p0.pool.size.increment}" />
  28. </bean>
  29.  
  30. <!-- 本地回话工厂bean,spring整合hibernate的核心入口 -->
  31. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  32. <property name="dataSource" ref="dataSource" />
  33. <!-- 指定hibernate自身的属性 -->
  34. <property name="hibernateProperties">
  35. <props>
  36. <prop key="hibernate.dialect">${hibernate.dialect}</prop>
  37. <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
  38. <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
  39. </props>
  40. </property>
  41. <property name="mappingDirectoryLocations">
  42. <list>
  43. <value>classpath:cn/itcast/spring/domain</value>
  44. </list>
  45. </property>
  46. </bean>
  47.  
  48. <!-- customerDao -->
  49. <bean id="customerDao" class="cn.itcast.spring.hibernate.aspectj.CustomerDaoImpl">
  50. <property name="sf" ref="sessionFactory" />
  51. </bean>
  52.  
  53. <!-- hibernate事务管理器,在service层面上实现事务管理,而且达到平台无关性 -->
  54. <bean id="htm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  55. <property name="sessionFactory" ref="sessionFactory" />
  56. </bean>
  57.  
  58. <!-- customerService -->
  59. <bean id="customerService" class="cn.itcast.spring.hibernate.aspectj.CustomerServiceImpl">
  60. <property name="dao" ref="customerDao" />
  61. </bean>
  62.  
  63. <!-- 使用注解驱动 -->
  64. <tx:annotation-driven transaction-manager="htm"/>
  65. </beans>

Spring -- spring结合aop 进行 tx&aspectj事务管理配置方法的更多相关文章

  1. spring,mybatis事务管理配置与@Transactional注解使用[转]

    spring,mybatis事务管理配置与@Transactional注解使用[转] spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是至关 ...

  2. Spring整合Hibernate 二 - 声明式的事务管理

    Spring大战Hibernate之声明式的事务管理 Spring配置文件: 添加事务管理类的bean: <bean id="txManager" class="o ...

  3. spring + ibatis 多数据源事务(分布式事务)管理配置方法(转)

    spring + ibatis 多数据源事务(分布式事务)管理配置方法(转) .我先要给大家讲一个概念:spring 的多数据源事务,这是民间的说法.官方的说法是:spring 的分布式事务.明白了这 ...

  4. spring,mybatis事务管理配置与@Transactional注解使用

    spring,mybatis事务管理配置与@Transactional注解使用[转]   spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是 ...

  5. Spring事务管理配置以及异常处理

    Spring事务管理配置: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...

  6. spring 事物(三)—— 声明式事务管理详解

    spring的事务处理分为两种: 1.编程式事务:在程序中控制事务开始,执行和提交:详情请点此跳转: 2.声明式事务:在Spring配置文件中对事务进行配置,无须在程序中写代码:(建议使用) 我对&q ...

  7. Spring Boot 2.x基础教程:事务管理入门

    什么是事务? 我们在开发企业应用时,通常业务人员的一个操作实际上是对数据库读写的多步操作的结合.由于数据操作在顺序执行的过程中,任何一步操作都有可能发生异常,异常会导致后续操作无法完成,此时由于业务逻 ...

  8. [Spring-AOP-XML] 利用Spirng中的AOP和XML进行事务管理

    Spring中的AOP进行事务管理有三种方式 A.自定义事务切面 利用AspectJ来编写事务,我们一般把这个切面作用在service层中.其他代码在下面 编写一个Transaction实现类,通过S ...

  9. spring3.0事务管理配置

    转载:http://war-martin.iteye.com/blog/1396335 第一种配置方法:基于XML的事务管理 这种方法不需要对原有的业务做任何修改,通过在XML文件中定义需要拦截方法的 ...

随机推荐

  1. 【BZOJ2259】[Oibh]新型计算机 最短路

    [BZOJ2259][Oibh]新型计算机 Description Tim正在摆弄着他设计的“计算机”,他认为这台计算机原理很独特,因此利用它可以解决许多难题. 但是,有一个难题他却解决不了,是这台计 ...

  2. Java核心技术1

    Java方法参数的使用情况: 一个方法不能修改一个基本数据 对象析构与finalize方法 Java有自动的垃圾回收器,不需要人工回收内存,例如,文件或使用了系统资源的另一个对象的句柄.在这种情况下, ...

  3. GRPC使用错误排查记录

    1. 编译报错 f.fr.SetReuseFrames undefined (type *http2.Framer has no field or method SetReuseFrames) 该问题 ...

  4. mysql_表内容_操作

    1.增 语法:insert into 表 (列名,列名...) values (值,值...) # 插入单条数据 insert into 表 (列名,列名...) values (值,值...) # ...

  5. 翻页bug 在接口文档中应规范参数的取值区间

    <?php$a=array("red","green","blue","yellow","brown&q ...

  6. 获取文档版本版本值 滚动标识符 游标 控制查询如何执行 控制查询在哪些分片执行 boost加权

    映射mapping.json{ "book": { "_index": { "enabled": true }, "_id&quo ...

  7. 利用epoll实现异步IO

    之前异步IO一直没搞明白,大致的理解就是在一个大的循环中,有两部分:第一部分是监听事件:第二部分是处理事件(通过添加回调函数的方式).就拿网络通信来说,可以先通过调用 select 模块中的 sele ...

  8. Milking Time---poj3616(简单dp)

    题目链接:http://poj.org/problem?id=3616 题意:人从奶牛身上挤奶有m个时间段(1----n),每个时间段包含 s e f 表示从 s 到 e 的这段时间可以获得 f 单位 ...

  9. Learning How to Learn学习笔记(转)

    add by zhj: 工作中提高自己水平的最重要的一点是——快速的学习能力.这篇文章就是探讨这个问题的,掌握了快速学习能力的规律,你自然就有了快速学习能力了. 原文:Learning How to ...

  10. docker中制作自己的JDK+tomcat镜像

    方式一 首先,准备好想要的jdk和tomcat,另外,我们需要创建一个Dockerfile文件.下面展示一个Dockerfile文件的完整内容: FROM ubuntu:14.10 MAINTAINE ...