You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/asciidoc/reference/mongodb.adoc
+5-5
Original file line number
Diff line number
Diff line change
@@ -195,11 +195,11 @@ public class AppConfig {
195
195
196
196
This approach allows you to use the standard `com.mongodb.Mongo` API that you may already be used to using but also pollutes the code with the UnknownHostException checked exception. The use of the checked exception is not desirable as Java based bean metadata uses methods as a means to set object dependencies, making the calling code cluttered.
197
197
198
-
An alternative is to register an instance of `com.mongodb.Mongo` instance with the container using Spring'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 of also providing the container with an ExceptionTranslator implementation that translates MongoDB exceptions to exceptions in Spring's portable `DataAccessException` hierarchy for data access classes annoated with the `@Repository` annotation. This hierarchy and use of `@Repository` is described in http://docs.spring.io/spring/docs/current/spring-framework-reference/html/dao.html[Spring's DAO support features].
198
+
An alternative is to register an instance of `com.mongodb.Mongo` instance with the container using Spring's `MongoClientFactoryBean`. As compared to instantiating a `com.mongodb.Mongo` instance directly, the FactoryBean approach does not throw a checked exception and has the added advantage of also providing the container with an ExceptionTranslator implementation that translates MongoDB exceptions to exceptions in Spring's portable `DataAccessException` hierarchy for data access classes annoated with the `@Repository` annotation. This hierarchy and use of `@Repository` is described in http://docs.spring.io/spring/docs/current/spring-framework-reference/html/dao.html[Spring's DAO support features].
199
199
200
200
An example of a Java based bean metadata that supports exception translation on `@Repository` annotated classes is shown below:
201
201
202
-
.Registering a com.mongodb.Mongo object using Spring's MongoFactoryBean and enabling Spring's exception translation support
202
+
.Registering a com.mongodb.Mongo object using Spring's MongoClientFactoryBean and enabling Spring's exception translation support
203
203
====
204
204
[source,java]
205
205
----
@@ -209,16 +209,16 @@ public class AppConfig {
209
209
/*
210
210
* Factory bean that creates the com.mongodb.Mongo instance
211
211
*/
212
-
public @Bean MongoFactoryBean mongo() {
213
-
MongoFactoryBean mongo = new MongoFactoryBean();
212
+
public @Bean MongoClientFactoryBean mongo() {
213
+
MongoClientFactoryBean mongo = new MongoClientFactoryBean();
214
214
mongo.setHost("localhost");
215
215
return mongo;
216
216
}
217
217
}
218
218
----
219
219
====
220
220
221
-
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.
221
+
To access the `com.mongodb.Mongo` object created by the `MongoClientFactoryBean` in other `@Configuration` or your own classes, use a "`private @Autowired Mongo mongo;`" field.
222
222
223
223
[[mongo.mongo-xml-config]]
224
224
=== Registering a Mongo instance using XML based metadata
0 commit comments