本文介绍了使用 AOP 和 Spring Boot 的多个审计表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建审计方面,根据设计按某些主要实体类型存储不同的审计.我创建了像 @Auditing 这样的注释,我在其中定义了审计类型变量.这将在方法级别进行注释.在方面设计中,我在哪里添加用于主实体的审计表的逻辑?

I want to create audit aspect that will store different auditing by some main entity type as per design. I have created annotation like @Auditing where I defined audit type variable. That will annotated at method level. Where do I add logic for which audit table use for main entity in aspect design?

举个例子:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Auditing {

   Event event();
}

方面设计:

@Aspect
@Component
public class AuditAspect {

    @AfterReturning(value = "@annotation(auditable)")
    public void save(Auditing audit) {

        Audit auditInfo = new Audit();
        // I plan to add some condition here by entity type
    }
}

我打算在创建审计实体后添加一些条件,例如哪个表用于按类型进行审计存储"?这样好吗?

I plan to add some condition after audit entity create like ‘Which table is used for audit store by type’? Is this good?

推荐答案

如果您担心性能,据我所知 Aspect 代码在应用程序引导期间仅被编织到您自己的代码中一次,因此没问题.

If you're worried about performance, from my knowledge Aspect code is weaved into your own code only once during application bootstrapping so there's no problem there.

本质上是这样的方法:

@Auditing
public void test(){
  // method logic
}

变成

@Auditing
public void test(){
   Audit auditInfo = new Audit();
  // extra auditing logic...

  // method logic
}

所以只要方面方法主体不是执行时间长的东西,你就不应该有问题.但是,如果审计方面也访问数据库并且您的 Audited 方法被频繁调用,您可能会产生很大的开销.将所有审计更新添加到一个集合中并以批处理方式定期执行它们可能会更好.

so as long as the aspect method body isn't something with a long execution time you shouldn't have a problem. However if the auditing aspect also accesses the database and your Audited methods are being frequently called you might have a big overhead. It would probably be better to add all the audit updates in a collection and periodically execute them in a batch manner.

现在关于你应该把表断言代码放在哪里,我的意见是在方面.否则,您必须为每张桌子创建不同的方面,这首先击败了这一点.但是,请检查您是否确实可以将表名作为注释参数传递并在您的方面访问它.

Now regarding where you should put the table assertion code, my opinion is inside the aspect. Otherwise you'd have to create different aspects for each table which beats the point in the first place. However, please check if you can actually pass the table name as an annoatation parameter and access it inside your aspect.

您最想要的是:

@Auditing(tableName="AUDIT_TABLE_1")
public void auditableMethod() {
  // logic
}

@Aspect
public void audit(Auditing audit) {
  String table = audit.tableName;
  // do your jdbc logic
}

这篇关于使用 AOP 和 Spring Boot 的多个审计表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-21 19:24