先决条件
- 修饰符必须为public,不写默认为public
- 关键字为@interface
- 注解名称为自定义注解名称
使用的注解修饰注解
@Target
@Retention
@Document
表明该注解标记的元素可以被Javadoc 或类似的工具文档化
反射获取注解
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
XXX annotation = method.getAnnotation(XXX.class);
实例
//Controller层切点
@Pointcut("execution (* com.XX.controller..*.*(..))")
public void controllerAspect() {
}
/**
* 前置通知 用于拦截Controller层记录用户的操作
*
* @param joinPoint 切点
*/
@Before("controllerAspect()")
public void doBefore(JoinPoint joinPoint) {
System.out.println("==========执行controller前置通知===============");
if(logger.isInfoEnabled()){
logger.info("before " + joinPoint);
}
}