java - Spring 数据 Neo4J 4.0.0 : BeforeSaveEvent not firing?

标签 java spring neo4j spring-data-neo4j-4

我试图在 Spring 中设置 Neo4J 时捕获 BeforeSaveEvent,以便我可以在正在保存的类上调用方法 beforeSave()。不幸的是,它似乎没有被注册为监听器,因为我的打印语句都没有被执行。

赞赏的想法。

@Configuration
@EnableNeo4jRepositories(basePackages = "com.noxgroup.nitro")
@EnableTransactionManagement
public class NitroNeo4jConfiguration extends Neo4jConfiguration {

    @Bean
    public Neo4jServer neo4jServer () {
        System.setProperty("username", "neo4j");
        System.setProperty("password", "*************");
        return new RemoteServer("http://localhost:7474");
    }

    @Bean
    public SessionFactory getSessionFactory() {
        return new SessionFactory("com.noxgroup.nitro.domain");
    }

    @Bean
    ApplicationListener<BeforeSaveEvent> beforeSaveEventApplicationListener() {
        return new ApplicationListener<BeforeSaveEvent>() {
            @Override
            public void onApplicationEvent(BeforeSaveEvent event) {
                System.out.println("Listening to event");
                Object entity = event.getEntity();
                if (entity instanceof NitroNode) {
                     ((NitroNode)entity).beforeSave();
                } else {
                    System.out.println("Not picking it up");
                }
            }
        };
    }

}

最佳答案

这些事件由 Neo4jTemplate 触发(参见 http://docs.spring.io/spring-data/neo4j/docs/4.0.0.M1/reference/html/#_data_manipulation_events_formerly_lifecycle_events ),因此您必须使用这些事件来触发保存。

在您的配置 NitroNeo4jConfiguration 中包含

@Bean
public Neo4jOperations getNeo4jTemplate() throws Exception {
    return new Neo4jTemplate(getSession());
}

在你的应用程序中,

@Autowired
private Neo4jOperations neo4jTemplate;

然后用来保存

neo4jTemplate.save(person);

关于java - Spring 数据 Neo4J 4.0.0 : BeforeSaveEvent not firing?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30604863/

相关文章:

java - 如何在Spring TaskExecutor框架中管理线程

java - 在 neo4j 3 中放置扩展依赖项的位置

java - Neo4j 和 java : What does this error "NULL is not a keyed collection" mean?

java - 数据源和jdbctemplate的依赖注入(inject)?

java - java中数组和二维数组相乘

使用 IMAP 登录 Gmail 时出现 java.lang.reflect.InitationTargetException

java - 奇数节点总和与偶数节点总和的二叉树差异

java - JSONObject 总是返回 "empty": false

java - 如果通过java使用neo4j,如何重置neo4j数据库?

java - Android万能音乐播放器在MediaItem ViewHolder中添加专辑封面