Skip to content

Commit 73be2fc

Browse files
Thomas Darimontodrotbohm
authored andcommitted
spring-projects#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.
1 parent 6597d7c commit 73be2fc

8 files changed

Lines changed: 237 additions & 1 deletion

File tree

mongodb/java8/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Spring Data MongoDB - Java 8 examples
2+
3+
This project contains samples of Java 8 specific features of Spring Data (MongoDB).
4+
5+
## Support for JDK 8's `Stream` for repository methods
6+
7+
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.
8+
9+
```java
10+
public interface PersonRepository extends CrudRepository<Person, String> {
11+
12+
@Override
13+
List<Person> findAll();
14+
15+
//Custom Query method returning a Java 8 Stream
16+
@Query("{}")
17+
Stream<Person> findAllByCustomQueryWithStream();
18+
}
19+
```
20+
21+
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.

mongodb/java8/pom.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>org.springframework.data.examples</groupId>
7+
<artifactId>spring-data-mongodb-examples</artifactId>
8+
<version>1.0.0.BUILD-SNAPSHOT</version>
9+
</parent>
10+
11+
<artifactId>spring-data-mongodb-java8</artifactId>
12+
<name>Spring Data MongoDB - Java 8 specific features</name>
13+
14+
<properties>
15+
<spring-data-releasetrain.version>Fowler-BUILD-SNAPSHOT</spring-data-releasetrain.version>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>${project.groupId}</groupId>
21+
<artifactId>spring-data-mongodb-utils</artifactId>
22+
<version>${project.version}</version>
23+
<scope>test</scope>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>org.springframework.data</groupId>
28+
<artifactId>spring-data-mongodb</artifactId>
29+
</dependency>
30+
31+
</dependencies>
32+
33+
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.mongodb.people;
17+
18+
import org.springframework.boot.autoconfigure.SpringBootApplication;
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.data.mongodb.core.mapping.event.LoggingEventListener;
21+
22+
/**
23+
* Simple configuration that registers a {@link LoggingEventListener} to demonstrate mapping behaviour when Java 8
24+
* Streams are used.
25+
*
26+
* @author Thomas Darimont
27+
*/
28+
@SpringBootApplication
29+
class ApplicationConfiguration {
30+
31+
public @Bean LoggingEventListener mongoEventListener() {
32+
return new LoggingEventListener();
33+
}
34+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.mongodb.people;
17+
18+
import lombok.Data;
19+
import lombok.RequiredArgsConstructor;
20+
21+
import org.springframework.data.annotation.Id;
22+
23+
/**
24+
* An entity to represent a Person.
25+
*
26+
* @author Thomas Darimont
27+
*/
28+
@Data
29+
@RequiredArgsConstructor
30+
public class Person {
31+
32+
private @Id String id;
33+
private final String firstname;
34+
private final String lastname;
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.mongodb.people;
17+
18+
import java.util.List;
19+
import java.util.stream.Stream;
20+
21+
import org.springframework.data.mongodb.repository.Query;
22+
import org.springframework.data.repository.CrudRepository;
23+
24+
/**
25+
* Repository interface to manage {@link Person} instances.
26+
*
27+
* @author Thomas Darimont
28+
*/
29+
public interface PersonRepository extends CrudRepository<Person, String> {
30+
31+
List<Person> findAll();
32+
33+
@Query("{}")
34+
Stream<Person> findAllByCustomQueryWithStream();
35+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.mongodb.people;
17+
18+
import org.junit.Before;
19+
import org.junit.ClassRule;
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.boot.test.SpringApplicationConfiguration;
24+
import org.springframework.data.mongodb.core.MongoOperations;
25+
import org.springframework.data.mongodb.core.mapping.event.LoggingEventListener;
26+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
27+
28+
import example.springdata.mongodb.util.RequiresMongoDB;
29+
30+
/**
31+
* Integration test for {@link PersonRepository}.
32+
*
33+
* @author Thomas Darimont
34+
*/
35+
@RunWith(SpringJUnit4ClassRunner.class)
36+
@SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
37+
public class PersonRepositoryIntegrationTest {
38+
39+
@ClassRule public static RequiresMongoDB mongodbAvailable = RequiresMongoDB.anyVersion();
40+
41+
@Autowired PersonRepository repository;
42+
@Autowired MongoOperations operations;
43+
44+
Person dave, oliver, carter;
45+
46+
@Before
47+
public void setUp() {
48+
49+
repository.deleteAll();
50+
51+
dave = repository.save(new Person("Dave", "Matthews"));
52+
oliver = repository.save(new Person("Oliver August", "Matthews"));
53+
carter = repository.save(new Person("Carter", "Beauford"));
54+
}
55+
56+
/**
57+
* Note that the all object conversions are preformed before the results are printed to the console.
58+
*/
59+
@Test
60+
public void shouldPerformConversionBeforeResultProcessing() {
61+
repository.findAll().forEach(System.out::println);
62+
}
63+
64+
/**
65+
* Note that the object conversions are preformed during stream processing as one can see from the
66+
* {@link LoggingEventListener} output that is printed to the console.
67+
*/
68+
@Test
69+
public void shouldPerformConversionDuringJava8StreamProcessing() {
70+
repository.findAllByCustomQueryWithStream().forEach(System.out::println);
71+
}
72+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* Package showing usage of Spring Data MongoDB Repositories with Java 8.
3+
*/
4+
package example.springdata.mongodb.people;
5+

mongodb/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
<name>Spring Data MongoDB - Examples</name>
1515
<description>Sample projects for Spring Data MongoDB</description>
1616
<url>http://projects.spring.io/spring-data-mongodb</url>
17-
<inceptionYear>2011-2014</inceptionYear>
17+
<inceptionYear>2011-2015</inceptionYear>
1818

1919
<modules>
2020
<module>aggregation</module>
2121
<module>example</module>
2222
<module>text-search</module>
23+
<module>java8</module>
2324
<module>util</module>
2425
</modules>
2526

0 commit comments

Comments
 (0)