使用Spring Data Neo4j进行审计

tst*_*rms 2 entity neo4j spring-data spring-data-neo4j

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

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

tst*_*rms 5

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

@NodeEntity
public class Entity {

    @GraphId
    private Long id;

    @CreatedDate
    private Long date;

}
Run Code Online (Sandbox Code Playgroud)

确保配置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()));
    }

}
Run Code Online (Sandbox Code Playgroud)