springboot学习记录–aop入门
execution表达式
例:execution(* com.study.managersystem.service….(…))
execution(返回值类型 被切包名…(类名).方法名(参数))
Aop注解
- @Aspect 切面类
- @Component 申明aop切面类
- @Pointcut(“execution(* com.study.managersystem.service….(…))”) 申明切点
- @Before(“被增强方法”) 前置通知,在切点方法执行前执行
- @After(“被增强方法”) 后置通知,在切点方法执行后执行
- @Around(“被增强方法”) 前置通知,在切点方法执行前后执行(第一次在before之前,第二次在after之前)
实例
package com.study.managersystem.aop;
import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class aopTest {
private Logger logger = LoggerFactory.getLogger(getClass());
@Pointcut("execution(* com.study.managersystem.service..*.*(..))")
public void dayLog(){
}
@Before("dayLog()")
public void beforeLog(JoinPoint point){
logger.info("before dayLog ==============");
}
@After("dayLog()")
public void afterLog(JoinPoint joinpoint){
logger.info("after dayLog =================");
}
@Around("dayLog()")
public void around(ProceedingJoinPoint proceedingJoinPoint){
logger.info("around pre dayLog =================");
try {
proceedingJoinPoint.proceed();
} catch (Throwable throwable) {
logger.error("around proceed dayLog error");
throwable.printStackTrace();
}
logger.info("around end dayLog =================");
}
}