Spring中的环绕通知
aop:around:
作用:
用于配置环绕通知
属性:
method:指定通知中方法的名称。
pointct:定义切入点表达式
pointcut-ref:指定切入点表达式的引用
说明:
它是 spring 框架为我们提供的一种可以在代码中手动控制增强代码什么时候执行的方式
注意: 通常情况下,环绕通知都是独立使用的
* 环绕通知
* spring 框架为我们提供了一个接口:ProceedingJoinPoint,它可以作为环绕通知的方法参数。
* 在环绕通知执行时,spring 框架会为我们提供该接口的实现类对象,我们直接使用就行。
* 它是 spring 框架为我们提供的一种可以在代码中手动控制增强代码什么时候执行的方式。
* 通常情况下,环绕通知都是独立使用的
用于记录日志的工具类,里面提供了公共的代码
package com.utils;
import org.aspectj.lang.ProceedingJoinPoint;
//用于记录日志的工具类,里面提供了公共的代码
public class Logger {
/*
* 前置通知
* */
public void beforePrintLog(){
System.out.println("前置通知Logger类中的beforePrintLog方法开始记录日志了。。。");
}
/*
* 后置通知
* */
public void afterReturningPrintLog(){
System.out.println("后置通知Logger类中的afterReturningPrintLog方法开始记录日志了。。。");
}
/*
* 异常通知
* */
public void afterThrowingPrintLog(){
System.out.println("异常通知Logger类中的afterThrowingPrintLog方法开始记录日志了。。。");
}
/*
* 最终通知
* */
public void afterPrintLog(){
System.out.println("最终通知Logger类中的afterPrintLog方法开始记录日志了。。。");
}
/*
* 环绕通知
* spring 框架为我们提供了一个接口:ProceedingJoinPoint,它可以作为环绕通知的方法参数。
* 在环绕通知执行时,spring 框架会为我们提供该接口的实现类对象,我们直接使用就行。
* 它是 spring 框架为我们提供的一种可以在代码中手动控制增强代码什么时候执行的方式。
* 通常情况下,环绕通知都是独立使用的
* */
public Object aroundPrintLog(ProceedingJoinPoint pjp){
//定义返回值
Object rtValue = null;
try {
Object[] args = pjp.getArgs(); // 获取方法执行所需的参数
System.out.println("环绕通知Logger类中的aroundPrintLog方法开始记录日志了。。。前置"); //前置通知:开启事务
System.out.println("环绕通知Logger类中的aroundPrintLog方法开始记录日志了。。。后置");//后置通知
rtValue = pjp.proceed(args);//明确调用业务层方法(切入点方法)
return rtValue;
}catch(Throwable e) {
System.out.println("环绕通知Logger类中的aroundPrintLog方法开始记录日志了。。。异常");// 异常通知
throw new RuntimeException(e);
}finally {
System.out.println("环绕通知Logger类中的aroundPrintLog方法开始记录日志了。。。最终");//最终通知
}
}
}
配置文件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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置spring的IOC-->
<bean id="accountService" class="com.service.AccountServiceImpl"></bean>
<bean id="logger" class="com.utils.Logger"></bean>
<!-- 配置AOP-->
<aop:config>
<!--配置切点,在aspect外配置切点,此切点可为所以切面可用-->
<aop:pointcut id="pt1" expression="execution(* com.service.*.*(..))"/>
<!--配置切面-->
<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:around method="aroundPrintLog" pointcut-ref="pt1"></aop:around>
</aop:aspect>
</aop:config>
</beans>
账户的业务层接口
package com.service;
/*
* 账户的业务层接口
* */
public interface IAccountService {
/*
* 模拟保存账户
* */
public void saveAccount();
/*
* 模拟更新账户
* */
public void updateAccount(int i);
/*
* 模拟删除账户
* */
public int deleteAccount();
}
账户的业务层实现类
package com.service;
//账户的业务层实现类
public class AccountServiceImpl implements IAccountService {
public void saveAccount() {
System.out.println("执行了保存账户。。。");
}
public void updateAccount(int i) {
System.out.println("执行了更新账户。。。"+i);
}
public int deleteAccount() {
System.out.println("执行了删除账户。。。");
return 0;
}
}
测试类
import com.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService accountService = (IAccountService) ac.getBean("accountService");
accountService.saveAccount();
}
}
执行结果: