1.程序运行类:
public class TestMain {
public static void main(String[] args) {
// UserService userService = new UserServiceImpl();
// UserDaoProxy userDaoProxy = new UserDaoProxy(userService);
// userDaoProxy.add();
// UserService o = (UserService)InvocationHandlerImpl.ClassLoader(new UserServiceImpl());
// o.add();
//使用ClassPathXmlApplicationContext 加载spring.xml文件
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring.xml");
//调用需要执行的方法
UserService userService = (UserService) classPathXmlApplicationContext.getBean("userServiceImpl");
userService.add();
}
}
2.AOP事务切面类:
将一些复用的代码块,使用AOP封装,保持代码的整洁,事务通知,不一一介绍
//切面类
@Aspect
@Component
public class AopLog {
//前置通知
@Before("execution(* com.shty.springtransaction.service..*.*(..))")
public void before(){
System.out.println("前置通知,在方法之前执行....");
}
// 运行通知
@AfterReturning("execution(* com.shty.springtransaction.service..*.*(..))")
public void returning() {
System.out.println("运行通知.....");
}
//后置通知
@After("execution(* com.shty.springtransaction.service..*.*(..))")
public void after(){
System.out.println("后置通知,在方法之后执行....");
}
}
3.AOP手动封装事务
环绕通知:得到请求的方法,保持事务出现异常,不会调用后面的代码。
异常通知:保证项目出现异常,异常通知可以获取到,则将当前事务进行回滚。
//aop 手动事务封装
@Component
@Aspect
public class AopTransactionUtils {
@Autowired
private transactionUtils transactionUtils;
// 环绕通知
@Around("execution(* com.shty.springtransaction.service..*.*(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕通知开始....开启事务");
TransactionStatus begin = transactionUtils.begin();
//调用方法,如果调用方法抛出异常,不会执行后面的代码
proceedingJoinPoint.proceed();
System.out.println("环绕通知结束.....提交事务");
transactionUtils.commit(begin);
}
// 异常通知
@AfterThrowing("execution(* com.shty.springtransaction.service..*.*(..))")
public void afterThrowing() {
System.out.println("异常通知....回滚事务");
//获取当前事务 直接回滚
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
}
4.service 与serviceImpl类
public interface UserService {
public void add();
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
public void add() {
//注意:在使用Spring事务的时候,service不需要try 将异常抛出给aop 异常通知接受回滚,如果要try 手动进行回滚。
// try {
System.out.println("往数据库添加数据");
// int i = 1/0;
userDao.add("test",18);
//}catch (Exception e){
//手动进行回滚
//TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//}
}
}
5.dao类,插入数据库方法
@Repository
public class UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void add(String name, Integer age) {
String sql = "INSERT INTO user(NAME, age) VALUES(?,?);";
int updateResult = jdbcTemplate.update(sql, name, age);
System.out.println("updateResult:" + updateResult);
}
6.spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.shty"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!-- 开启事物注解 -->
<!-- 1. 数据源对象: C3P0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!-- 2. JdbcTemplate工具类实例 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 3.配置事务 -->
<bean id="dataSourceTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
7.pom.xml文件
<dependencies>
<!-- 引入Spring-AOP等相关Jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.1_2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
</dependencies>