【问题标题】:How to configure spring-data-neo4j embedded server properties?如何配置 spring-data-neo4j 嵌入式服务器属性?
【发布时间】:2017-05-15 08:28:45
【问题描述】:

我已使用 spring-data-neo4j v 4.2.1、neo4j-ogm v 2.1.2 进行设置。
我需要具有特定配置的嵌入式 neo4j 服务器进行测试。 cypher.forbid_shortestpath_common_nodes=false.

我在 spring @Configuration bean 中尝试了这个但没有成功:

@Bean
public org.neo4j.ogm.config.Configuration getConfiguration() {
    org.neo4j.ogm.config.Configuration config = new org.neo4j.ogm.config.Configuration();
    config.driverConfiguration().setDriverClassName("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver");
    config.set("cypher.forbid_shortestpath_common_nodes", false);
    return config;
}

请问,如何在 spring java 配置中进行设置?

【问题讨论】:

    标签: neo4j spring-data-neo4j-4 neo4j-ogm


    【解决方案1】:

    cypher.forbid_shortestpath_common_nodes 是 Neo4j 设置,而不是 SDN/OGM,因此您需要在创建数据库时将其提供给数据库。

    理想情况下,嵌入式数据库的配置应如下所示:

    @Configuration
    @EnableNeo4jRepositories(basePackageClasses = UserRepository.class)
    @ComponentScan(basePackageClasses = UserService.class)
    static class EmbeddedConfig {
    
        @Bean(destroyMethod = "shutdown")
        public GraphDatabaseService graphDatabaseService() {
            GraphDatabaseService graphDatabaseService = new GraphDatabaseFactory()
                .newEmbeddedDatabaseBuilder(new File("target/graph.db"))
                .setConfig(GraphDatabaseSettings.forbid_shortestpath_common_nodes, "false")
                .newGraphDatabase();
    
            return graphDatabaseService;
        }
    
        @Bean
        public SessionFactory getSessionFactory() {
            org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration();
            EmbeddedDriver driver = new EmbeddedDriver(graphDatabaseService());
            Components.setDriver(driver);
            return new SessionFactory(configuration, User.class.getPackage().getName());
        }
    
        @Bean
        public Neo4jTransactionManager transactionManager() throws Exception {
            return new Neo4jTransactionManager(getSessionFactory());
        }
    }
    

    但是这对于 SDN 4.2.x 不起作用,但有一个解决方法:

        @Bean
        public SessionFactory getSessionFactory() {
            org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration();
            // Register your configuration here, this will confuse OGM so the driver you set below won't be destroyed
            Components.configure(configuration);
    
            // Register your driver
            EmbeddedDriver driver = new EmbeddedDriver(graphDatabaseService());
            Components.setDriver(driver);
    
            // Set driver class name so you won't get NPE
            configuration.driverConfiguration().setDriverClassName("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver");
    
            return new SessionFactory(configuration, User.class.getPackage().getName());
        }
    

    【讨论】:

    • 当 SessionFactory 的构造函数调用 Components.configure(configuration); 调用 Components.destroy(); 调用 driver = null; 时,为什么要使用:Components.setDriver(driver); 设置驱动程序?
    • 第二条评论:SessionFactory.openSession() 调用 Components.driver() 调用 Components.loadDriver() 调用 Components.setDriver (DriverService.load(configuration.driverConfiguration()))。所以,最终,只有你的 Configuration 的 DriverConfiguration 被用来创建新的 Driver。
    • 我们的解决方案是使用SessionFactory的另一个构造函数(没有配置参数的那个)。
    【解决方案2】:

    neo4j-ogm-embedded-driver 版本3.2.1 开始,可以应用以下方法。

    @Configuration
    public class EmbeddedNeo4jConfig {
    
        @Bean
        org.neo4j.ogm.config.Configuration getConfiguration() {
    
            var builder = new org.neo4j.ogm.config.Configuration.Builder()
                    .neo4jConfLocation("neo4j.conf");
            return builder.build();
        }
    }
    

    test/resources/neo4j.conf:

    cypher.forbid_shortestpath_common_nodes=false
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-30
      • 2016-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多