0

我想用Spring數據連接到Neo4J數據庫使用以下鏈接(http://projects.spring.io/spring-data-neo4j/#quick-start)。但它似乎並不奏效。它無法自行創建Repository Implementation。這就是爲什麼我得到BeanCreationException。這裏我到目前爲止嘗試過。Spring Data Neo4J數據庫連接

  1. PersistenceContext.java
@Configuration 
@EnableTransactionManagement 
@ComponentScan("com.ksh.neo4j") 
@EnableNeo4jRepositories("com.ksh.neo4j.repositories") 
public class PersistenceContext { 

    @Bean 
    public org.neo4j.ogm.config.Configuration getConfiguration() 
    { 
     org.neo4j.ogm.config.Configuration configuration = new org.neo4j.ogm.config.Configuration(); 
     configuration.driverConfiguration() 
        .setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver") 
        .setURI("http://localhost:7474"); 
     return configuration; 
    } 
    @Bean 
    public Neo4jTransactionManager transactionManager() { 
     return new Neo4jTransactionManager(getSessionFactory()); 
    } 
    @Bean 
    public SessionFactory getSessionFactory() { 
     return new SessionFactory("com.ksh.neo4j.domains"); 
    } 
} 
  • PersonRepository.java
  • @Repository 
    public interface PersonRepository extends GraphRepository<Person> { 
    } 
    
  • PersonService.java
  • @Service 
    public class PersonService { 
        @Autowired 
        private PersonRepository personRepository; 
    
        public Person save(Person person) { 
         return personRepository.save(person); 
        } 
    } 
    
  • PersonController.java
  • @RestController 
    @RequestMapping("/person/**") 
    public class PersonController { 
        @Autowired 
        private PersonService personService; 
    
        @RequestMapping("/save") 
        public ResponseObject save(@RequestBody Person person) 
        { 
         try { 
          personService.save(person); 
          return ResponseObjectCreator.createResponseObj(ResponseStatus.SUCSESS); 
         } 
         catch (Exception e) { 
          e.printStackTrace(); 
         } 
         return ResponseObjectCreator.createResponseObj(ResponseStatus.FAILURE); 
        } 
    } 
    
    1. WebMvcConfig.java
    @Configuration 
        @EnableWebMvc 
        @ComponentScan(basePackages = {"com.ksh.neo4j.controllers", "com.ksh.neo4j"}) 
        public class WebMvcConfig extends WebMvcConfigurerAdapter {] 
         @Override 
         public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 
          configurer.enable(); 
         } 
        } 
    
  • 的pom.xml
  • <dependency> 
        <groupId>org.springframework.data</groupId> 
        <artifactId>spring-data-commons</artifactId> 
        <version>1.13.1.RELEASE</version> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework.data</groupId> 
        <artifactId>spring-data-neo4j</artifactId> 
        <version>4.2.1.RELEASE</version> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-web</artifactId> 
        <version>4.3.7.RELEASE</version> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-webmvc</artifactId> 
        <version>4.3.7.RELEASE</version> 
    </dependency> 
    <dependency> 
        <groupId>javax.servlet</groupId> 
        <artifactId>javax.servlet-api</artifactId> 
        <version>3.1.0</version> 
        <scope>compile</scope> 
    </dependency> 
    <dependency> 
        <groupId>org.neo4j</groupId> 
        <artifactId>neo4j-ogm-embedded-driver</artifactId> 
        <version>2.1.2</version> 
    </dependency> 
    <dependency> 
        <groupId>org.neo4j</groupId> 
        <artifactId>neo4j-ogm-core</artifactId> 
        <version>2.1.1</version> 
    </dependency> 
    <dependency> 
        <groupId>org.neo4j</groupId> 
        <artifactId>neo4j-ogm-http-driver</artifactId> 
        <version>2.1.1</version> 
    </dependency> 
    <dependency> 
        <groupId>org.neo4j</groupId> 
        <artifactId>neo4j-kernel</artifactId> 
        <version>3.1.3</version> 
    </dependency> 
    <dependency> 
        <groupId>org.neo4j.app</groupId> 
        <artifactId>neo4j-server</artifactId> 
        <version>3.1.3</version> 
    </dependency> 
    

    我得到以下例外

    所致:org.springframework.beans.BeanInstantiationException: 無法實例[org.neo4j.ogm.session.SessionFactory]:出廠 方法 '提供了getSessionFactory' 拋出異常;嵌套的異常是 org.neo4j.ogm.exception.ServiceNotFoundException:無法加載 driver:null。

    任何幫助都會很好。

    回答

    1

    你沒有說你正在使用的版本,但如果你正在使用4.2.1然後快速啓動似乎缺少事務管理的定義:

    添加下列內容PersistenceContext類:

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

    請參閱spring-data-neo4j文檔以獲取更多詳細信息。

    http://docs.spring.io/spring-data/data-neo4j/docs/current/reference/html/#reference.getting_started.spring-configuration

    +0

    我用的是4.2.1,但是4.2.1版本沒有類org.springframework.data.neo4j.config.Neo4jConfiguration。所以我將版本更改爲4.1.8。你給出的解決方案給我錯誤,說「構造函數Neo4jTransactionManager(SessionFactory)是未定義的」。可能是因爲4.1.8。 – KSHiTiJ

    +0

    是的,對於4.2.1只是刪除'擴展Neo4jConfiguration' –

    +0

    @ frant.harth我已更新異常和pom.xml的問題。我得到「無法加載驅動程序:null」。錯誤。 – KSHiTiJ

    相關問題