本文介绍了使用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的多重审核表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 23:35