1.使用自定义注解需要引入的包
1.9.1 org.aspectj aspectjrt ${aspectj.version}
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() {
}
@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;
}
@Before(value = "aspect()")
public void before() {
System.out.println("=================Before======================");
}
@After(value = "aspect()")
public void after() {
System.out.println("=================After======================");
}
@AfterReturning(value = "aspect()", returning = "response")
public void afterReturning(JoinPoint joinPoint, Response> response) {
System.out.println("=================AfterReturning======================");
}
@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======================



