重新连接到Neo4j REST服务
问题描述:
我有一个用户可以连接到不同的neo4j数据库的用例。我正在使用弹簧数据。我的配置如下所示:重新连接到Neo4j REST服务
@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?还是有另一种方式?
答
你的实际使用情况是?
你也可以实例化一个Neo4jTemplate
直接在构造函数中传递一个SpringRestGraphDatabase
的实例。
+0
我有一个客户端,用户可以在对话框中输入任何网址,并且可以保存图形而无需重新启动客户端。我只跟随了教程@ spring-data-neo4j,其中存储库是使用代理创建的。我还没有关于模板的线索。也许这是一个解决方案。 – Spindizzy
您可以将第二个URL定义为第二个bean。您应该也许也使用SpringCypherRestGraphDatabase –
是的,我知道SpringRestGraphDatabase已被弃用,它只是一个示例。但是你的建议意味着我只能切换一次不同的URL。 – Spindizzy