2017-04-22 38 views
0

我將Neo4j配置從ogm.properties移至Java配置。SDN4 Java配置和索引.auto = assert

這是我目前的配置:

@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 

修訂

這是我基於博爾特駕駛的生產配置:

@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的配置問題。

+0

您是否將嵌入式驅動程序添加爲依賴項(Maven/Gradle)? –

+0

是的。我的原始配置在我的問題是沒有任何錯誤 – alexanoid

+0

我想我以前見過這個。雖然你只需要嵌入,當你添加所有3個驅動程序時會發生什麼 - http,bolt,embedded? –

回答

2

這似乎是OGM配置中的一個盲點,所以需要跳過一些環節。

有2個問題:

  • 驅動程序類名稱必須設置,否則你將得到NPE
  • ,即使你做,你通過Components.setDriver設置的驅動程序將在new SessionFactory被摧毀(OGM認爲你與自定義配置重新配置它)

對於嵌入式數據庫您sessionFactory()應該如下所示:

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"); 

// Configure auto index 
configuration.autoIndexConfiguration().setAutoIndex("assert"); 

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

它的工作原理,但要小心它是一個黑客。應該罰款測試雖然。

+0

不幸的是它不起作用。我在'org.neo4j.ogm.config.Configuration'找不到'Configuration.Builder()'。我使用'OGM 2.1.1'。另外,我已經更新了我提供'Configuration'實例到'SessionFactory'的地方的問題,但是它現在失敗了,並且出現異常 – alexanoid

+0

@alexanoid你需要圖表數據庫上的loadPropertiesFromFile嗎?如果沒有,您可以在'Configuration'中配置驅動程序類名和URI以及OGM,併爲您創建嵌入式數據庫。 –

+0

這就是爲什麼我已經從簡單的'ogm.properties'配置轉移到基於Java的..我需要'loadPropertiesFromFile'爲了指定https://github.com/graphaware/neo4j-to-elasticsearch插件的屬性 – alexanoid