1.使用自定义注解需要引入的包
<properties>
<aspectj.version>1.9.1</aspectj.version>
</properties>
<!--使用AspectJ方式注解需要相应的包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
2.自定义注解
import java.lang.annotation.*;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation {
String name();
}
3.自定义Aspect
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import javax.xml.ws.Response;
@Aspect
@Component
@ComponentScan
@EnableAspectJAutoProxy
public class MyAspect {
@Pointcut("@annotation(com.example.demo.annotation.MyAnnotation)")
private void aspect() {
}
/**
* 在目标方法执行之前和之后都可以执行额外代码的通知。
* 在环绕通知中必须显式的调用目标方法,目标方法才会执行,这个显式调用时通过ProceedingJoinPoint来实现的,可以在环绕通知中接收一个此类型的形参,spring容器会自动将该对象传入,注意这个参数必须处在环绕通知的第一个形参位置。
* 要注意,只有环绕通知可以接收ProceedingJoinPoint,而其他通知只能接收JoinPoint。
* 环绕通知需要返回返回值,否则真正调用者将拿不到返回值,只能得到一个null。
* 环绕通知有控制目标方法是否执行、有控制是否返回值、有改变返回值的能力。
*/
@Around(value = "aspect()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
Object retValue = null;
try {
Object[] args = joinPoint.getArgs(); //得到方法执行所需的参数
System.out.println("=================Around===前置======================");
retValue = joinPoint.proceed(args);//明确调用业务层方法(切入点方法)
System.out.println("=================Around===后置===================");
return retValue;
} catch (Throwable throwable) {
System.out.println("=================Around===异常===================");
throwable.printStackTrace();
} finally {
System.out.println("=================Around===最终===================");
}
return retValue;
}
/**
* 在目标方法执行之前执行执行的通知。
* 前置通知方法,可以没有参数,也可以额外接收一个JoinPoint,Spring会自动将该对象传入,代表当前的连接点,通过该对象可以获取目标对象 和 目标方法相关的信息。
* 注意,如果接收JoinPoint,必须保证其为方法的第一个参数,否则报错。
*/
@Before(value = "aspect()")
public void before() {
System.out.println("=================Before======================");
}
/**
* 在目标方法执行之后执行的通知。
* 在后置通知中也可以选择性的接收一个JoinPoint来获取连接点的额外信息,但是这个参数必须处在参数列表的第一个。
*/
@After(value = "aspect()")
public void after() {
System.out.println("=================After======================");
}
/**
*是在目标方法执行之后执行的通知。
*和后置通知不同之处在于,后置通知是在方法正常返回后执行的通知,如果方法没有正常返-例如抛出异常,则后置通知不会执行。
*而最终通知无论如何都会在目标方法调用过后执行,即使目标方法没有正常的执行完成。
*另外,后置通知可以通过配置得到返回值,而最终通知无法得到。
*最终通知也可以额外接收一个JoinPoint参数,来获取目标对象和目标方法相关信息,但一定要保证必须是第一个参数。
*/
@AfterReturning(value = "aspect()", returning = "response")
public void afterReturning(JoinPoint joinPoint, Response<?> response) {
System.out.println("=================AfterReturning======================");
}
/**
* 在目标方法抛出异常时执行的通知
* 可以配置传入JoinPoint获取目标对象和目标方法相关信息,但必须处在参数列表第一位
* 另外,还可以配置参数,让异常通知可以接收到目标方法抛出的异常对象。
*/
@AfterThrowing(value = "aspect()",throwing = "e")
public void afterThrowing(JoinPoint joinPoint, Exception e) {
System.out.println("=================AfterThrowing======================");
}
}
4.执行效果
@Around 正常执行:
=================Around===前置======================
=============================已进入加注解的方法=============================
=================Around===后置===================
=================Around===最终===================
===============================方法执行完成===========================
@Around 异常执行:
=================Around===前置======================
=============================已进入加注解的方法=============================
=================Around===异常===================
java.lang.NullPointerException
=================Around===最终===================
===============================方法执行完成===========================
other 正常执行:
=================Before======================
=============================已进入加注解的方法=============================
=================After======================
===============================方法执行完成===========================
other 异常执行:
=================Before======================
=============================已进入加注解的方法=============================
=================AfterThrowing======================
=================After======================