前面记录了前置通知,后置通知,异常通知,最终通知,这一篇博客讲记录最终通知。
前,后,异,最 ,通知博客链接地址
最终通知的包括了前,后,异,最 通知,它通常和try - catch - finally联合使用,try里面控制的目标对象是否执行,在方法执行之前当前置通知,在方法执行之后被当后置通知,在catch里面被称为异常通知,finally里的通知被称为最终通知。
try{
环绕前置
目标方法执行
环绕后置
}catch{
环绕异常
}finally{
环绕最终 }
最终通知的使用
- 导入jar包 , 或者Maven依赖
<dependencies>
<!--aop 需要的包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!--spring 核心包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<!-- 日志包-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
- 编写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">
<!-- 扫描com.stone.dome包下的所有注解 -->
<context:component-scan base-package="com.stone.aopdome"/>
<!-- 开启基于注解的aop 功能-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
- 写业务类
接口
@Component
public interface Operation
{
// 加法
double add(double i,double j);
// 减法
double subtract(double i,double j);
// 乘法
double mul(double i,double j);
// 除法
double div(double i,double j);
}
实现类
@Component
public class OperationImpl implements Operation
{
@Override
public double add(double i, double j)
{
return i+j;
}
@Override
public double subtract(double i, double j)
{
return i-j;
}
@Override
public double mul(double i, double j)
{
return i*j;
}
@Override
public double div(double i, double j)
{
return i/j;
}
}
- 写通知类
@Aspect //声明该类是一个切面类
@Component
public class OperationProxy
{
// execution切入点表达式 (访问修饰符 返回值类型 目标执行的方法)
@Around("execution(public double com.stone.aopdome.operation.impl.OperationImpl.add(double,double))")
public Object logAround(ProceedingJoinPoint pjp) throws Throwable
{
Object result = null;
try
{
System.out.println("环绕通知模拟的【前置通知】");
result = pjp.proceed(); //控制着目标方法的执行,相当于目标方法是否执行,该方法的返回值就是目标方法得到返回值
System.out.println("环绕通知模拟的【后置通知】 方法的返回值为 "+result+"\t目标方法"+pjp.getSignature().getName()+"\t参数个数 "+pjp.getArgs().length);
}
catch (Exception e)
{
System.out.println("环绕通知模拟的【异常置通知】"+e);
}
finally
{
System.out.println("环绕通知模拟的【最终置通知】");
}
return result;
}
}
- 测试
// 测试环绕通知
@Test
public void testaop2(){
Operation bean = ioc.getBean(Operation.class);
double subtract = bean.subtract(56.0, 20.0);
System.out.println(subtract);
}
执行结果
环绕通知和其他四类通知的执行顺序:
环绕前置 – 普通前置 --目标方法执行 --环绕后置/环绕异常 – 环绕最终 – 配置后置 – 普通返回/普通异常