diff --git a/src/docbkx/reference/mongo-repositories.xml b/src/docbkx/reference/mongo-repositories.xml index 58d2b7231..03ec90f8d 100644 --- a/src/docbkx/reference/mongo-repositories.xml +++ b/src/docbkx/reference/mongo-repositories.xml @@ -542,4 +542,52 @@ Page<Person> page = repository.findAll(person.lastname.contains("a"), MongoDB queries. + +
+ Miscellaneous + + + +
+ CDI Integration + + Instances of the repository interfaces are usually created by a + container, which Spring is the most natural choice when working with + Spring Data. As of version 1.3.0 Spring Data MongoDB ships with a custom + CDI extension that allows using the repository abstraction in CDI + environments. The extension is part of the JAR so all you need to do to + activate it is dropping the Spring Data MongoDB JAR into your classpath. + You can now set up the infrastructure by implementing a CDI Producer for + the MongoTemplate: + + class MongoTemplateProducer { + + @Produces + @ApplicationScoped + public MongoOperations createMongoTemplate() throws UnknownHostException, MongoException { + + MongoDbFactory factory = new SimpleMongoDbFactory(new Mongo(), "database"); + return new MongoTemplate(factory); + } +} + + The Spring Data MongoDB CDI extension will pick up the + MongoTemplate available as CDI bean and create a + proxy for a Spring Data repository whenever an bean of a repository type + is requested by the container. Thus obtaining an instance of a Spring + Data repository is a matter of declaring an @Inject-ed + property: + + class RepositoryClient { + + @Inject + PersonRepository repository; + + public void businessMethod() { + + List<Person> people = repository.findAll(); + } +} +
+