DATADOC-88 - Create MongoDbFactory to consolidate DB, Server location, and user credentials into one location

DATADOC-147 - Update reference documentation to cover changes from M2 to M3 (partial work)
This commit is contained in:
Mark Pollack
2011-05-24 18:07:18 -04:00
parent c7c2a66c3b
commit e1f8eee2d1
3 changed files with 68 additions and 3 deletions

View File

@@ -22,6 +22,7 @@ import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
@@ -84,6 +85,7 @@ public class MongoDbFactoryParser extends AbstractBeanDefinitionParser {
String mongoRef = element.getAttribute("mongo-ref");
String mongoId = null;
if (!StringUtils.hasText(mongoRef)) {
BeanDefinitionBuilder mongoBuilder = BeanDefinitionBuilder.genericBeanDefinition(MongoFactoryBean.class);
Element mongoEl = DomUtils.getChildElementByTagName(element, "mongo");
@@ -94,11 +96,24 @@ public class MongoDbFactoryParser extends AbstractBeanDefinitionParser {
mongoBuilder.addPropertyValue("port", (StringUtils.hasText(overridePort) ? overridePort : port));
ParsingUtils.parseMongoOptions(parserContext, mongoEl, mongoBuilder);
ParsingUtils.parseReplicaSet(parserContext, mongoEl, mongoBuilder);
String innerId = mongoEl.getAttribute("id");
if (StringUtils.hasText(innerId)) {
mongoId = innerId;
}
}
else {
mongoBuilder.addPropertyValue("host", host);
mongoBuilder.addPropertyValue("port", port);
}
/* MLP - WIP
if (mongoId == null) {
mongoRef = BeanDefinitionReaderUtils.registerWithGeneratedName(mongoBuilder.getBeanDefinition(), parserContext.getRegistry());
} else {
registry.registerBeanDefinition(MONGO, mongoBuilder.getBeanDefinition());
mongoRef = MONGO;
}
*/
registry.registerBeanDefinition(MONGO, mongoBuilder.getBeanDefinition());
mongoRef = MONGO;
}

View File

@@ -10,7 +10,7 @@
<context:property-placeholder
location="classpath:/org/springframework/data/document/mongodb/config/mongo.properties"/>
<mongo:db-factory dbname="database">
<mongo:db-factory dbname="database" >
<mongo:mongo host="${mongo.host}" port="${mongo.port}">
<mongo:options
connections-per-host="${mongo.connectionsPerHost}"

View File

@@ -473,8 +473,58 @@ public class AppConfig {
<title>Registering a MongoDbFactory instance using Java based
metadata</title>
<para>As an alternative to configuring a com.mongodb.Mongo instance and
later providing the database name, the MongoDbFactory </para>
<para>As an alternative to configuring a
<classname>com.mongodb.Mongo</classname> instance and later providing
the database name and optionally the username and password, is to use
<classname>SimpleMongoDbFactory</classname>, which implements the
<interfacename>MongoDbFactory</interfacename> inteface shown below. A
reference to <interfacename>MongoDbFactory</interfacename> can be passed
to constructors of <classname>MongoTemplate</classname> to simplify its
creation.</para>
<programlisting language="java">public interface MongoDbFactory {
DB getDb() throws DataAccessException;
DB getDb(String dbName) throws DataAccessException;
}</programlisting>
<para>The most simple usage is shown below</para>
<programlisting>@Configuration
public class MongoConfiguration {
public @Bean MongoDbFactory mongoDbFactory() throws Exception {
return new SimpleMongoDbFactory(new Mongo(), "database");
}
}</programlisting>
<para>To define the username and password create an instance of
org.springframework.data.authentication.UserCredentials and pass it into
the constructor as shown below</para>
<programlisting>@Configuration
public class MongoConfiguration {
public @Bean MongoDbFactory mongoDbFactory() throws Exception {
UserCredentials userCredentials = new UserCredentials("joe", "secret");
return new SimpleMongoDbFactory(new Mongo(), "database", userCredentials);
}
}
</programlisting>
</section>
<section>
<title>Registering a Mongo instance using XML based metadata</title>
<para>The mongo namespace lets you create a MongoDbFactory instance
which is a convenient way to group together the a mongo instance, a
database name and an optional username and password. A simple usage is
shown below</para>
<programlisting language="xml"> &lt;mongo:db-factory dbname="database" &gt;</programlisting>
</section>
</section>