@Pointcut("execution (* com.salme.blogtest.rest..*.*(..))")
public void controllerAspect() { }
// 切面
@Before("controllerAspect()")
public void doBefore(JoinPoint joinPoint) throws Exception {
System.out.println("==========执行controller前置通知===============");
if (log.isInfoEnabled()) {
//获得类名
String classType = joinPoint.getTarget().getClass().getName();
System.out.println("类名====" + classType + "===");
//获得方法名
String methodName = joinPoint.getSignature().getName();
System.out.println("方法名====" + methodName + "===");
System.out.println("目标方法内的参数为"+ Arrays.asList(joinPoint.getArgs()));
LogControl controllerLog = getAnnotationLog(joinPoint);
if(controllerLog != null){
String action = controllerLog.action();
String title = controllerLog.title();
log.info(">>>>>>>>>>>>>模块名称:{}",title);
log.info(">>>>>>>>>>>>>操作名称:{}",action);
}
//打印日志,如有需要还可以存入数据库
log.info(">>>>>>>>>>>>>参数:{}",Arrays.asList(joinPoint.getArgs()));
log.info(">>>>>>>>>>>>>类名:{}",classType);
log.info(">>>>>>>>>>>>>方法名:{}",methodName);
}
}
}
log注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogControl {
String title() default "";
String action() default "";
}
具体rest
@LogControl(title = "ioc",action = "select")
@RequestMapping("/log-test")
public Test getLog(String uid, Test test){
test.setName(uid);
return test;
}