Reputation: 161
Can someone tell me how I can create service for an embedded neo4j database using spring-data but set the path to the database at runtime rather than at startup. In my application, the location of the database depends on input from the user. I currently have a database Service bean defined as follows:
@Bean
GraphDatabaseService graphDatabaseService() {
GraphDatabaseService graphDB = new GraphDatabaseFactory().newEmbeddedDatabase("/path/to/db");
return graphDB;
}
This does not work because I don't know what that path should be until after the application has started. Any help here would be appreciated.
Upvotes: 1
Views: 248
Reputation: 5001
I don't think you can do this if you specify this bean already in your configuration class. If you do specify it in this file, make sure that you add the @Bean(destroyMethod = "shutdown")
annotation on your creation method.
You can specify singleton beans at runtime. When you have the database directory, you could do something similar to this:
GraphDatabaseService graphDB = new GraphDatabaseFactory().newEmbeddedDatabase("user_dir");
// get a hold of ConfigurableApplicationContext#getBeanFactory()
beanFactory.registerSingleton("graphDatabaseService", graphDB);
Make sure to shutdown the GraphDatabaseService when the application exits.
Upvotes: 1