使用spring提供的事务处理机制的好处是程序员可以不用关心事务的切面了,只要配置就好了,可以少写代码。
spring声明式事务处理
spring
声明:针对的是程序员,程序员告诉spring容器,哪些方法需要事务,哪些方法不需要事务
事务处理 spring容器来做事务处理
目的:让spring管理事务,开发者不再关注事务
spring声明式事务处理的步骤:
1、搭建环境
2、把dao层和service层的接口和类写完
3、在spring的配置文件中,先导入dataSource
4、测试
5、导入dao和service层的bean
6、测试
7、进行AOP的配置
1、引入事务管理器
2、进行aop的配置
8、测试service层的类看是否是代理对象
例子
Person.java
1 package cn.itcast.spring.jdbc.transaction; 2 3 public class Person { 4 private Long pid; 5 private String pname; 6 private String psex; 7 public Long getPid() { 8 return pid; 9 } 10 public void setPid(Long pid) { 11 this.pid = pid; 12 } 13 public String getPname() { 14 return pname; 15 } 16 public void setPname(String pname) { 17 this.pname = pname; 18 } 19 public String getPsex() { 20 return psex; 21 } 22 public void setPsex(String psex) { 23 this.psex = psex; 24 } 25 26 }
MyPersonDao.java
1 package cn.itcast.spring.jdbc.transaction; 2 import org.springframework.jdbc.core.support.JdbcDaoSupport; 3 public class MyPersonDao extends JdbcDaoSupport{ 4 public void savePerson() 5 { 6 this.getJdbcTemplate().execute("insert into person(pname,psex) values(‘bbbbbb‘,‘aaaa‘)"); 7 int i=1/0; 8 } 9 }
PersonService.java
1 package cn.itcast.spring.jdbc.transaction; 2 3 import cn.itcast.spring.jdbc.PersonDao; 4 5 public class PersonService { 6 private MyPersonDao dao=new MyPersonDao(); 7 8 public MyPersonDao getDao() { 9 return dao; 10 } 11 12 public void setDao(MyPersonDao dao) { 13 this.dao = dao; 14 } 15 16 public void savePerson() 17 { 18 dao.savePerson(); 19 20 } 21 }
MyException.java
1 package cn.itcast.spring.jdbc.transaction; 2 3 public class MyException { 4 public void defineException(Throwable ex){ 5 System.out.println(ex.getMessage()); 6 } 7 8 }
jdbc.properties
1 jdbc.driverClassName=com.mysql.jdbc.Driver 2 jdbc.url=jdbc\:mysql\://localhost\:3306/hibernate_basic 3 jdbc.username=root 4 jdbc.password=friends
重点 applicationContext-spring_jdbc_transaction.xml
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 3 xmlns:tx="http://www.springframework.org/schema/tx" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 6 http://www.springframework.org/schema/aop 7 http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 8 http://www.springframework.org/schema/tx 9 http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 10 <bean 11 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 12 <property name="locations"> 13 <value>classpath:jdbc.properties</value> 14 </property> 15 </bean> 16 17 18 <bean id="dataSource" destroy-method="close" 19 class="org.apache.commons.dbcp.BasicDataSource"> 20 <property name="driverClassName" value="${jdbc.driverClassName}" /> 21 <property name="url" value="${jdbc.url}" /> 22 <property name="username" value="${jdbc.username}" /> 23 <property name="password" value="${jdbc.password}" /> 24 </bean> 25 26 <bean id="personDao" class="cn.itcast.spring.jdbc.transaction.MyPersonDao"> 27 <property name="dataSource"> 28 <ref bean="dataSource" /> 29 </property> 30 </bean> 31 <bean id="serviceDao" class="cn.itcast.spring.jdbc.transaction.PersonService"> 32 <property name="dao"> 33 <ref bean="personDao"/> 34 </property> 35 </bean> 36 37 <bean id="myException" class="cn.itcast.spring.jdbc.transaction.MyException"></bean> 38 <!-- spring提供的事务管理器, --> 39 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 40 <property name="dataSource"> 41 <ref bean="dataSource"/> 42 </property> 43 </bean> 44 <!-- 45 通知 1、告诉spring容器,采用什么样的方法处理事务 2、告诉spring容器,目标方法应该采用什么样的事务处理策略 46 --> 47 <tx:advice id="tx" transaction-manager="transactionManager"> 48 <tx:attributes> 49 <!-- 50 save开头的函数名 name规定方法 isolation 默认值为DEFAULT propagation 传播机制 REQUIRED 51 --> 52 <tx:method name="save*" read-only="false" /> 53 </tx:attributes> 54 </tx:advice> 55 <!-- 本来事务由程序员自己写并且当切面放入,但是这里spring提供了事务处理的通知方法,所以不用程序员写切面了 --> 56 <aop:config > 57 <aop:pointcut expression="execution(* cn.itcast.spring.jdbc.transaction.PersonService.*(..))" id="perform"/> 58 <aop:advisor advice-ref="tx" pointcut-ref="perform" /> 59 <!-- 指定了切面和通知 --> 60 <aop:aspect ref="myException"> 61 <aop:after-throwing method="defineException" pointcut-ref="perform" throwing="ex"/> 62 </aop:aspect> 63 </aop:config> 64 </beans>
测试
1 package cn.itcast.spring.jdbc.transaction; 2 3 import org.junit.Test; 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 7 import cn.itcast.spring.jdbc.PersonDao; 8 import cn.itcast.spring.jdbc.PersonDao2; 9 import cn.itcast.spring.jdbc.PersonDao3; 10 11 public class test { 12 @Test 13 public void testPersonDao1() 14 { 15 ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext-spring_jdbc_transaction.xml"); 16 PersonService dao=(PersonService) context.getBean("serviceDao"); 17 dao.savePerson(); 18 } 19 20 }