定义了注解Check
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RUNTIME)
public @interface Check {
// 字段校验规则,格式:字段名+校验规则+冒号+错误信息,例如:id<10:ID必须少于10
String[] value();
}
和切面类
@Aspect
@Component
public class CheckParamAspect {
private static final Logger LOG = LoggerFactory.getLogger(CheckParamAspect.class);
@Pointcut("@annotation(xx.xx.xx.checker.Check)")
public void annotationPoinCut(){}
//
// @Pointcut("execution(public * xx.xx.xx.controller..*.*(..))")
// public void annotationPoinCut(){}
@Around("annotationPoinCut()")//
public Object check(ProceedingJoinPoint point) throws Throwable {
LOG.debug("aop check");
}
}
希望达到的效果是所有被@check注解的方法 执行check()
在controller中
@RequestMapping(value = "/check", method = RequestMethod.GET)
public void check(HttpServletRequest request) {
System.out.println("收到请求"+request.toString()) ;
testValid(xxx)
}
@Check
testValid(xxx)
现象:发现@check标签并没有增强testValid()方法;
原因:testValid(xxx) 是嵌套调用的 这种调用是调用的类本身的testVaild方法 而非 Spring生成的代理类的增强后的testVaild方法
所以不会打印“aop check”
解决方法
@Autowired private TestController self;
用self.testValid()就是增强后的testValid方法了
参考:[Done]Spring @Pointcut 切点调用不到(SpringAOP嵌套方法不起作用) 注意事项 - 大辉_FFf - 博客园
5834

被折叠的 条评论
为什么被折叠?



