Neo4jServer中的Neo4jServer - 4.1.0?

Gam*_*tes 1 spring-data-neo4j spring-data-neo4j-4

我一直在使用4.1.0-BUILD-SNAPSHOT中的最新代码,因为我需要4.1分支中的一些新bug修复,只是注意到"neo4jServer()"不再是Neo4jConfiguration公开的方法.初始化服务器连接和单元测试的内存中版本的新方法是什么?在我分别使用"RemoteServer"和"InProcessServer"之前.

Vin*_*nce 5

请注意,官方文档将很快更新.

同时:

发生了什么变化

SDN 4.1使用新的Neo4j OGM 2.0库.OGM 2.0引入了API更改,主要是因为增加了对Embedded和Remote Neo4j的支持.因此,现在使用适当的方式完成与生产数据库的连接Driver,而不是使用RemoteServerInProcessServer不推荐使用的.

对于测试,我们建议使用EmbeddedDriver.仍然可以创建内存中的测试服务器,但这个答案中没有涉及.

可用驱动程序

Driver目前提供以下实现

  • http:org.neo4j.drivers.http.driver.HttpDriver
  • embedded:org.neo4j.drivers.embedded.driver.EmbeddedDriver

Bolt协议(Neo4j 3.0)的驱动程序实现即将推出.

配置驱动程序

有两种方法可以配置驱动程序 - 使用属性文件或通过Java配置.存在这些主题的变化(特别是对于传递凭证),但是现在以下内容应该让您前进:


配置Http驱动程序

Http驱动程序通过Http连接到Neo4j服务器并与之通信.如果您的应用程序在客户端 - 服务器模式下运行,则必须使用Http驱动程序.请注意,Http驱动程序将尝试连接到在单独进程中运行的服务器.它不能用于启动进程内服务器.

属性文件配置:

使用属性文件的优点是它不需要更改Spring配置.

ogm.properties在类路径上创建一个名为somewhere 的文件.它应包含以下条目:

  driver=org.neo4j.ogm.drivers.http.driver.HttpDriver
  URI=http://user:password@localhost:7474
Run Code Online (Sandbox Code Playgroud)

Java配置:

配置驱动程序最简单的方法是创建一个Configurationbean并将其作为SessionFactorySpring配置中构造函数的第一个参数传递:

import org.neo4j.ogm.config.Configuration;
...

@Bean 
public Configuration getConfiguration() {
   Configuration config = new Configuration();
   config
       .driverConfiguration()
       .setDriverClassName
        ("org.neo4j.ogm.drivers.http.driver.HttpDriver")
       .setURI("http://user:password@localhost:7474");
   return config;
}

@Bean 
public SessionFactory getSessionFactory() {
    return new SessionFactory(getConfiguration(), <packages> );
}
Run Code Online (Sandbox Code Playgroud)


配置嵌入式驱动程序

嵌入式驱动程序直接连接到Neo4j数据库引擎.没有涉及服务器,因此应用程序代码和数据库之间没有网络开销.如果您不想使用客户端 - 服务器模型,或者您的应用程序作为Neo4j非托管扩展运行,则应使用嵌入式驱动程序.

您可以指定永久数据存储位置,以便在应用程序关闭后提供数据的持久性,或者您可以使用非永久性数据存储,该存储仅在应用程序运行时存在(非常适合测试).

ogm.properties在类路径上创建一个名为somewhere 的文件.它应包含以下条目:

属性文件配置(永久数据存储)

driver=org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver
URI=file:///var/tmp/graph.db
Run Code Online (Sandbox Code Playgroud)

属性文件配置(非永久数据存储)

driver=org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver
Run Code Online (Sandbox Code Playgroud)

要使用非永久数据存储,只需省略URI属性即可.

Java配置

与Http驱动程序一样,使用相同的技术配置嵌入式驱动程序.设置Configuration bean并将其作为SessionFactory构造函数的第一个参数传递:

import org.neo4j.ogm.config.Configuration;
...

@Bean 
public Configuration getConfiguration() {
   Configuration config = new Configuration();
   config
       .driverConfiguration()
       .setDriverClassName
        ("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver")
       .setURI("file:///var/tmp/graph.db");
   return config;
}

@Bean 
public SessionFactory getSessionFactory() {
    return new SessionFactory(getConfiguration(), <packages> );
}
Run Code Online (Sandbox Code Playgroud)

如果要使用非永久数据存储(例如,用于测试),请不要在配置上设置URI属性:

@Bean 
public Configuration getConfiguration() {
   Configuration config = new Configuration();
   config
       .driverConfiguration()
       .setDriverClassName
        ("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver")
   return config;
}
Run Code Online (Sandbox Code Playgroud)