簡體   English   中英

在運行時設置嵌入式neo4j數據庫的路徑

[英]Set path to embedded neo4j database at runtime

有人可以告訴我如何使用spring-data為嵌入式neo4j數據庫創建服務,但如何在運行時而非啟動時設置數據庫的路徑。 在我的應用程序中,數據庫的位置取決於用戶的輸入。 我目前有一個數據庫Service Bean,定義如下:

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

這不起作用,因為直到應用程序啟動后,我才知道該路徑應該是什么。 在這里的任何幫助,將不勝感激。

如果您已經在配置類中指定了此bean,我認為您不能這樣做。 如果確實在此文件中指定了它,請確保在創建方法上添加@Bean(destroyMethod = "shutdown")批注。

您可以在運行時指定單例bean。 當您擁有數據庫目錄時,可以執行以下操作:

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

確保在應用程序退出時關閉GraphDatabaseService。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM