#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:
Thomas Darimont
2015-03-02 18:06:31 +01:00
committed by Oliver Gierke
parent 6597d7ca95
commit 73be2fcddb
8 changed files with 237 additions and 1 deletions

View File

@@ -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();
}
}

View File

@@ -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;
}

View File

@@ -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();
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,5 @@
/**
* Package showing usage of Spring Data MongoDB Repositories with Java 8.
*/
package example.springdata.mongodb.people;