Spring Data MongoDB - Spring Web Services - Parent - SpringSource

Spring Data MongoDB - Spring Web Services - Parent - SpringSource Spring Data MongoDB - Spring Web Services - Parent - SpringSource

static.springsource.org
from static.springsource.org More from this publisher
13.07.2015 Views

please define productname in your docbook file!@Configurationpublic class AppConfig {}/** Use the standard Mongo driver API to create a com.mongodb.Mongo instance.*/public @Bean Mongo mongo() throws UnknownHostException {return new Mongo("localhost");}Example 5.1 Registering a com.mongodb.Mongo object using Java based bean metadataThis approach allows you to use the standard com.mongodb.Mongo API that you may already be usedto using but also pollutes the code with the UnknownHostException checked exception. The use of thechecked exception is not desirable as Java based bean metadata uses methods as a means to setobject dependencies, making the calling code cluttered.An alternative is to register an instance of com.mongodb.Mongo instance with the container usingSpring's MongoFactoryBean. As compared to instantiating a com.mongodb.Mongo instance directly,the FactoryBean approach does not throw a checked exception and has the added advantage ofalso providing the container with an ExceptionTranslator implementation that translates MongoDBexceptions to exceptions in Spring's portable DataAccessException hierarchy for data accessclasses annoated with the @Repository annotation. This hierarchy and use of @Repository isdescribed in Spring's DAO support features.An example of a Java based bean metadata that supports exception translation on @Repositoryannotated classes is shown below:@Configurationpublic class AppConfig {}/** Factory bean that creates the com.mongodb.Mongo instance*/public @Bean MongoFactoryBean mongo() {MongoFactoryBean mongo = new MongoFactoryBean();mongo.setHost("localhost");return mongo;}To access the com.mongodb.Mongo object created by the MongoFactoryBean in other@Configuration or your own classes, use a "private @Autowired Mongo mongo;" field.Example 5.2 Registering a com.mongodb.Mongo object using Spring's MongoFactoryBean andenabling Spring's exception translation supportRegistering a Mongo instance using XML based metadataWhile you can use Spring's traditional XML namespace to register an instance ofcom.mongodb.Mongo with the container, the XML can be quite verbose as it is general purpose. XMLnamespaces are a better alternative to configuring commonly used objects such as the Mongo instance.The mongo namespace alows you to create a Mongo instance server location, replica-sets, and options.To use the Mongo namespace elements you will need to reference the Mongo schema:1.4.0.BUILD-SNAPSHOTSpring Data MongoDB -Reference Documentation 29

please define productname in your docbook file!Example 5.3 XML schema to configure MongoDBA more advanced configuration with MongoOptions is shown below (note these are not recommendedvalues)Example 5.4 XML schema to configure a com.mongodb.Mongo object with MongoOptionsA configuration using replica sets is shown below.Example 5.5 XML schema to configure com.mongodb.Mongo object with Replica SetsThe MongoDbFactory interfaceWhile com.mongodb.Mongo is the entry point to the MongoDB driver API, connecting to a specificMongoDB database instance requires additional information such as the database name andan optional username and password. With that information you can obtain a com.mongodb.DBobject and access all the functionality of a specific MongoDB database instance. Spring providesthe org.springframework.data.mongodb.core.MongoDbFactory interface shown below tobootstrap connectivity to the database.1.4.0.BUILD-SNAPSHOTSpring Data MongoDB -Reference Documentation 30

please define productname in your docbook file!@Configurationpublic class AppConfig {}/** Use the standard Mongo driver API to create a com.mongodb.Mongo instance.*/public @Bean Mongo mongo() throws UnknownHostException {return new Mongo("localhost");}Example 5.1 Registering a com.mongodb.Mongo object using Java based bean metadataThis approach allows you to use the standard com.mongodb.Mongo API that you may already be usedto using but also pollutes the code with the UnknownHostException checked exception. The use of thechecked exception is not desirable as Java based bean metadata uses methods as a means to setobject dependencies, making the calling code cluttered.An alternative is to register an instance of com.mongodb.Mongo instance with the container using<strong>Spring</strong>'s MongoFactoryBean. As compared to instantiating a com.mongodb.Mongo instance directly,the FactoryBean approach does not throw a checked exception and has the added advantage ofalso providing the container with an ExceptionTranslator implementation that translates <strong>MongoDB</strong>exceptions to exceptions in <strong>Spring</strong>'s portable <strong>Data</strong>AccessException hierarchy for data accessclasses annoated with the @Repository annotation. This hierarchy and use of @Repository isdescribed in <strong>Spring</strong>'s DAO support features.An example of a Java based bean metadata that supports exception translation on @Repositoryannotated classes is shown below:@Configurationpublic class AppConfig {}/** Factory bean that creates the com.mongodb.Mongo instance*/public @Bean MongoFactoryBean mongo() {MongoFactoryBean mongo = new MongoFactoryBean();mongo.setHost("localhost");return mongo;}To access the com.mongodb.Mongo object created by the MongoFactoryBean in other@Configuration or your own classes, use a "private @Autowired Mongo mongo;" field.Example 5.2 Registering a com.mongodb.Mongo object using <strong>Spring</strong>'s MongoFactoryBean andenabling <strong>Spring</strong>'s exception translation supportRegistering a Mongo instance using XML based metadataWhile you can use <strong>Spring</strong>'s traditional XML namespace to register an instance ofcom.mongodb.Mongo with the container, the XML can be quite verbose as it is general purpose. XMLnamespaces are a better alternative to configuring commonly used objects such as the Mongo instance.The mongo namespace alows you to create a Mongo instance server location, replica-sets, and options.To use the Mongo namespace elements you will need to reference the Mongo schema:1.4.0.BUILD-SNAPSHOT<strong>Spring</strong> <strong>Data</strong> <strong>MongoDB</strong> -Reference Documentation 29

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!