Spring/AOP:在数据库中实现活动日志的最佳方式 [英] Spring / AOP: Best way to implement an activities log in the database

查看:32
本文介绍了Spring/AOP:在数据库中实现活动日志的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些 Spring/AOP 教程,并且对相关概念有所熟悉.

I have been going through some Spring / AOP tutorials and have somewhat familiarized myself with the related concepts.

现在满足我的要求,我需要创建一个活动日志实现,它将登录用户的活动保存在数据库中,范围可以从申请服务或在 Admin 的情况下创建新用户 用户等.在调用任何具有注释的方法时(比如 @ActivityLog),这些信息将以 actorId 的形式持久化>actionCommentactionTimeactedUponId、...等

Now coming to my requirements, I need to create an Activities Log implementation which will save the activities of a logged-in user in the DB which can range from applying for a service or creating new users in case of Admin users, etc. On invocation of any method having an annotation (say @ActivityLog), this information is to be persisted in the form of actorId, actionComment, actionTime, actedUponId, ... etc.

现在,如果我创建一个 POJO 类(映射到数据库中的 ActivityLog 表)并希望从 Advice 内部保存此数据(最好使用与方法相同的事务,方法使用 @Transactional 注释),我如何实际填充这个 POJO 中的变量?我可能可以从会话对象 & 中获取 actorIdactionTime 可以简单地为 new Date() 但是actionComment/actedUponId 的动态值如何?

Now, if I create a POJO class (that maps to a ActivityLog table in the DB) and want to save this data from inside the Advice (preferably using the same transaction as the method, method uses @Transactional annotation), how do I actually populate the variables in this POJO?? I can probably get the actorId from the session object & actionTime can simply be new Date() but how about the dynamic values for actionComment / actedUponId?

任何帮助都会很棒!(顺便说一句,我要求不使用 Hibernate Interceptors.)

Any help will be brilliant! (BTW, I have a requirement to not use Hibernate Interceptors.)

推荐答案

这是一个完整的例子:

@Aspect
@Component
public class WebMethodAuditor {

protected final Log logger = LogFactory.getLog(getClass());

public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";

@Autowired
AuditRecordDAO auditRecordDAO; 

@Before("execution(* com.mycontrollers.*.*(..))")
public void beforeWebMethodExecution(JoinPoint joinPoint) {
    Object[] args = joinPoint.getArgs();
    String methodName = joinPoint.getSignature().getName();
    User principal = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    Timestamp timestamp = new Timestamp(new java.util.Date().getTime());
    // only log those methods called by an end user
    if(principal.getUsername() != null) {
        for(Object o : args) {
            Boolean doInspect = true;
            if(o instanceof ServletRequestDataBinder) doInspect = false;
            if(o instanceof ExtendedModelMap) doInspect = false;
            if(doInspect) {
                if(o instanceof BaseForm ) {
                    // only show form objects
                    AuditRecord ar = new AuditRecord();
                    ar.setUsername(principal.getUsername());
                    ar.setClazz(o.getClass().getCanonicalName());
                    ar.setMethod(methodName);
                    ar.setAsString(o.toString());
                    ar.setAudit_timestamp(timestamp);
                    auditRecordDAO.save(ar);
                }
            }
        }
    }
}

}

这篇关于Spring/AOP:在数据库中实现活动日志的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆