开发者

Help create AspectJ equivellent to @PrePersist and @PreUpdate for audit use case

In JPA, there is @PrePersist and @PreUpdate annotations that allow operations before CRUD operations. I am trying to find out the ApsectJ equivalent to this.

My use case is a JPA application that was built by one team, now would like to add an Audit Aspect to each Pre-Persist and Pre-Update that occurs, without adding a lif开发者_Python百科ecycle listener to the original Entity.


If the entities don't have @PrePersist and @PreUpdate methods, you can use AspectJ intertype declarations (ITDs) to introduce those methods.

public aspect Audit {
    declare parents: @Entity * implements AuditedEntity;

    public interface AuditedEntity {}

    @PrePersist
    public void AuditedEntity.prePersistAuditing() {
       ... auditing logic
    }

    ... similar code for @PreUpdate
}

If the entities already have the methods, you can advise those to perform auditing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜