Skip to content

Commit c0b9974

Browse files
Thomas Darimontodrotbohm
authored andcommitted
DATAMONGO-742 - Document CDI integration in reference documentation.
Added chapter for CDI Integration under the new chapter Miscellaneous. Original pull request: spring-projects#63.
1 parent 595bbd3 commit c0b9974

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/docbkx/reference/mongo-repositories.xml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,4 +542,52 @@ Page<Person> page = repository.findAll(person.lastname.contains("a"),
542542
MongoDB queries.</para>
543543
</section>
544544
</section>
545+
546+
<section>
547+
<title>Miscellaneous</title>
548+
549+
<para/>
550+
551+
<section>
552+
<title>CDI Integration</title>
553+
554+
<para>Instances of the repository interfaces are usually created by a
555+
container, which Spring is the most natural choice when working with
556+
Spring Data. As of version 1.3.0 Spring Data MongoDB ships with a custom
557+
CDI extension that allows using the repository abstraction in CDI
558+
environments. The extension is part of the JAR so all you need to do to
559+
activate it is dropping the Spring Data MongoDB JAR into your classpath.
560+
You can now set up the infrastructure by implementing a CDI Producer for
561+
the <classname>MongoTemplate</classname>:</para>
562+
563+
<programlisting language="java">class MongoTemplateProducer {
564+
565+
@Produces
566+
@ApplicationScoped
567+
public MongoOperations createMongoTemplate() throws UnknownHostException, MongoException {
568+
569+
MongoDbFactory factory = new SimpleMongoDbFactory(new Mongo(), "database");
570+
return new MongoTemplate(factory);
571+
}
572+
}</programlisting>
573+
574+
<para>The Spring Data MongoDB CDI extension will pick up the
575+
<classname>MongoTemplate</classname> available as CDI bean and create a
576+
proxy for a Spring Data repository whenever an bean of a repository type
577+
is requested by the container. Thus obtaining an instance of a Spring
578+
Data repository is a matter of declaring an <code>@Inject</code>-ed
579+
property:</para>
580+
581+
<programlisting language="java">class RepositoryClient {
582+
583+
@Inject
584+
PersonRepository repository;
585+
586+
public void businessMethod() {
587+
588+
List&lt;Person&gt; people = repository.findAll();
589+
}
590+
}</programlisting>
591+
</section>
592+
</section>
545593
</chapter>

0 commit comments

Comments
 (0)