切面优先级问题
如果同一个方法,有多个切面在同一个切入点切入,那么执行的优先级顺序如何控制.
基本语法: @Order(value=n)来控制n值越小,优先级越高.
package com.elf.spring.aop.aspectj;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.Arrays;
public class SmartAnimalAspect3 {
@Before(value = "execution(public float com.elf.spring.aop.aspectj.SmartDog.getSum(float, float))")
public void showBeginLog(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
System.out.println("SmartAnimalAspect3-切面类showBeginLog()-方法执行前-日志-方法名-" + signature.getName() + "-参数 "
+ Arrays.asList(joinPoint.getArgs()));
}
@AfterReturning(value = "execution(public float com.elf.spring.aop.aspectj.SmartDog.getSum(float, float))", returning = "res")
public void showSuccessEndLog(JoinPoint joinPoint, Object res) {
Signature signature = joinPoint.getSignature();
System.out.println("SmartAnimalAspect3-切面类showSuccessEndLog()-方法执行正常结束-日志-方法名-" + signature.getName() + " 返回的结果是=" + res);
}
@AfterThrowing(value = "execution(public float com.elf.spring.aop.aspectj.SmartDog.getSum(float, float))", throwing = "throwable")
public void showExceptionLog(JoinPoint joinPoint, Throwable throwable) {
Signature signature = joinPoint.getSignature();
System.out.println("SmartAnimalAspect3-切面类showExceptionLog()-方法执行异常-日志-方法名-" + signature.getName() + " 异常信息=" + throwable);
}
@After(value = "execution(public float com.elf.spring.aop.aspectj.SmartDog.getSum(float, float))")
public void showFinallyEndLog(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
System.out.println("SmartAnimalAspect3-切面类showFinallyEndLog()-方法最终执行完毕-日志-方法名-" + signature.getName());
}
}
package com.elf.spring.aop.aspectj;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Order(value = 2)
@Aspect
@Component
public class SmartAnimalAspect {
@Pointcut(value = "execution(public float com.elf.spring.aop.aspectj.SmartDog.getSum(float, float)))")
public void myPointCut() {
}
@Before(value = "myPointCut()")
public void showBeginLog(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
System.out.println("SmartAnimalAspect-切面类showBeginLog()[使用的myPointCut()]-方法执行前-日志-方法名-" + signature.getName() + "-参数 "
+ Arrays.asList(joinPoint.getArgs()));
}
@AfterReturning(value = "myPointCut()", returning = "res")
public void showSuccessEndLog(JoinPoint joinPoint, Object res) {
Signature signature = joinPoint.getSignature();
System.out.println("SmartAnimalAspect-切面类showSuccessEndLog()-方法执行正常结束-日志-方法名-" + signature.getName() + " 返回的结果是=" + res);
}
@AfterThrowing(value = "myPointCut()", throwing = "throwable")
public void showExceptionLog(JoinPoint joinPoint, Throwable throwable) {
Signature signature = joinPoint.getSignature();
System.out.println("SmartAnimalAspect-切面类showExceptionLog()-方法执行异常-日志-方法名-" + signature.getName() + " 异常信息=" + throwable);
}
@After(value = "myPointCut()")
public void showFinallyEndLog(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
System.out.println("SmartAnimalAspect-切面类showFinallyEndLog()-方法最终执行完毕-日志-方法名-" + signature.getName());
}
@Before(value = "execution(public void com.elf.spring.aop.aspectj.UsbInterface.work())")
public void hi(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
System.out.println("切面类的hi()-执行的目标方法-" + signature.getName());
}
@Before(value = "execution(public void Car.run())")
public void ok1(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
System.out.println("切面类的ok1()-执行的目标方法-" + signature.getName());
}
@AfterReturning(value = "execution(public void Car.run())")
public void ok2(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
System.out.println("切面类的ok2()-执行的目标方法-" + signature.getName());
}
@AfterThrowing(value = "execution(public void Car.run())")
public void ok3(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
System.out.println("切面类的ok3()-执行的目标方法-" + signature.getName());
}
@After(value = "execution(public void Car.run())")
public void ok4(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
System.out.println("切面类的ok4()-执行的目标方法-" + signature.getName());
joinPoint.getSignature().getName();
joinPoint.getSignature().getDeclaringType().getSimpleName();
joinPoint.getSignature().getDeclaringTypeName();
joinPoint.getSignature().getModifiers();
Object[] args = joinPoint.getArgs();
joinPoint.getTarget();
joinPoint.getThis();
}
}
public class AopAspectjTest {
@Test
public void smartDogTestByProxy() {
ApplicationContext ioc =
new ClassPathXmlApplicationContext("beans08.xml");
SmartAnimalable smartAnimalable =
ioc.getBean(SmartAnimalable.class);
smartAnimalable.getSum(10, 2);
System.out.println("smartAnimalable运行类型="
+ smartAnimalable.getClass());
System.out.println("=============================");
}
- 前置通知执行的顺序是通过注解来配置的,@Order(value=n)n值越小,越先执行.
- 前置通知先执行的切面类,其返回通知和最终通知先执行.有点像Filter过滤器链式调用的机制.
- 一个切面类可以控制多个横切关注点.
底层用的还是动态代理和反射机制.