【发布时间】: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