Spring Boot + Neo4j获取GraphDatabaseService bean?

时间:2016-08-22 16:32:57

标签: spring-boot spring-data-neo4j spring-data-neo4j-4

使用新风格(Spring Data Neo4j 4.1.2.RELEASE)Neo4jConfiguration可以获取对底层嵌入式GraphDatabaseService的引用以传递给web ui吗?

新款配置:

@Configuration
@EnableNeo4jRepositories(basePackages = "fu.bar")
@EnableTransactionManagement
public class Neo4j extends Neo4jConfiguration {

    @Bean
    @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON, proxyMode = ScopedProxyMode.TARGET_CLASS)
    public Session getSession() throws Exception {
        return super.getSession();
    }

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

    @Bean
    public SessionFactory getSessionFactory() {
        SessionFactory sessionFactory = new SessionFactory(getConfiguration(), "fu.bar");
        return sessionFactory;
    }

我没有在Javadoc中看到任何有帮助的东西,但我怀疑Boot在某个地方有一个实例。

感谢。

1 个答案:

答案 0 :(得分:2)

如果您正在使用嵌入式驱动程序,则可以按如下方式获取GraphDatabaseService

EmbeddedDriver embeddedDriver = (EmbeddedDriver) Components.driver();
GraphDatabaseService databaseService = embeddedDriver.getGraphDatabaseService();

使用HTTP,可以通过以下方式直接访问数据库:

String uri = Components.driver().getConfiguration().getURI() +
                        "/db/data/index/node/" + indexName;
HttpPost httpPost = new HttpPost(uri);

这些示例来自Spring Data Neo4j参考指南的section on indexes