首先要有一個註解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyLog {
}
然後有一個切面類
@Component
@Aspect
public class MyLogAspect {
@Pointcut("@annotation(cn.alibaba.MyAnnotation.MyLog)")
public void logPointCut(){
}
@Around("logPointCut()")
public void logAround(ProceedingJoinPoint joinPoint){
//方法名稱
String methodName = joinPoint.getSignature().getName();
System.out.println("開始:"+methodName+"方法");
try {
joinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("結束:"+methodName+"方法");
}
}
@RequestMapping("/testLog3")
@ResponseBody
@MyLog
public void testLog3(){
System.out.println("testlog3 ing...");
}
測試controlller就可以了
如果是springmvc項目 配置文件還要加一句開啓aspectj
<aop:aspectj-autoproxy proxy-target-class="true" />