项目有个字典转换的处理,是在controller上加切点,对所有page类数据进行过滤处理,找到字典项转成中文用于页面显示。
我这是普通搜索在Mapper里得到数据,也想进行如上处理,进行的改造,简单记录下。
开始想要通过AfterRunning 来处理,认为是mapper的selectCollection方法中得到数据后我来处理一下返回即可。但结果是,在AfterRunning 中能够抓到切点并处理数据,但是没有返回到mapper的结果中。
@Pointcut(“execution (* com.*.mapper.*Mapper.selectCollection(…))”)
public void excudeService() {
log.info(“excudeService”);
}
@AfterReturning(returning = “result”, pointcut = “excudeService()”)
public Object doAfterReturning(JoinPoint jp, Object result) throws Throwable {
result = this.parseDictText(result); //处理数据方法,重构了result里数据。
return result;
}
List users = userMapper.selectCollection(user);
这里的users中数据是没处理的。
改用@Around可以实现,
@Around(“excudeService()”)
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
Object result = pjp.proceed();
result = this.parseDictText(result); //处理数据方法,重构了result里数据。
return result;
}
上面的users 里就是处理后的数据了。
原因未知,但找了一些资料整理了一下。
参考的:https://blog.csdn.net/u010502101/article/details/78823056
@Before 可用参数JoinPoint 可以取到切点方法的传入,做些参数相关的事,是否可以做到,过滤修改参数来做不同处理?
@Before(“execution( xxx )”)
public void beforMethod(JoinPoint point){
@After 可用参数JoinPoint 可以取到切点方法的传入参数,是否也就做点输出呢?
@After(("execution( xxx "))
public void afterMethod(JoinPoint point){
@AfterRunning 用参数JoinPoint和返回结果Object result,可以输出点参数信息,也可以重构下结果result,比如把里面字典项做下显示处理,但result没有在切点的结果中得到(不知为啥?)
@AfterReturning(value=“execution( xxx )”, returning=“result”)
public void afterReturning(JoinPoint point, Object result){
@AfterThrowing 可用参数JoinPoint和异常,加点输出吧。
@AfterThrowing(value=" xxx )", throwing=“ex”)
public void afterReturning(JoinPoint point, Exception ex){
@Around 可用参数ProceedingJoinPoint,可以得到切点方法结果,并进行处理转换返回到切点结果。很多处理都是加输出,记录日志。
@Around(“execution( xxx )”)
public Object aroundMethod(ProceedingJoinPoint pdj){
有知道原因的请求帮忙解释一下,谢谢!
有其他写日志外的应用的例子也请推荐下,谢谢!