#54 - Added a project for MongoDB examples using Java 8 features.
This initial version of the example projects demonstrates the usage and mapping behavior of Java 8 Streams in repositories. The test cases oppose a plain List based query method with one that uses a Stream and shows how the former pulls all data into memory first and the iteration is done over the pre-populated list. The execution of the Stream based method in contrast shows that the individual elements are read and converted while iterating the stream.
This commit is contained in:
committed by
Oliver Gierke
parent
6597d7ca95
commit
73be2fcddb
21
mongodb/java8/README.md
Normal file
21
mongodb/java8/README.md
Normal file
@@ -0,0 +1,21 @@
|
||||
# Spring Data MongoDB - Java 8 examples
|
||||
|
||||
This project contains samples of Java 8 specific features of Spring Data (MongoDB).
|
||||
|
||||
## Support for JDK 8's `Stream` for repository methods
|
||||
|
||||
Repository methods can use a Java 8 `Stream` as a return type which will cause the reading of the results and the to-object-conversion of the `DBObject` to happen while iterating over the stream.
|
||||
|
||||
```java
|
||||
public interface PersonRepository extends CrudRepository<Person, String> {
|
||||
|
||||
@Override
|
||||
List<Person> findAll();
|
||||
|
||||
//Custom Query method returning a Java 8 Stream
|
||||
@Query("{}")
|
||||
Stream<Person> findAllByCustomQueryWithStream();
|
||||
}
|
||||
```
|
||||
|
||||
The test cases in `PersonRepositoryIntegrationTest` oppose a plain `List` based query method with one that uses a `Stream` and shows how the former pulls all data into memory first and the iteration is done over the pre-populated list. The execution of the `Stream`-based method in contrast shows that the individual elements are read and converted while iterating the stream.
|
||||
33
mongodb/java8/pom.xml
Normal file
33
mongodb/java8/pom.xml
Normal file
@@ -0,0 +1,33 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.data.examples</groupId>
|
||||
<artifactId>spring-data-mongodb-examples</artifactId>
|
||||
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>spring-data-mongodb-java8</artifactId>
|
||||
<name>Spring Data MongoDB - Java 8 specific features</name>
|
||||
|
||||
<properties>
|
||||
<spring-data-releasetrain.version>Fowler-BUILD-SNAPSHOT</spring-data-releasetrain.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>spring-data-mongodb-utils</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-mongodb</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.mongodb.people;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.mongodb.core.mapping.event.LoggingEventListener;
|
||||
|
||||
/**
|
||||
* Simple configuration that registers a {@link LoggingEventListener} to demonstrate mapping behaviour when Java 8
|
||||
* Streams are used.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@SpringBootApplication
|
||||
class ApplicationConfiguration {
|
||||
|
||||
public @Bean LoggingEventListener mongoEventListener() {
|
||||
return new LoggingEventListener();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.mongodb.people;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
/**
|
||||
* An entity to represent a Person.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@Data
|
||||
@RequiredArgsConstructor
|
||||
public class Person {
|
||||
|
||||
private @Id String id;
|
||||
private final String firstname;
|
||||
private final String lastname;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.mongodb.people;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.data.mongodb.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
/**
|
||||
* Repository interface to manage {@link Person} instances.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
public interface PersonRepository extends CrudRepository<Person, String> {
|
||||
|
||||
List<Person> findAll();
|
||||
|
||||
@Query("{}")
|
||||
Stream<Person> findAllByCustomQueryWithStream();
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.mongodb.people;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.mapping.event.LoggingEventListener;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import example.springdata.mongodb.util.RequiresMongoDB;
|
||||
|
||||
/**
|
||||
* Integration test for {@link PersonRepository}.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
|
||||
public class PersonRepositoryIntegrationTest {
|
||||
|
||||
@ClassRule public static RequiresMongoDB mongodbAvailable = RequiresMongoDB.anyVersion();
|
||||
|
||||
@Autowired PersonRepository repository;
|
||||
@Autowired MongoOperations operations;
|
||||
|
||||
Person dave, oliver, carter;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
repository.deleteAll();
|
||||
|
||||
dave = repository.save(new Person("Dave", "Matthews"));
|
||||
oliver = repository.save(new Person("Oliver August", "Matthews"));
|
||||
carter = repository.save(new Person("Carter", "Beauford"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Note that the all object conversions are preformed before the results are printed to the console.
|
||||
*/
|
||||
@Test
|
||||
public void shouldPerformConversionBeforeResultProcessing() {
|
||||
repository.findAll().forEach(System.out::println);
|
||||
}
|
||||
|
||||
/**
|
||||
* Note that the object conversions are preformed during stream processing as one can see from the
|
||||
* {@link LoggingEventListener} output that is printed to the console.
|
||||
*/
|
||||
@Test
|
||||
public void shouldPerformConversionDuringJava8StreamProcessing() {
|
||||
repository.findAllByCustomQueryWithStream().forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
/**
|
||||
* Package showing usage of Spring Data MongoDB Repositories with Java 8.
|
||||
*/
|
||||
package example.springdata.mongodb.people;
|
||||
|
||||
@@ -14,12 +14,13 @@
|
||||
<name>Spring Data MongoDB - Examples</name>
|
||||
<description>Sample projects for Spring Data MongoDB</description>
|
||||
<url>http://projects.spring.io/spring-data-mongodb</url>
|
||||
<inceptionYear>2011-2014</inceptionYear>
|
||||
<inceptionYear>2011-2015</inceptionYear>
|
||||
|
||||
<modules>
|
||||
<module>aggregation</module>
|
||||
<module>example</module>
|
||||
<module>text-search</module>
|
||||
<module>java8</module>
|
||||
<module>util</module>
|
||||
</modules>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user