Springboot使用aop
导入pom包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
@Aspect
@Component
public class AopDemo {
@Pointcut("execution(public * com.zhk.study.ping.*.*(..))")
public void aop(){}
@Before("aop()")
public void doBefore(JoinPoint joinPoint){
System.out.println("前置通知");
}
@After("aop()")
public void doAfter(JoinPoint joinPoint){
System.out.println("后置通知");
}
@AfterReturning("aop()")
public void doAfterReturning(JoinPoint joinPoint){
System.out.println("最終通知");
}
@AfterThrowing("aop())")
public void deAfterThrowing(JoinPoint joinPoint){
System.out.println("异常通知");
}
@Around("aop()")
public Object deAround(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("环绕通知1");
Object proceed = joinPoint.proceed();
System.out.println("环绕通知2");
return proceed;
}
}
执行循序:
环绕通知1
前置通知
pn
最終通知
后置通知
环绕通知2
利用自定义注解使用AOP
@Around("@annotation(注解名字)")