|
|
|
@ -542,4 +542,52 @@ Page<Person> page = repository.findAll(person.lastname.contains("a"), |
|
|
|
MongoDB queries.</para> |
|
|
|
MongoDB queries.</para> |
|
|
|
</section> |
|
|
|
</section> |
|
|
|
</section> |
|
|
|
</section> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
|
|
|
|
<title>Miscellaneous</title> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<para/> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section> |
|
|
|
|
|
|
|
<title>CDI Integration</title> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<para>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 <classname>MongoTemplate</classname>:</para> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<programlisting language="java">class MongoTemplateProducer { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Produces |
|
|
|
|
|
|
|
@ApplicationScoped |
|
|
|
|
|
|
|
public MongoOperations createMongoTemplate() throws UnknownHostException, MongoException { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MongoDbFactory factory = new SimpleMongoDbFactory(new Mongo(), "database"); |
|
|
|
|
|
|
|
return new MongoTemplate(factory); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}</programlisting> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<para>The Spring Data MongoDB CDI extension will pick up the |
|
|
|
|
|
|
|
<classname>MongoTemplate</classname> 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 <code>@Inject</code>-ed |
|
|
|
|
|
|
|
property:</para> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<programlisting language="java">class RepositoryClient { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Inject |
|
|
|
|
|
|
|
PersonRepository repository; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void businessMethod() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<Person> people = repository.findAll(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}</programlisting> |
|
|
|
|
|
|
|
</section> |
|
|
|
|
|
|
|
</section> |
|
|
|
</chapter> |
|
|
|
</chapter> |
|
|
|
|