@@ -542,4 +542,52 @@ Page<Person> page = repository.findAll(person.lastname.contains("a"),
542
542
MongoDB queries.</para >
543
543
</section >
544
544
</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< Person> people = repository.findAll();
589
+ }
590
+ }</programlisting >
591
+ </section >
592
+ </section >
545
593
</chapter >
0 commit comments