编写的AOP代码如下
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
@Aspect
@Component
@Slf4j
public class ActionEnterAdvisor {
// Controller层切点 匹配.controller包及其子包下的所有类的所有方法
@Pointcut("execution(* com.litongjava.spring.boot.ueditor.contoller.IndexController.index(..))")
public void actionEnterAspect() {
}
// 定义增强 advice
@Before("actionEnterAspect()")
public void doBefore(JoinPoint joinPoint){
log.info("已经进入");
}
}
测试访问IndexController的index方法会执行增强
后来我修改@Pointcut的值为如下
@Pointcut("execution(* com.baidu.ueditor.ActionEnter.exec(..))")
测试在com.baidu.ueditor.ActionEnter.exe方法执行前,增强代码不会执行
为什么不会执行呢?
Spring aop的限制蛮多的,要求切面类和被切面类都必须是 spring 容器管理,且切面方法是public。