diff --git a/spring-boot-samples/README.adoc b/spring-boot-samples/README.adoc index 69f5fc16a5..1186970424 100644 --- a/spring-boot-samples/README.adoc +++ b/spring-boot-samples/README.adoc @@ -16,6 +16,8 @@ -- A production features sample with no web application * link:spring-boot-sample-actuator-log4j[spring-boot-sample-actuator-log4j] -- A production features sample using log4j for logging (instead of logback) +* link:spring-boot-sample-cache-ehcache[spring-boot-sample-cache] + -- A web sample that uses Spring's cache abstraction * link:spring-boot-sample-web-ui[spring-boot-sample-web-ui] -- A thymeleaf web application * link:spring-boot-sample-web-static[spring-boot-sample-web-static] diff --git a/spring-boot-samples/pom.xml b/spring-boot-samples/pom.xml index 0b0a7cf3dc..474d828daa 100644 --- a/spring-boot-samples/pom.xml +++ b/spring-boot-samples/pom.xml @@ -30,6 +30,7 @@ spring-boot-sample-aop spring-boot-sample-atmosphere spring-boot-sample-batch + spring-boot-sample-cache spring-boot-sample-data-elasticsearch spring-boot-sample-data-gemfire spring-boot-sample-data-jpa diff --git a/spring-boot-samples/spring-boot-sample-cache/pom.xml b/spring-boot-samples/spring-boot-sample-cache/pom.xml new file mode 100644 index 0000000000..4d0f28cab5 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-cache/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-samples + 1.3.0.BUILD-SNAPSHOT + + spring-boot-sample-cache + Spring Boot Cache Sample + Spring Boot Cache Sample + http://projects.spring.io/spring-boot/ + + Pivotal Software, Inc. + http://www.spring.io + + + ${basedir}/../.. + + + + org.springframework.boot + spring-boot-starter + + + org.springframework + spring-context-support + + + net.sf.ehcache + ehcache + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/CountryService.java b/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/CountryService.java new file mode 100644 index 0000000000..8c17927f3e --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/CountryService.java @@ -0,0 +1,38 @@ +/* + * Copyright 2012-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 sample; + +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Component; + +import java.util.Arrays; +import java.util.List; + +/** + * @author Eddú Meléndez + * @since 1.3.0 + */ +@Component +public class CountryService { + + @Cacheable("countries") + public List countries() { + System.out.println("Loading countries"); + return Arrays.asList("Perú", "United States"); + } + +} diff --git a/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/SampleEhCacheCacheApplication.java b/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/SampleEhCacheCacheApplication.java new file mode 100644 index 0000000000..b033f53b0e --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/SampleEhCacheCacheApplication.java @@ -0,0 +1,35 @@ +/* + * Copyright 2012-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 sample; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; + +/** + * @author Eddú Meléndez + * @since 1.3.0 + */ +@EnableCaching +@SpringBootApplication +public class SampleEhCacheCacheApplication { + + public static void main(String[] args) { + SpringApplication.run(SampleEhCacheCacheApplication.class); + } + +} diff --git a/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/Startup.java b/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/Startup.java new file mode 100644 index 0000000000..7cdd3f9149 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-cache/src/main/java/sample/Startup.java @@ -0,0 +1,41 @@ +/* + * Copyright 2012-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 sample; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +/** + * @author Eddú Meléndez + * @since 1.3.0 + */ +@Component +public class Startup implements CommandLineRunner { + + @Autowired + private CountryService countryService; + + @Override + public void run(String... args) throws Exception { + System.out.println("---- calling country service"); + System.out.println(countryService.countries()); + System.out.println("---- calling country service again from cache."); + System.out.println(countryService.countries()); + } + +} diff --git a/spring-boot-samples/spring-boot-sample-cache/src/main/resources/application-override.properties b/spring-boot-samples/spring-boot-sample-cache/src/main/resources/application-override.properties new file mode 100644 index 0000000000..5255d6360f --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-cache/src/main/resources/application-override.properties @@ -0,0 +1 @@ +spring.cache.config=/cache/ehcache-override.xml diff --git a/spring-boot-samples/spring-boot-sample-cache/src/main/resources/cache/ehcache-override.xml b/spring-boot-samples/spring-boot-sample-cache/src/main/resources/cache/ehcache-override.xml new file mode 100644 index 0000000000..62948a72f2 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-cache/src/main/resources/cache/ehcache-override.xml @@ -0,0 +1,8 @@ + + + + diff --git a/spring-boot-samples/spring-boot-sample-cache/src/main/resources/ehcache.xml b/spring-boot-samples/spring-boot-sample-cache/src/main/resources/ehcache.xml new file mode 100644 index 0000000000..62948a72f2 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-cache/src/main/resources/ehcache.xml @@ -0,0 +1,8 @@ + + + + diff --git a/spring-boot-samples/spring-boot-sample-cache/src/test/java/sample/AbstractEhCacheCacheTests.java b/spring-boot-samples/spring-boot-sample-cache/src/test/java/sample/AbstractEhCacheCacheTests.java new file mode 100644 index 0000000000..5e9c672337 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-cache/src/test/java/sample/AbstractEhCacheCacheTests.java @@ -0,0 +1,63 @@ +/* + * Copyright 2012-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 sample; + +import org.junit.After; +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.cache.CacheManager; +import org.springframework.cache.ehcache.EhCacheCacheManager; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.core.IsInstanceOf.instanceOf; +import static org.junit.Assert.assertThat; + +/** + * @author Eddú Meléndez + * @since 1.3.0 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = {SampleEhCacheCacheApplication.class}) +public abstract class AbstractEhCacheCacheTests { + + @Autowired + private CacheManager cacheManager; + + @Autowired + private EhCacheCacheManager ehCacheManager; + + @After + public void tearDown() { + if (this.ehCacheManager != null) { + this.ehCacheManager.getCacheManager().shutdown(); + } + } + + @Test + public void testCacheManagerInstance() { + assertThat(this.cacheManager, instanceOf(EhCacheCacheManager.class)); + } + + @Test + public void testCacheManager() { + assertThat(this.cacheManager.getCacheNames(), contains("countries")); + } + +} diff --git a/spring-boot-samples/spring-boot-sample-cache/src/test/java/sample/SampleEhCacheCacheApplicationOverrideTests.java b/spring-boot-samples/spring-boot-sample-cache/src/test/java/sample/SampleEhCacheCacheApplicationOverrideTests.java new file mode 100644 index 0000000000..5b5e72d253 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-cache/src/test/java/sample/SampleEhCacheCacheApplicationOverrideTests.java @@ -0,0 +1,33 @@ +/* + * Copyright 2012-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 sample; + +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Eddú Meléndez + * @since 1.3.0 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = {SampleEhCacheCacheApplication.class}) +@ActiveProfiles("override") +public class SampleEhCacheCacheApplicationOverrideTests extends AbstractEhCacheCacheTests { + +} diff --git a/spring-boot-samples/spring-boot-sample-cache/src/test/java/sample/SampleEhCacheCacheApplicationTests.java b/spring-boot-samples/spring-boot-sample-cache/src/test/java/sample/SampleEhCacheCacheApplicationTests.java new file mode 100644 index 0000000000..5346848c36 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-cache/src/test/java/sample/SampleEhCacheCacheApplicationTests.java @@ -0,0 +1,31 @@ +/* + * Copyright 2012-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 sample; + +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Eddú Meléndez + * @since 1.3.0 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = {SampleEhCacheCacheApplication.class}) +public class SampleEhCacheCacheApplicationTests extends AbstractEhCacheCacheTests { + +}