SDN4 Java配置和索引.auto =断言

时间:2017-04-22 18:21:22

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

我将我的Neo4j配置从ogm.properties移到了Java配置。

这是我目前的配置:

@Configuration
@EnableNeo4jRepositories(basePackages = "com.example.domain.repository.neo4j")
@EnableTransactionManagement
public class Neo4jTestConfig {

    @Value("${neo4j.embedded.database.path}")
    private String storeDir;

    @Bean
    public Neo4jTransactionManager transactionManager() throws Exception {
        return new Neo4jTransactionManager(sessionFactory());
    }

    @Bean
    public SessionFactory sessionFactory() {

        Components.setDriver(new EmbeddedDriver(graphDatabaseService()));

        return new SessionFactory("com.example");
    }

    @Bean(destroyMethod = "shutdown")
    public GraphDatabaseService graphDatabaseService() {

        // @formatter:off
        GraphDatabaseService graphDatabaseService = new GraphDatabaseFactory()
                .newEmbeddedDatabaseBuilder(new File(storeDir))
                .loadPropertiesFromFile(this.getClass().getClassLoader().getResource("neo4j.properties").getPath())
                .newGraphDatabase();
        // @formatter:on

        return graphDatabaseService;
    }

}

目前我不知道如何正确地将OGM属性indexes.auto=assert添加到此配置中。

已更新

我更新了我的配置如下:

@Profile("test")
@Configuration
@EnableNeo4jRepositories(basePackages = "com.example.domain.repository.neo4j")
@EnableTransactionManagement
public class Neo4jTestConfig {

    @Value("${neo4j.embedded.database.path}")
    private String storeDir;

    @Bean
    public Neo4jTransactionManager transactionManager() throws Exception {
        return new Neo4jTransactionManager(sessionFactory());
    }

    @Bean
    public SessionFactory sessionFactory() {

        Components.setDriver(new EmbeddedDriver(graphDatabaseService()));

        return new SessionFactory(configuration(), "com.example.domain.model");
    }

    @Bean(destroyMethod = "shutdown")
    public GraphDatabaseService graphDatabaseService() {

        // @formatter:off
        GraphDatabaseService graphDatabaseService = new GraphDatabaseFactory()
                .newEmbeddedDatabaseBuilder(new File(storeDir))
                .loadPropertiesFromFile(this.getClass().getClassLoader().getResource("neo4j.properties").getPath())
                .newGraphDatabase();
        // @formatter:on

        return graphDatabaseService;
    }

    @Bean
    public org.neo4j.ogm.config.Configuration configuration() {
        org.neo4j.ogm.config.Configuration config = new org.neo4j.ogm.config.Configuration();
        config.autoIndexConfiguration().setAutoIndex("assert");
        return config;
    }

}

但它现在失败,但有以下例外:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.neo4j.ogm.session.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is org.neo4j.ogm.exception.ServiceNotFoundException: Could not load driver: null.
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 80 common frames omitted
Caused by: org.neo4j.ogm.exception.ServiceNotFoundException: Could not load driver: null.
    at org.neo4j.ogm.service.DriverService.load(DriverService.java:57)
    at org.neo4j.ogm.service.DriverService.load(DriverService.java:69)
    at org.neo4j.ogm.service.Components.loadDriver(Components.java:158)
    at org.neo4j.ogm.service.Components.driver(Components.java:104)
    at org.neo4j.ogm.session.SessionFactory.<init>(SessionFactory.java:44)
    at org.neo4j.ogm.session.SessionFactory.<init>(SessionFactory.java:93)
    at com.example.domain.configuration.Neo4jTestConfig.sessionFactory(Neo4jTestConfig.java:37)
    at com.example.domain.configuration.Neo4jTestConfig$$EnhancerBySpringCGLIB$$bde0f39a.CGLIB$sessionFactory$1(<generated>)
    at com.example.domain.configuration.Neo4jTestConfig$$EnhancerBySpringCGLIB$$bde0f39a$$FastClassBySpringCGLIB$$b12a6805.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)
    at com.example.domain.configuration.Neo4jTestConfig$$EnhancerBySpringCGLIB$$bde0f39a.sessionFactory(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 81 common frames omitted

已更新

这是我基于Bolt Driver的生产配置:

@Profile("production")
@Configuration
@EnableNeo4jRepositories(basePackages = "com.example.domain.repository.neo4j")
@EnableTransactionManagement
public class Neo4jConfig {

    @Value("${neo4j.server.database.uri}")
    private String serverDatabaseUri;

    @Value("${neo4j.username}")
    private String username;

    @Value("${neo4j.password}")
    private String password;

    @Bean
    public Neo4jTransactionManager transactionManager() throws Exception {
        return new Neo4jTransactionManager(sessionFactory());
    }

    @Bean
    public SessionFactory sessionFactory() {
        Components.setDriver(new BoltDriver());

        return new SessionFactory(configuration(), "com.example.domain.model");
    }

    @Bean
    public org.neo4j.ogm.config.Configuration configuration() {
        org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration();

        // @formatter:off
        configuration
            .autoIndexConfiguration()
                .setAutoIndex("assert");
        configuration
            .driverConfiguration()
                .setCredentials(username, password)
                .setURI(serverDatabaseUri);
        // @formatter:on

        return configuration;
    }

}

此配置工作正常,但我仍然遇到基于嵌入式Java配置的问题。

1 个答案:

答案 0 :(得分:2)

这似乎是OGM配置中的一个盲点,因此需要一些跳跃箍。

有两个问题:

  • 必须设置驱动程序类名,否则将获得NPE
  • 即使您这样做,.container a:hover{ box-shadow: 10px 10px 5px #000000; -moz-box-shadow: 0px 10px 5px #000000; -webkit-box-shadow: 0px 10px 5px #000000; 设置的驱动程序也会在Components.setDriver中被销毁(OGM认为您正在重新配置它)

对于使用自定义配置的嵌入式数据库,您的new SessionFactory应如下所示:

sessionFactory()

它有效,但要注意它是一个黑客。虽然应该可以进行测试。