【问题标题】:Creating a neo4j sessionfactory in SDN 5.0.0在 SDN 5.0.0 中创建 neo4j sessionfactory
【发布时间】:2017-10-18 14:29:54
【问题描述】:

背景

我有一个 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()

问题

那么,当当前文档的建议不起作用时,如何添加会话工厂?我查看了许多其他示例,但它们都使用旧版本和不推荐使用的东西,例如 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

删除所有@Beans 并尝试此人设置: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

【问题讨论】:

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


    【解决方案1】:

    您的初始配置看起来不错,只是在构建器实例化后缺少 .build()(这是文档中的错字)。

    所以配置初始化应该是:

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

    关于第二个:你不能混合使用 SDN 风格的配置和 Spring Boot 自动配置。

    要么您使用 SDN 风格(如在您的第一个代码 sn-p 中),ogm.properties 文件看起来像:

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

    例如here

    或者你使用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 文件。

    【讨论】:

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