【发布时间】:2015-10-09 08:01:49
【问题描述】:
我有一个用例,用户可以连接到不同的 neo4j 数据库。我正在使用弹簧数据。我的配置如下:
@Configuration
@ComponentScan("org.inno")
@EnableTransactionManagement
@EnableNeo4jRepositories("org.inno.dao")
public class AppConfig extends Neo4jConfiguration {
public AppConfig() {
setBasePackage("org.inno.model");
}
@Bean
@Lazy
public GraphDatabaseService graphDatabaseService() {
//url may change based on the user preferences
String url = "http://localhost:7474/db/data/";
return new SpringRestGraphDatabase(url);
}
}
我使用 GraphDatabaseService 作为存储库。
我的问题:如何在运行时连接到不同的数据库?我不是在谈论应用程序的启动。在那里我可以使用 Preferences API。我说的是一个我已经连接到服务器但想切换到另一个服务器的用例。不幸的是,该服务仅提供关闭方法,但没有重新连接到另一个 URL。我是否必须通过访问 ApplicationContext 来销毁 GraphDatabaseService 的 bean?还是有其他方法?
【问题讨论】:
-
您可以使用第二个 URL 定义为第二个 bean。您可能还应该使用 SpringCypherRestGraphDatabase
-
是的,我知道 SpringRestGraphDatabase 已被弃用,它只是一个示例。但是你的提议意味着我只能切换一次不同的 URL。
标签: spring neo4j spring-data-neo4j