使用 Spring Data Neo4j 进行审计 [英] Audits with Spring Data Neo4j

查看:67
本文介绍了使用 Spring Data Neo4j 进行审计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个使用 Spring Data Neo4j 的项目.每当创建 NodeEntity 时,我想创建一个引用的 Audit NodeEntity,其中包含创建日期和用户.

I'm currently working on a project that makes use of Spring Data Neo4j. Whenever a NodeEntity is created, I would like to create a referenced Audit NodeEntity that contains the creation date and user.

我想出的一个解决方案是编写一个 AOP 方面,它与我的服务层的 create 方法挂钩.这适用于非级联的实体,但级联的实体呢?那些没有在我的服务层中明确传递,所以我的 AOP 类不会拦截它们.JPA 中是否有实体侦听器之类的概念,或者如何挂钩这种机制?

A solution that I've come up with, is to write an AOP Aspect which hooks in on the create method of my service layer. This works fine for entities that aren't cascaded, but what about the cascaded ones? That are not explicitly passed in my service layer so my AOP class will not intercept them. Is there a concept like entity listeners in JPA, or how can I hook into this mechanism?

推荐答案

从 Spring Data Neo4j 2.2 开始,我们可以使用 AuditingEventListener 来对实体进行审计.Spring Data 1.5 提供了 @CreatedDate, @CreatedBy, @LastModifiedDate@LastModifiedBy 注释.您可以按如下方式使用它们:

Since Spring Data Neo4j 2.2, we can use the AuditingEventListener for the auditing of entities. Spring Data 1.5 offers the @CreatedDate, @CreatedBy, @LastModifiedDate and @LastModifiedBy annotations. You can use them as follows:

@NodeEntity
public class Entity {

    @GraphId
    private Long id;

    @CreatedDate
    private Long date;

}

确保配置 AuditingEventListener:

Make sure to configure the AuditingEventListener:

@Configuration("db")
@EnableNeo4jRepositories(basePackages = { "your.package" })
@EnableTransactionManagement
public class DatabaseSpringConfiguration extends Neo4jConfiguration {

    @Bean(destroyMethod = "shutdown")
    public EmbeddedGraphDatabase graphDatabaseService() {
        return new EmbeddedGraphDatabase("data/neo4j.db");
    }

    @Bean
    public AuditingEventListener auditingEventListener() throws Exception {
        return new AuditingEventListener(new IsNewAwareAuditingHandler<Object>(isNewStrategyFactory()));
    }

}

这篇关于使用 Spring Data Neo4j 进行审计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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