springboot Aspect类中,@After执行xxRepository.save()方法后,会同步提交目标方法中的xxRepository.update()。

Aspect中保存日志

@Aspect
@Component
public class LogAspect {

    @Autowired
    private LogService logService;

        @Pointcut("@annotation(com.webmagic.Annotation.LogOperator)")
    public void LogOperator(){}

         @AfterReturning(value = "LogOperator()")
    public void doAfterAdvice(JoinPoint joinPoint){
            .............
            logService.insertLog(logEntity);
    }
}

目标方法

    @PostMapping("/test")
    @LogOperator(method = "test")
    public Result test(){
        UserEntity userEntity = new UserEntity();
        userEntity.setUserName("test");
        userEntity.setPassWord("123");
        userEntity = userService.insertUser(userEntity);
        userEntity.setPassWord("");
        return Result.success();
    }

当目标方法test中inserUser返回的对象,属性有变时,执行到@After中insertLog,会:
图片说明

最终数据库中保存的password为空。

为什么会这样呢?

userEntity.setPassWord("123");
这个会引起 update

用的是hibernate吧 这段代码userEntity.setPassWord(""); userEntity是数据库 操作过的,后续对这个对象有操作,就算是不调用CRUD,还是会默认提交的;你把这行代码删了就行了