代理模式
代理模式作为23种经典设计模式之一,其比较官方的定义为“为其他对象提供一种代理以控制对这个对象的访问”,简单点说就是,之前A类自己做一件事,在使用代理之后,A类不直接去做,而是由A类的代理类B来去做。代理类其实是在之前类的基础上做了一层封装。java中有静态代理、JDK动态代理、CGLib动态代理的方式。静态代理指的是代理类是在编译期就存在的,相反动态代理则是在程序运行期动态生成的
静态代理 & 动态代理
1 静态代理
一个接口,两个实现类,代理实现类组合真实实现类
2 动态代理
JDK动态代理和CGlib字节码动态代理
简单转账功能
新建Maven项目名为“spring-aop“
准备数据
# 删除spring_aop数据库
drop database if exists spring_aop;
# 创建spring_aop数据库
create database spring_aop;
# 使用spring_aop数据库
use spring_aop;
# 创建account表
create table account (
id int(11) auto_increment primary key,
accountNum varchar(20) default NULL,
money int(8) default 0
);
# 新增数据
insert into account (accountNum, money) values
("622200001",1000),("622200002",1000);
导包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.13.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-dbutils/commons-dbutils -->
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
引入AOP(XML)
代码实现
删除事务代理工具类:TransactionProxyUtils.java
导入aspectjweaver包
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.3</version>
</dependency>
配置文件中添加 AOP 的相关配置
<!-- aop相关的节点配置 -->
<aop:config>
<!-- 切入点 表示哪些类的哪些方法在执行的时候会应用Spring配置的通知进行增强 -->
<aop:pointcut expression="execution ( * services.*.*(..))" id="pc"/>
<!-- 配置切面类的节点 作用主要就是整合通知和切入点 -->
<aop:aspect ref="transactionManager">
<aop:before method="beginTransaction" pointcut-ref="pc"/>
<aop:after-returning method="commit" pointcut-ref="pc"/>
<aop:after method="release" pointcut-ref="pc"/>
<aop:after-throwing method="rollback" pointcut-ref="pc"/>
</aop:aspect>
</aop:config>
XML改注解(AOP)
删除XML中的AOPXML配置并注解代理模式
<!-- 注解 开启代理模式 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
注释事务管理器类:TransactionManager.java
package transaction;
@Component
@Aspect
public class TransactionManager {
// 数据库连接工具类
@Autowired
private ConnectionUtils connectionUtils;
@Pointcut("execution(* services.*.*(..))")
private void transactionPointcut() {
}
/**
* 开启事务
*/
@Before("transactionPointcut()")
public void beginTransaction() {
try {
System.out.println("开启事务");
connectionUtils.getThreadConnection().setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 提交事务
*/
@AfterReturning("transactionPointcut()")
public void commit() {
try {
System.out.println("提交事务");
connectionUtils.getThreadConnection().commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 回滚事务
*/
@AfterThrowing("transactionPointcut()")
public void rollback() {
try {
System.out.println("回滚事务");
connectionUtils.getThreadConnection().rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 释放连接
*/
@After("transactionPointcut()")
public void release() {
try {
System.out.println("释放连接");
connectionUtils.getThreadConnection().close();
} catch (SQLException e) {
e.printStackTrace();
}
connectionUtils.removeConnection();
}
}
执行结果