Spring Data MongoDB - Spring Web Services - Parent - SpringSource
Spring Data MongoDB - Spring Web Services - Parent - SpringSource Spring Data MongoDB - Spring Web Services - Parent - SpringSource
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
- Page 1 and 2: Spring Data MongoDB - Reference Doc
- Page 3 and 4: please define productname in your d
- Page 5 and 6: please define productname in your d
- Page 7 and 8: please define productname in your d
- Page 9 and 10: please define productname in your d
- Page 11: please define productname in your d
- Page 14 and 15: please define productname in your d
- Page 16 and 17: please define productname in your d
- Page 18 and 19: please define productname in your d
- Page 20 and 21: please define productname in your d
- Page 22 and 23: please define productname in your d
- Page 24 and 25: please define productname in your d
- Page 26 and 27: please define productname in your d
- Page 28 and 29: please define productname in your d
- Page 30 and 31: please define productname in your d
- Page 32 and 33: please define productname in your d
- Page 36 and 37: please define productname in your d
- Page 38 and 39: please define productname in your d
- Page 40 and 41: please define productname in your d
- Page 42 and 43: please define productname in your d
- Page 44 and 45: please define productname in your d
- Page 46 and 47: please define productname in your d
- Page 48 and 49: please define productname in your d
- Page 50 and 51: please define productname in your d
- Page 52 and 53: please define productname in your d
- Page 54 and 55: please define productname in your d
- Page 56 and 57: please define productname in your d
- Page 58 and 59: please define productname in your d
- Page 60 and 61: please define productname in your d
- Page 62 and 63: please define productname in your d
- Page 64 and 65: please define productname in your d
- Page 66 and 67: please define productname in your d
- Page 68 and 69: please define productname in your d
- Page 70 and 71: please define productname in your d
- Page 72 and 73: please define productname in your d
- Page 74 and 75: please define productname in your d
- Page 76 and 77: please define productname in your d
- Page 78 and 79: please define productname in your d
- Page 80 and 81: please define productname in your d
- Page 82 and 83: please define productname in your d
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