【问题标题】:Set path to embedded neo4j database at runtime在运行时设置嵌入式 Neo4j 数据库的路径
【发布时间】:2014-03-13 19:45:58
【问题描述】:

谁能告诉我如何使用 spring-data 为嵌入式 Neo4j 数据库创建服务,但在运行时而不是在启动时设置数据库的路径。在我的应用程序中,数据库的位置取决于用户的输入。我目前有一个数据库服务 bean 定义如下:

@Bean
GraphDatabaseService graphDatabaseService() {
  GraphDatabaseService graphDB = new GraphDatabaseFactory().newEmbeddedDatabase("/path/to/db");
  return graphDB;
}

这不起作用,因为在应用程序启动之前我不知道该路径应该是什么。这里的任何帮助将不胜感激。

【问题讨论】:

    标签: java neo4j spring-data


    【解决方案1】:

    如果你已经在你的配置类中指定了这个bean,我认为你不能这样做。如果您确实在此文件中指定了它,请确保在您的创建方法中添加 @Bean(destroyMethod = "shutdown") 注释。

    您可以在运行时指定单例 bean。当你有数据库目录时,你可以做类似这样的事情:

    GraphDatabaseService graphDB = new GraphDatabaseFactory().newEmbeddedDatabase("user_dir");
    // get a hold of ConfigurableApplicationContext#getBeanFactory()
    beanFactory.registerSingleton("graphDatabaseService", graphDB);
    

    确保在应用程序退出时关闭 GraphDatabaseService。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多