We are deploying a spring-based web application on Tomcat, and we would like to provide the user with the ability to use the mongo database to connect. To do this, we have a setup wizard that captures the details of the Mongo connection from the user through the online form. After collecting these details, we would like to dynamically connect to Mongo. Now we load our mongo through the xml configuration:
<bean id="configmapper" class="com.db.util.ConfigurationMapper" />
<bean id="configmap" factory-bean="configmapper" factory-method="readXML" />
<mongo:mongo host="#{configmap.getHost()}" port="#{configmap.getPort()}" />
<mongo:db-factory dbname="#{configmap.getName()}"
mongo-ref="mongo" />
User values are stored in read / write mode by the bean configuration card reader, but after installing them, we want to basically restart the mongo driver. To do this, we try:
XmlWebApplicationContext context = ((XmlWebApplicationContext) ContextLoader
.getCurrentWebApplicationContext());
context.refresh();
beans, Mongo beans, beans :
DEBUG 2015-05-12 21:09:29,590 [http-nio-8087-exec-6] (DefaultSingletonBeanRegistry.java:221) - Creating shared instance of singleton bean 'mongo'
...
DEBUG 2015-05-12 21:09:08,121 [localhost-startStop-1] (DefaultSingletonBeanRegistry.java:221) - Creating shared instance of singleton bean 'mongoTemplate'
...
DEBUG 2015-05-12 21:09:29,590 [http-nio-8087-exec-6] (AbstractAutowireCapableBeanFactory.java:449) - Creating instance of bean 'mongoDbFactory'
, , :
Caused by: java.lang.IllegalStateException: open
at org.bson.util.Assertions.isTrue(Assertions.java:36)
at com.mongodb.DBTCPConnector.isMongosConnection(DBTCPConnector.java:367)
at com.mongodb.Mongo.isMongosConnection(Mongo.java:622)
at com.mongodb.DBCollection.findOne(DBCollection.java:936)
at com.mongodb.DBCollection.findOne(DBCollection.java:914)
at com.mongodb.DBCollection.findOne(DBCollection.java:858)
at org.springframework.data.mongodb.core.MongoTemplate$FindOneCallback.doInCollection(MongoTemplate.java:2069)
at org.springframework.data.mongodb.core.MongoTemplate$FindOneCallback.doInCollection(MongoTemplate.java:2053)
at org.springframework.data.mongodb.core.MongoTemplate.executeFindOneInternal(MongoTemplate.java:1828)
at org.springframework.data.mongodb.core.MongoTemplate.doFindOne(MongoTemplate.java:1645)
at org.springframework.data.mongodb.core.MongoTemplate.findOne(MongoTemplate.java:560)
, , , , bean :
@Autowired
private Mongo mongo;
...
mongo.close();
XmlWebApplicationContext context = ((XmlWebApplicationContext) ContextLoader
.getCurrentWebApplicationContext());
context.refresh();
.
- , , ? , ? - GUI, .
, .