在SDN 5.0.0中创建neo4j sessionfactory

时间:2017-10-18 14:29:54

标签: java spring spring-boot spring-data-neo4j spring-data-neo4j-5

背景

我有一个Maven,Spring数据Neo4j项目。它发布并获取单个对象。我正在尝试添加会话,但遇到错误。

有文档:https://docs.spring.io/spring-data/neo4j/docs/current/reference/html/#reference:session

他们建议:

@Configuration
@EnableNeo4jRepositories(basePackages = "org.neo4j.example.repository")
@EnableTransactionManagement
public class MyConfiguration {

    @Bean
    public SessionFactory sessionFactory() {
        // with domain entity base package(s)
        return new SessionFactory(configuration(), "org.neo4j.example.domain");
    }

    @Bean
    public org.neo4j.ogm.config.Configuration configuration() {
        ConfigurationSource properties = new ClasspathConfigurationSource("ogm.properties");
        org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration.Builder(properties)
        return configuration;
    }

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

}

但是当我将第二个@Bean我的eclipse标记:new org.neo4j.ogm.config.Configuration.Builder(properties)复制为红色并建议我将类型配置更改为Builder。然后继续建议更改,直到第二个@Bean看起来像:

@Bean
public Builder configuration() {
    ConfigurationSource properties = new ClasspathConfigurationSource("ogm.properties");
    Builder configuration = new org.neo4j.ogm.config.Configuration.Builder(properties);
    return configuration;
}

但是第一个@Bean停止工作,因为它需要configuration()

问题

那么,当当前doc的建议不起作用时,如何添加sessionfactory呢?我已经查看了许多其他示例,但它们都使用旧版本和Neo4jConfiguration

等弃用的内容

备注

我的pom.xml没有父项和这些依赖项和存储库:

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-neo4j</artifactId>
        <version>5.0.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.0.0.M3</version>
    </dependency>



<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
        <version>2.0.0.M3</version>
    </dependency>

    <dependency>
        <groupId>com.voodoodyne.jackson.jsog</groupId>
        <artifactId>jackson-jsog</artifactId>
        <version>1.1</version>
        <scope>compile</scope>
    </dependency>



  </dependencies>



  <repositories>
    <repository>
        <id>spring-libs-release</id>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/libs-release</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

更新1

删除所有@Bean并尝试此人设置:Why does Neo4j OGM with Spring Boot 2.0.0.M4 apparently require the embedded driver? 像这样:

@Configuration
@EnableNeo4jRepositories("com.myproject.repository")
@EnableTransactionManagement
public class ServiceRegistryConfiguration {

    @Bean
    public org.neo4j.ogm.config.Configuration configuration() {
        return new org.neo4j.ogm.config.Configuration.Builder(new ClasspathConfigurationSource("ogm.properties")).build();
    }
    @Bean
    public SessionFactory sessionFactory() {
        return new SessionFactory(configuration(), "com.myproject.domain");
    }

}

导致了这个错误:

 ...Factory method 'configuration' threw exception; nested exception is java.lang.NullPointerException

我的ogm.properties文件是:

server.port=8585
spring.data.neo4j.uri=bolt://localhost
spring.data.neo4j.username=myuser
spring.data.neo4j.password=mypass

1 个答案:

答案 0 :(得分:2)

您的初始配置看起来很不错,除了在构建器实例化之后缺少 .build() (它是文档中的拼写错误)。

所以配置init应该是:

new org.neo4j.ogm.config.Configuration.Builder(properties).build();

关于第二种:你不能混合SDN样式配置和Spring Boot自动配置。

使用 SDN格式(如第一个代码段中所示),ogm.properties文件如下:

URI=bolt://localhost
username=myUser
password=myPassword

示例here

OR 您使用 Spring Boot自动配置,然后无需在配置类中声明配置,sessionFactory和事务管理器。只需使用

注释配置
@EnableNeo4jRepositories("com.myproject.repository")
@EntityScan(basePackages = "com.myproject.domain")

并使用spring boot属性格式:

spring.data.neo4j.uri=bolt://localhost
spring.data.neo4j.username=myuser
spring.data.neo4j.password=mypass

示例here

关于NullPointerException:它是因为在类路径中找不到ogm.properties文件。