首先要有一个注解
@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" />