背景:
想利用spring的aop增加自动记录日志功能(很老掉牙的需求),本打算只扫描RequestMapping注解的方法,增加日志记录;后面想想可能在非controller类中也需要记录日志功能,就增加了个自定义注解Loggable,只要有该注解的方法,也增加日志功能。也确实实现了记录日志的功能,所有controller类中有RequestMapping注解的方法和非controller类中有Loggable注解的方法,突然之间想到如果我在controller类中只用Loggable注解看看,结果懵逼了,硬是扫描不到该方法。。。苦思良久找不到答案,先上代码。
Loggable注解类:
package com.lingroad.common.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Component;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Component
public @interface Loggable {
}
spring-mvc.xml配置部分:
LogAspect类:
package com.lingroad.common.log;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
@Aspect
public class LogAspect {
private final Logger log = LogManager.getLogger(this.getClass());
@Pointcut(" execution(* com.lingroad.web..*.*(..)) && @annotation(org.springframework.web.bind.annotation.RequestMapping) ")
private void requestMapping(){}
@Pointcut(" execution(* *(..)) && @annotation(com.lingroad.common.annotation.Loggable) ")
private void loggable(){}
@Pointcut(" loggable() || requestMapping() ")
public void aspect(){
}
@Around("aspect()")
public Object doAround(ProceedingJoinPoint point) throws Throwable {
long start = System.currentTimeMillis();
Object result = point.proceed();
log.info("{}({}): {} in {}ms",
MethodSignature.class.cast(point.getSignature()).getMethod().getName(),
point.getArgs(),
result,
System.currentTimeMillis() - start
);
return result;
}
}
controller类:
package com.lingroad.web.systemmanage.user.controller;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.lingroad.common.annotation.Loggable;
import com.lingroad.common.util.WebRequestUtil;
import com.lingroad.web.systemmanage.user.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
private static Logger log = LogManager.getLogger(UserController.class.getName());
@Autowired
private UserService userService;
@RequestMapping("preLogin.do")
public String preLogin(){
return "login";
}
@RequestMapping("login.do")
public String login(@RequestParam("username") String account,@RequestParam("pwd") String password
,@RequestParam(value="autologin",required=false)String autologin){
boolean loginSuccess = userService.login(account, password);
log.info("login {}",loginSuccess);
testMethod("zhangsan",26);
WebRequestUtil.setRequestAttr("message", loginSuccess);
return "login";
}
@Loggable
public void testMethod(String name,int age){
}
}
service类:
package com.lingroad.web.systemmanage.user.service;
import org.springframework.stereotype.Service;
import com.lingroad.common.annotation.Loggable;
@Service
public class UserService {
@Loggable
public boolean login(String account,String password){
if("admin".equals(account)&&"123456".equals(password)){
return true;
}
return false;
}
}
实际输出:
2016-09-21 14:29:43.832 INFO [1576787878@qtp-1257550483-0][com.lingroad.common.log.LogAspect:29] - login([admin, 123456]): true in 14ms
2016-09-21 14:29:43.833 INFO [1576787878@qtp-1257550483-0][com.lingroad.web.systemmanage.user.controller.UserController:31] - login true
2016-09-21 14:29:43.834 INFO [1576787878@qtp-1257550483-0][com.lingroad.common.log.LogAspect:29] - login([admin, 123456, null]): login in 16ms
问题:
缺省testMethod方法的日志输出,求大神