1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
@Aspect @Component @Configuration public class DaoAspect { private static final String createBy = "createBy"; private static final String createTime = "createTime"; private static final String lastUpdateBy = "lastUpdateBy"; private static final String lastUpdateTime = "lastUpdateTime";
@Pointcut("execution(* com.louis.kitty.*.dao.*.update*(..))") public void daoUpdate() { }
@Pointcut("execution(* com.louis.kitty.*.dao.*.insert*(..))") public void daoCreate() { }
@Around("daoUpdate()") public Object doAroundUpdate(ProceedingJoinPoint pjp) throws Throwable { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); //获取request 上下文请求 if (attributes == null) { return pjp.proceed(); //调用了目标方法,并获取其返回值; } HttpServletRequest request = attributes.getRequest(); String token = request.getHeader("token"); String username = getUserName(); if (token != null && username != null) { Object[] objects = pjp.getArgs();//获取当前方法请求的参数对象 if (objects != null && objects.length > 0) { for (Object arg : objects) { BeanUtils.setProperty(arg, lastUpdateBy, username);//绑定参数对象 BeanUtils.setProperty(arg, lastUpdateTime, new Date()); } } } Object object = pjp.proceed(); return object;
}
@Around("daoCreate()") public Object doAroundCreate(ProceedingJoinPoint pjp) throws Throwable { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); if (attributes == null) { return pjp.proceed(); } Object[] objects = pjp.getArgs(); if (objects != null && objects.length > 0) { for (Object arg : objects) { String username = getUserName(); if (username != null) { if (StringUtils.isBlank(BeanUtils.getProperty(arg, createBy))) { BeanUtils.setProperty(arg, createBy, username); } if (StringUtils.isBlank(BeanUtils.getProperty(arg, createTime))) { BeanUtils.setProperty(arg, createTime, new Date()); } } } } Object object = pjp.proceed(); return object; }
private String getUserName() { return SecurityUtils.getUsername(); } }
|
近期评论