本文针对 AOP 的概念作用进行了分析,并且对基于 XML 与注解两种方式配置 AOP 进行了解读。
一、AOP 概述
1.1 什么是 AOP
简单的说它就是把我们程序重复的代码抽取出来,在需要执行的时候,使用动态代理的技术,在不修改源码的基础上,对我们的已有方法进行增强。
1.2 AOP 的作用及优势
- 作用: 在程序运行期间,不修改源码对已有方法进行增强。
- 优势: 减少重复代码;提高开发效率;维护方便
1.3 AOP 的实现方式
动态代理
1.4 Spring 中 AOP 的 相关术语
-
连接点: 所谓连接点就是指那些被拦截的点,即业务层中所有的方法。
-
切入点: 所谓切入点就是指我们要对哪些连接点进行拦截,即业务层中被增强的方法例如:
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if("test".equals(method.getName())){ return method.invoke(accountService, args); } //...增强操作... } //业务层 void test();//是连接点,不是切入点。因为方法被拦截,没有被增强。
-
通知:前置通知、后置通知、异常通知、最终通知、环绕通知
-
Introduction(引介): 引介是一种特殊的通知在不修改类代码的前提下,Introduction可以在运行期为类动态地添加一些方法或Fields。(了解)
-
Target(目标对象): 代理的目标对象。
-
weaving(织入): 是指把增强应用到目标对象来创建新的代理对象的 过程 。spring采用动态代理织入,而AspectJ采用编译期织入和类装载期织入。
-
Proxy(代理): 一个类被 AOP 织入增强后,就产生一个结果代理类。
-
Aspect(切面): 是切入点和通知(引介)的结合。
1.5 AOP 要明确做的事
- 开发阶段(我们做的)
编写核心业务代码(开发主线):要求熟悉业务需求
把公用代码抽取出来,制作成通知。
在配置文件中,声明切入点与通知间的关系,即切面。 - 运行阶段(Spring完成的)
Spring 框架监控切入点方法的执行。一旦监控到切入点方法被运行,使用代理机制,动态创建目标对象的代理对象。根据通知类别,在代理对象的对应位置,将通知对应的功能织入,完成完整的代码逻辑运行。
二、Spring 中基于 XML 的 AOP 配置步骤 ★
为了演示 AOP 配置步骤,建立一个工程。工程目录如下:
2.1 工程准备
导入依赖:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
</dependencies>
账户业务层及其实现类代码:
public interface IAccountService {
void saveAccount();
void updateAccount(int i);
int deleteAccount();
}
public class AccountServiceImpl implements IAccountService {
public void saveAccount() {
System.out.println("保存了账户");
}
public void updateAccount(int i) {
System.out.println("更新了账户");
}
public int deleteAccount() {
System.out.println("删除了账户");
return 0;
}
}
日志的工具类:
/**
* 用于记录日志的工具类,它里面提供了公共的代码
*/
public class Logger {
/**
* 用于打印日志:计划让其在切入点方法执行之前执行(切入点方法就是业务层方法)
*/
public void printLog(){
System.out.println("Logger类中的printLog方法开始执行了...");
}
}
bean.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置spring的ioc, service对象配置进来-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>
</beans>
2.2 配置 AOP 步骤
-
把通知 Bean 交给 spring 来管理
<!--配置Logger类--> <bean id="logger" class="com.itheima.utils.Logger"></bean>
-
使用 aop:config 标签标明开始 AOP 的配置
<!--配置aop--> <aop:config> </aop:config>
-
使用 aop:aspect 标签标明配置切面
id 属性: 是给切面提供一个唯一标识,一般使用 logAdvice 作为 id 值
ref 属性: 是指定通知类 bean 的 id<!--配置aop--> <aop:config> <!--配置切面--> <aop:aspect id="logAdvice" ref="logger"> </aop:aspect> </aop:config>
-
在 aop:aspect 标签的内部使用对应标签来配置通知的类型
我们现在示例是让 printLog 方法在切入点执行之前执行:所以是前置通知。 aop:before: 表示配置前置通知
method 属性: 用于指定 logger 类中那个方法是前置通知
pointcut 属性: 用于指定切入点表达式,该表达式的含义指的是对业务层中哪些方法增强(建立前置通知和业务层之间的关系)<!--配置aop--> <aop:config> <!--配置切面--> <aop:aspect id="logAdvice" ref="logger"> <!--配置通知的类型,并且建立通知方法和切入点方法的关联--> <aop:before method="printLog" pointcut="execution( * com.itheima.service.impl.*.*(..))"></aop:before> </aop:aspect> </aop:config>
2.3 切入点表达式的写法
关键字: execution(表达式)
表达式: 访问修饰符 返回值 包名.包名.包名…类名.方法名(参数列表)
-
标准的表达式写法
public void com.ithiema.service.impl.AccountServiceImpl.saveAccount()
-
访问修饰符可以省略
void com.ithiema.service.impl.AccountServiceImpl.saveAccount()
-
返回值可以使用通配符,表示任意返回值
* com.ithiema.service.impl.AccountServiceImpl.saveAccount()
-
包名可以使用通配符,表示任意包。但是有几级包,就需要写几个 *.
* *.*.*.*.AccountServiceImpl.saveAccount()
-
包名可以使用…表示当前包及其子包
* *..AccountServiceImpl.saveAccount()
-
类名和方法名都可以使用 * 来实现通配
* *..*.*()
-
参数列表:
- 可以直接写数据类型:
基本类型直接写名称,int。如:* *…AccountServiceImpl.saveAccount(int)
引用类型写包名.类名的方式,如:java.lang.String - 可以使用 通配符 表示任意类型,但是必须有参数:
可以使用…表示有无参数均可,有参数可以是任意类型。如:* *…AccountServiceImpl.saveAccount(…)
- 全通配写法:
* ….*(…)
- 可以直接写数据类型:
2.3.1 切入点表达式的通常写法
实际开发中切入点表达式的通常写法:切到业务层实现类下的所有方法:
* com.itheima.service.impl.*.*(..)
配置完成后的 bean.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置spring的ioc, service对象配置进来-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>
<!--配置Logger类-->
<bean id="logger" class="com.itheima.utils.Logger"></bean>
<!--配置aop-->
<aop:config>
<!--配置切面-->
<aop:aspect id="logAdvice" ref="logger">
<!--配置通知的类型,并且建立通知方法和切入点方法的关联-->
<aop:before method="printLog" pointcut="execution( * com.itheima.service.impl.*.*(..))"></aop:before>
</aop:aspect>
</aop:config>
</beans>
测试方法:
public class AOPTest {
public static void main(String[] args) {
//1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2. 获取对象
IAccountService as = (IAccountService) ac.getBean("accountService");
//3. 执行方法
as.saveAccount();
as.deleteAccount();
as.updateAccount(1);
}
}
执行结果:
2.3.2 通用化切入点表达式
当我们对切入点表达式重复的引用时,配置会显得很冗长,因此可利用配置切入点表达式解决该问题。
<aop:pointcut id="pt1" expression="execution( * com.itheima.service.impl.*.*(..))"></aop:pointcut>
- 配置切入点表达式 id 的属性用于指定表达式的唯一标识。expression 属性用于指定表达式内容
- 此标签既可以写在 aop:aspect 内部,也可以写在外部。
- 标签写在内部只能当前切面使用,写在外面代表所有切面可用,但是一定要放在aop:config标签的最顶部
标签写在 aop:aspect 内部:
<!--配置aop-->
<aop:config>
<!--配置切面-->
<aop:aspect id="logAdvice" ref="logger">
<!--配置前置通知:在切入点方法执行之前执行-->
<aop:before method="beforePrintLog" pointcut-ref="pt1"></aop:before>
<aop:pointcut id="pt1" expression="execution( * com.itheima.service.impl.*.*(..))"></aop:pointcut>
</aop:aspect>
</aop:config>
标签写在 aop:aspect 外部:
<!--配置aop-->
<aop:config>
<!--配置切入点表达式-->
<aop:pointcut id="pt1" expression="execution( * com.itheima.service.impl.*.*(..))"></aop:pointcut>
<!--配置切面-->
<aop:aspect id="logAdvice" ref="logger">
<!--配置前置通知:在切入点方法执行之前执行-->
<aop:before method="beforePrintLog" pointcut-ref="pt1"></aop:before>
</aop:aspect>
</aop:config>
2.4 四种常用类型通知
日志的工具类:
/**
* 用于记录日志的工具类,它里面提供了公共的代码
*/
public class Logger {
/**
* 前置通知
*/
public void beforePrintLog(){
System.out.println("前置通知Logger类中的printLog方法开始执行了...");
}
/**
* 后置通知
*/
public void afterReturningPrintLog(){
System.out.println("后置通知Logger类中的printLog方法开始执行了...");
}
/**
* 异常通知
*/
public void afterThrowingPrintLog(){
System.out.println("异常通知Logger类中的printLog方法开始执行了...");
}
/**
* 最终通知
*/
public void afterPrintLog(){
System.out.println("最终通知Logger类中的printLog方法开始执行了...");
}
}
bean.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置spring的ioc, service对象配置进来-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>
<!--配置Logger类-->
<bean id="logger" class="com.itheima.utils.Logger"></bean>
<!--配置aop-->
<aop:config>
<!--配置切面-->
<aop:aspect id="logAdvice" ref="logger">
<!--配置前置通知:在切入点方法执行之前执行-->
<aop:before method="beforePrintLog" pointcut-ref="pt1"></aop:before>
<!--配置后置通知:在切入点之后正常方法之后执行,他和异常通知永远只能执行一个-->
<aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt1"></aop:after-returning>
<!--配置异常通知:在切入点之后异常方法之后执行,他和异常通知永远只能执行一个-->
<aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"></aop:after-throwing>
<!--配置最终通知:无论切入点方法是否正常执行,他都会在其后面执行-->
<aop:after method="afterPrintLog" pointcut-ref="pt1"></aop:after>
<!--在切面内部配置切入点表达式-->
<aop:pointcut id="pt1" expression="execution( * com.itheima.service.impl.*.*(..))"></aop:pointcut>
</aop:aspect>
</aop:config>
</beans>
测试结果:
2.5 环绕通知
Spring 为我们提供了一个接口,ProceedingJoinPoint。该接口有一个方法 proceed(),此方法就相当于明确调用切入点方法。
该接口可以作为环绕通知的方法参数,在程序执行时,spring框架会为我们提供该接口的实现类供我们使用。
Spring 中的环绕通知: 它是 Spring 框架为我们提供的一种可以在代码中手动增强方法和执行的方式。
在 Logger 中编写以下代码:
/**
* 环绕通知
*/
public Object aroundPrintLog(ProceedingJoinPoint pjp){
Object rtValue = null;
try {
Object[] args = pjp.getArgs(); // 固定写法
System.out.println("Logger类中aroundPrintLog方法开始记录日志了。。前置");
rtValue = pjp.proceed(args); // 固定写法
System.out.println("Logger类中aroundPrintLog方法开始记录日志了。。后置");
return rtValue;
} catch (Throwable throwable) {
System.out.println("Logger类中aroundPrintLog方法开始记录日志了。。异常");
throw new RuntimeException(throwable);
} finally {
System.out.println("Logger类中aroundPrintLog方法开始记录日志了。。最终");
}
}
注意:catch 的异常只能是 Throwable,其他异常获取不到。
bean.xml:
<!--配置aop-->
<aop:config>
<!--配置切面-->
<aop:aspect id="logAdvice" ref="logger">
<!-- 配置环绕通知 -->
<aop:around method="aroundPrintLog" pointcut-ref="pt1"></aop:around>
<!--配置切入点表达式-->
<aop:pointcut id="pt1" expression="execution( * com.itheima.service.impl.*.*(..))"></aop:pointcut>
</aop:aspect>
</aop:config>
测试方法结果:
三、Spring 中基于注解的 AOP 配置步骤(了解)
为了演示基于注解的AOP配置步骤,我们创建一个新的工程,工程目录如下:
3.1 工程准备
bean.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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">
<!--配置spring创建容器是要扫描的包-->
<context:component-scan base-package="com.itheima"></context:component-scan>
<!--开启spring开启Aop的支持-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
账户业务层及其实现类:
public interface IAccountService {
void saveAccount();
}
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
public void saveAccount() {
System.out.println("保存了账户");
}
}
3.1 四种常用类型通知与环绕通知
@Component("logger")
@Aspect // 表示当前类是一个切面类
public class Logger {
@Pointcut("execution( * com.itheima.service.impl.*.*(..))")
private void pt1(){}
/**
* 前置通知
*/
@Before("pt1()")
public void beforePrintLog(){
System.out.println("前置通知Logger类中的printLog方法开始执行了...");
}
/**
* 后置通知
*/
@AfterReturning("pt1()")
public void afterReturningPrintLog(){
System.out.println("后置通知Logger类中的printLog方法开始执行了...");
}
/**
* 异常通知
*/
@AfterThrowing("pt1()")
public void afterThrowingPrintLog(){
System.out.println("异常通知Logger类中的printLog方法开始执行了...");
}
/**
* 最终通知
*/
@After("pt1()")
public void afterPrintLog(){
System.out.println("最终通知Logger类中的printLog方法开始执行了...");
}
}
测试方法:
public static void main(String[] args) {
//1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2. 获取对象
IAccountService as = (IAccountService) ac.getBean("accountService");
//3. 执行方法
as.saveAccount();
}
从结果中看出,后置通知与最终通知顺序有所不同,这个是 Spring 内部的问题。解决该问题的方法是使用环绕通知,我们手动增强方法和执行的方式。
3.2 环绕通知类型
@Component("logger")
@Aspect//表示当前类是一个切面类
public class Logger {
@Pointcut("execution( * com.itheima.service.impl.*.*(..))")
private void pt1(){}
/**
* 环绕通知
*/
@Around(("pt1()"))
public Object aroundPrintLog(ProceedingJoinPoint pjp){
Object rtValue = null;
try {
Object[] args = pjp.getArgs();
System.out.println("Logger类中aroundPrintLog方法开始记录日志了。。前置");
rtValue = pjp.proceed(args);
System.out.println("Logger类中aroundPrintLog方法开始记录日志了。。后置");
return rtValue;
} catch (Throwable throwable) {
System.out.println("Logger类中aroundPrintLog方法开始记录日志了。。异常");
throw new RuntimeException(throwable);
} finally {
System.out.println("Logger类中aroundPrintLog方法开始记录日志了。。最终");
}
}
}
方法执行结果如下:
四、应用 AOP 对转账方法事务的优化(了解)
上一讲对于转账事务提出了一种解决方案,具体实现方式可查看右侧链接: Spring 从入门到精通系列 09 —— 转账方法的事务问题与动态代理
但是事务控制的比较冗余,现可通过 AOP 对于动态代理的方式进行优化。
4.1 方案一:xml 配置环绕通知
工程目录:
bean.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- (7) 配置代理的service
<bean id="proxyAccountService" factory-bean="beanFactory" factory-method="getAccountService" />-->
<!-- (6) 配置beanFactory -->
<bean id="beanFactory" class="com.axy.factory.BeanFactory">
<!-- 注入service
<property name="accountService" ref="accountService" />-->
<!-- 注入事务管理器 -->
<property name="txManager" ref="txManager" />
</bean>
<!-- 配置Service -->
<bean id="accountService" class="com.axy.service.impl.AccountServiceImpl">
<!-- 注入dao -->
<property name="accountDao" ref="accountDao" />
</bean>
<!--配置Dao对象-->
<bean id="accountDao" class="com.axy.dao.impl.AccountDaoImpl">
<!-- 注入QueryRunner -->
<property name="runner" ref="runner" />
<!-- (3) 注入ConnectionUtils -->
<property name="connectionUtils" ref="connectionUtils" />
</bean>
<!--配置QueryRunner-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype" />
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--连接数据库的必备信息-->
<property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatisdb?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=CST" />
<property name="user" value="root" />
<property name="password" value="123456" />
</bean>
<!-- (2) 配置 connectionUtils 工具类 -->
<bean id="connectionUtils" class="com.axy.utils.ConnectionUtils">
<!--注入数据源-->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- (4) 配置事务管理器 -->
<bean id="txManager" class="com.axy.utils.TransactionManager">
<!-- (5) 注入ConnectionUtils -->
<property name="connectionUtils" ref="connectionUtils" />
</bean>
<!-- new: 配置AOP-->
<aop:config>
<!--配置切面-->
<aop:aspect id="workAdvice" ref="beanFactory">
<aop:around method="getAccountService" pointcut="execution(* com.axy.service.impl.*.*(..))" />
</aop:aspect>
</aop:config>
</beans>
BeanFactory.java:
/**
* 用于创建 Service 的代理对象的工厂
*/
public class BeanFactory {
private TransactionManager txManager;
public void setTxManager(TransactionManager txManager) {
this.txManager = txManager;
}
public Object getAccountService(ProceedingJoinPoint pjp){
Object rtValue = null;
try {
Object[] args = pjp.getArgs();
// 1. 开启事务
txManager.beginTransaction();
// 2. 执行操作
rtValue = pjp.proceed(args);
//3.提交事务
txManager.commit();
//4.返回结果
return rtValue;
}catch (Throwable t){
//5.回滚操作
txManager.rollback();
throw new RuntimeException(t);
}finally {
//6.释放连接
txManager.release();
}
}
}
测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest {
@Autowired
private IAccountService as;
@Test
public void testTransfer(){
as.transfer("aaa","bbb",100f);
}
}
测试结果:
4.2 方案二:xml 配置四种常用通知
这种方案不会将 BeanFactory 加入 IOC 容器,而是直接指定事务管理器。
bean.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置Service -->
<bean id="accountService" class="com.axy.service.impl.AccountServiceImpl">
<!-- 注入dao -->
<property name="accountDao" ref="accountDao" />
</bean>
<!--配置Dao对象-->
<bean id="accountDao" class="com.axy.dao.impl.AccountDaoImpl">
<!-- 注入QueryRunner -->
<property name="runner" ref="runner" />
<!-- (3) 注入ConnectionUtils -->
<property name="connectionUtils" ref="connectionUtils" />
</bean>
<!--配置QueryRunner-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype" />
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--连接数据库的必备信息-->
<property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatisdb?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=CST" />
<property name="user" value="root" />
<property name="password" value="123456" />
</bean>
<!-- (2) 配置 connectionUtils 工具类 -->
<bean id="connectionUtils" class="com.axy.utils.ConnectionUtils">
<!--注入数据源-->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- (4) 配置事务管理器 -->
<bean id="txManager" class="com.axy.utils.TransactionManager">
<!-- (5) 注入ConnectionUtils -->
<property name="connectionUtils" ref="connectionUtils" />
</bean>
<!-- new: 配置AOP-->
<aop:config>
<aop:pointcut id="pt1" expression="execution(* com.axy.service.impl.*.*(..))" />
<!--配置切面-->
<aop:aspect id="workAdvice" ref="txManager">
<aop:before method="beginTransaction" pointcut-ref="pt1" />
<aop:after-returning method="commit" pointcut-ref="pt1" />
<aop:after-throwing method="rollback" pointcut-ref="pt1" />
<aop:after method="release" pointcut-ref="pt1" />
</aop:aspect>
</aop:config>
</beans>
4.3 方案三:xml 和注解配置四种通知
不用 beanFactory 的形式,并且将 service 和 dao 放入 IOC 容器。
bean.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: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/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--注解包扫描-->
<context:component-scan base-package="com.axy" />
<!--配置QueryRunner-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype" />
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--连接数据库的必备信息-->
<property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatisdb?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=CST" />
<property name="user" value="root" />
<property name="password" value="123456" />
</bean>
<!-- new: 配置AOP-->
<aop:config>
<aop:pointcut id="pt1" expression="execution(* com.axy.service.impl.*.*(..))" />
<!--配置切面-->
<aop:aspect id="workAdvice" ref="txManager">
<aop:before method="beginTransaction" pointcut-ref="pt1" />
<aop:after-returning method="commit" pointcut-ref="pt1" />
<aop:after-throwing method="rollback" pointcut-ref="pt1" />
<aop:after method="release" pointcut-ref="pt1" />
</aop:aspect>
</aop:config>
</beans>
其他类:
@Service("accountService")
public class AccountServiceImpl implements IAccountService{
@Autowired
private IAccountDao accountDao;
...
}
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
@Autowired
private QueryRunner runner;
@Autowired
private ConnectionUtils connectionUtils;
...
}
@Component("txManager")
public class TransactionManager {
@Autowired
private ConnectionUtils connectionUtils;
...
}
4.4 方案四:xml 和注解配置环绕通知
bean.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: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/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--注解包扫描-->
<context:component-scan base-package="com.axy" />
<!--配置QueryRunner-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype" />
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--连接数据库的必备信息-->
<property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatisdb?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=CST" />
<property name="user" value="root" />
<property name="password" value="123456" />
</bean>
<!-- new: 配置AOP-->
<aop:config>
<!--配置切面-->
<aop:aspect id="workAdvice" ref="txManager">
<aop:around method="getAccountService" pointcut="execution(* com.axy.service.impl.*.*(..))" />
</aop:aspect>
</aop:config>
</beans>
@Component("txManager")
public class TransactionManager {
@Autowired
private ConnectionUtils connectionUtils;
public Object aroundAdvice(ProceedingJoinPoint pjp){
Object rtValue = null;
try {
Object[] args = pjp.getArgs();
// 1. 开启事务
this.beginTransaction();
// 2. 执行操作
rtValue = pjp.proceed(args);
//3.提交事务
this.commit();
//4.返回结果
return rtValue;
}catch (Throwable t){
//5.回滚操作
this.rollback();
throw new RuntimeException(t);
}finally {
//6.释放连接
this.release();
}
}
}
另外,使用注解配置切面只可配置环绕通知,配置四种通知,由于提交事务和释放连接事务顺序不对,会出现异常。
本文针对 AOP 的概念作用进行了分析,并且对基于 XML 与注解两种方式配置 AOP 进行了解读,并对之前案例进行优化,如果大家对文章内容还存在一些疑问,欢迎大家在评论区留言哦~