Spring Data Neo4j:无法实例化[org.neo4j.ogm.session.SessionFactory]

时间:2017-02-23 20:00:19

标签: spring-data-neo4j

我正在尝试将Neo4j连接添加到我的Spring启动Web应用程序中。我在模板上使用这个example,但似乎无法使事情发挥作用。

我不断收到关于无法创建SessionFactory的运行时错误:“无法实例化[org.neo4j.ogm.session.SessionFactory]:工厂方法'sessionFactory'抛出异常;嵌套异常是java.lang.NullPointerException “

我正在使用spring boot版本1.5.1.RELEASE,这是我的pom.xml文件中的依赖项:

<dependencyManagement>
    <dependencies>
       <dependency>
          <!-- Import dependency management from Spring Boot -->
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-dependencies</artifactId>
         <version>1.5.1.RELEASE</version>
         <type>pom</type>
         <scope>import</scope>
       </dependency>
    </dependencies>
 </dependencyManagement>

<dependencies>

    <!-- For REST HTTP service and injection interfaces-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <!-- For websocket service support -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-messaging</artifactId>
    </dependency>

    <!-- For security features (user authentication, SSL) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

     <!-- For Neo4j integration -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-neo4j</artifactId>
    </dependency>

</dependencies>

这是我的Spring Application Class:

@SpringBootApplication
@EnableTransactionManagement
@EnableNeo4jRepositories(basePackages="mycompany.ocelot.entities")
public class ServiceApp {

    public static void main(String[] args) {
        SpringApplication.run(ServiceApp.class, args);
    }
}

这是我的实体类:

package mycompany.ocelot.entities;

import java.util.HashSet;
import java.util.Set;

import org.neo4j.ogm.annotation.GraphId;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;

@NodeEntity
public class User {

    @GraphId private Long id;

    private String username;

    @SuppressWarnings("unused")
    private User(){
        // required by Neo4j
    }

    public User(String username){
        this.username = username;
    }

    // getters & setters

    public Long getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String toString(){
        return username;
    }

}

这是我的UserRepository类:

package mycompany.ocelot.entities;

import java.util.List;

import org.springframework.data.neo4j.repository.GraphRepository;
import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends GraphRepository<User> {

    User findByUsername(String username);
}

这是运行时错误:

2017-02-23 19:55:04.928  WARN 4387 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'neo4jAuthenticationProvider': Unsatisfied dependency expressed through field 'user_repository'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userRepository': Unsatisfied dependency expressed through method 'setSession' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.data.neo4j.transaction.SharedSessionCreator#0': Cannot resolve reference to bean 'sessionFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.neo4j.ogm.session.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is java.lang.NullPointerException
2017-02-23 19:55:04.932  WARN 4387 --- [           main] o.s.boot.SpringApplication               : Error handling failed (Error creating bean with name 'delegatingApplicationListener' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available)

我尝试添加Neo4j配置类,但我得到了类似的错误。

package mycompany.ocelot.entities;

import org.neo4j.ogm.session.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.transaction.Neo4jTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableNeo4jRepositories(basePackages = "mycompany.ocelot.entities")
@EnableTransactionManagement
public class Neo4jConfiguration {

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

    @Bean
    public SessionFactory sessionFactory() {
        // with domain entity base package(s)
        return new SessionFactory("mycompany.ocelot.entities");
    }

}

更新1: 在尝试调试时,我尝试删除事物以确定问题何时开始发生。我发现只包含对pom文件的依赖会导致相同的异常。所以我猜测我所包含的弹簧模块之间存在一些冲突。

更新2: 我删除了对另一个内部java项目(我没有包含在上面发布的pom文件中)的依赖项,并删除了运行时错误。作为结果,我确实必须在这个项目中注释掉相当多的代码。所以,问题不在于弹簧模块之间,而是介于我们的代码和spring-data-neo4j之间。有没有人有关于如何追踪这类错误的任何指示?日志和堆栈跟踪似乎没有多大帮助,因为它们没有引用我们的代码。我的知识spring的IoC(我认为是问题出在哪里)并不是那么深,所以任何关于从哪里开始的指针都会有所帮助。

3 个答案:

答案 0 :(得分:2)

我从https://spring.io/guides/gs/accessing-data-neo4j运行了应用程序,没有使用HTTP驱动程序的任何问题。然后我添加了你的其他依赖项,也没有任何问题。

尝试使用升级到引导1.5.2并执行以下操作:

  • 删除注释:@EnableTransactionManagement@EnableNeo4jRepositories(basePackages="mycompany.ocelot.entities")。这些应该已经由Spring Boot配置。
  • @EntityScan("<package containing your model code>")
  • 下方添加@SpringBootApplication
  • 删除Neo4jConfiguration

确保您的配置具有适合Neo4j数据库的用户名和密码。

答案 1 :(得分:0)

当我将应用程序升级到Spring Boot 1.5.2版时,我遇到了类似的问题 - 在Neo4j自动配置期间,在各个地方有几个空指针。

问题在于,随着SDN 4.2的发布(新的Spring Boot版本引用),Neo4j的嵌入式驱动程序不再包含neo4j内核(see here)。添加以下两个依赖项后,我的应用程序启动并再次运行。

  <dependency>
     <groupId>org.neo4j</groupId>
     <artifactId>neo4j-ogm-embedded-driver</artifactId>
     <version>2.1.1</version>
  </dependency>
  <dependency>
     <groupId>org.neo4j</groupId>
     <artifactId>neo4j</artifactId>
     <version>3.1.1</version>
  </dependency>

也许这也可以解决您的问题。

答案 2 :(得分:0)

我刚刚在今天早上写的代码中看到了相同的错误:SessionFactory: "Failed to instantiate [org.neo4j.ogm.session.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is java.lang.NullPointerException"

每次启动应用程序(这是一个实现CommandLineRunner并连接到Neo4j图形数据库的Spring Boot应用程序)时,问题都会可靠且可重复地发生。

我一次删除了.java个文件(如D Perkins在上面的“更新2”中建议的那样),直到我有效地将其跟踪到非常简单的一段代码为止:

package com.xxx.xxxx;

import java.nio.file.Path;
import lombok.Data;

@Data
public class MyClass {
    private Path file;
    private String content;
}

我设法通过将类更改为使用java.io.File而不是java.nio.file.Path来避免该问题。即private File file;

我尝试过的不起作用的东西包括:

  • @Data@Getter替换@Setter
  • 从文件中删除Lombok,并使用源生成来创建吸气剂和吸气剂。
  • 删除唯一的名为new MyClass()的代码。

我正在项目中其他地方使用Lombok,没有任何问题。该项目是一个Spring Boot项目。

我希望这些信息对您有所帮助。