diff --git a/spring-boot-samples/README.adoc b/spring-boot-samples/README.adoc index 7273c688af..92c5c4d3f3 100644 --- a/spring-boot-samples/README.adoc +++ b/spring-boot-samples/README.adoc @@ -158,8 +158,8 @@ The following sample applications are provided: | link:spring-boot-sample-servlet[spring-boot-sample-servlet] | Web application with a "raw" `Servlet` returning plain text content -| link:spring-boot-sample-session-redis[spring-boot-sample-session-redis] -| Web Application that uses Spring Session to store session data in Redis +| link:spring-boot-sample-session[spring-boot-sample-session] +| Web Application that uses Spring Session to manage session data | link:spring-boot-sample-simple[spring-boot-sample-simple] | Simple command line application diff --git a/spring-boot-samples/pom.xml b/spring-boot-samples/pom.xml index 78dfbb838e..366ad6e29e 100644 --- a/spring-boot-samples/pom.xml +++ b/spring-boot-samples/pom.xml @@ -71,7 +71,7 @@ spring-boot-sample-secure-oauth2-actuator spring-boot-sample-secure-oauth2-resource spring-boot-sample-servlet - spring-boot-sample-session-redis + spring-boot-sample-session spring-boot-sample-simple spring-boot-sample-test spring-boot-sample-test-nomockito diff --git a/spring-boot-samples/spring-boot-sample-session-redis/pom.xml b/spring-boot-samples/spring-boot-sample-session-redis/pom.xml deleted file mode 100644 index 2eeca2f5ec..0000000000 --- a/spring-boot-samples/spring-boot-sample-session-redis/pom.xml +++ /dev/null @@ -1,50 +0,0 @@ - - - 4.0.0 - - - org.springframework.boot - spring-boot-samples - 2.0.0.BUILD-SNAPSHOT - - spring-boot-sample-session-redis - Spring Boot Session Redis Sample - Spring Boot Session Redis Sample - http://projects.spring.io/spring-boot/ - - Pivotal Software, Inc. - http://www.spring.io - - - ${basedir}/../.. - - - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-data-redis - - - org.springframework.session - spring-session-data-redis - - - - org.springframework.boot - spring-boot-starter-test - test - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - diff --git a/spring-boot-samples/spring-boot-sample-session/README.adoc b/spring-boot-samples/spring-boot-sample-session/README.adoc new file mode 100644 index 0000000000..80881ca97d --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-session/README.adoc @@ -0,0 +1,48 @@ += Spring Boot Spring Session Sample + +This sample demonstrates the Spring Session auto-configuration support. Spring Session +supports multiple session store types, including: + +* `Redis` +* `JDBC` +* `Hazelcast` + + + +== Using a different session store +Initially, the project uses JDBC session store backed by an in-memory embedded H2 +database. You can try out your favorite session store as explained below. + + + +=== Redis +Add `org.springframework.session:spring-session-data-redis` and +`spring-boot-starter-data-redis` dependencies to the project and make sure it is +configured properly (by default, a Redis instance with the default settings is expected +on your local box). + +TIP: Run sample application using Redis session store using +`$mvn spring-boot:run -Predis`. + + + +=== JDBC +Add `org.springframework.session:spring-session-jdbc`, +`org.springframework.boot:spring-boot-starter-jdbc` and `com.h2database:h2` dependencies +to the project. An in-memory embedded H2 database is automatically configured. + +TIP: Run sample application using JDBC session store with +`$mvn spring-boot:run -Pjdbc`. + +Note that this profile is active by default. + + + +=== Hazelcast +Add `org.springframework.session:spring-session-hazelcast` and `com.hazelcast:hazelcast` +dependencies to the project to enable support for Hazelcast. Since there is a default +`hazelcast.xml` configuration file at the root of the classpath, it is used to +automatically configure the underlying `HazelcastInstance`. + +TIP: Run sample application using Hazelcast session store with +`$mvn spring-boot:run -Phazelcast`. diff --git a/spring-boot-samples/spring-boot-sample-session/pom.xml b/spring-boot-samples/spring-boot-sample-session/pom.xml new file mode 100644 index 0000000000..70b62242f8 --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-session/pom.xml @@ -0,0 +1,90 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-samples + 2.0.0.BUILD-SNAPSHOT + + spring-boot-sample-session + Spring Boot Session Sample + Spring Boot Session Sample + http://projects.spring.io/spring-boot/ + + Pivotal Software, Inc. + http://www.spring.io + + + ${basedir}/../.. + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + redis + + + org.springframework.session + spring-session-data-redis + + + org.springframework.boot + spring-boot-starter-data-redis + + + + + jdbc + + true + + + + org.springframework.session + spring-session-jdbc + + + org.springframework.boot + spring-boot-starter-jdbc + + + com.h2database + h2 + + + + + hazelcast + + + org.springframework.session + spring-session-hazelcast + + + com.hazelcast + hazelcast + + + + + diff --git a/spring-boot-samples/spring-boot-sample-session-redis/src/main/java/sample/session/redis/HelloRestController.java b/spring-boot-samples/spring-boot-sample-session/src/main/java/sample/session/HelloRestController.java similarity index 92% rename from spring-boot-samples/spring-boot-sample-session-redis/src/main/java/sample/session/redis/HelloRestController.java rename to spring-boot-samples/spring-boot-sample-session/src/main/java/sample/session/HelloRestController.java index 437ce436ac..81eab2ff43 100644 --- a/spring-boot-samples/spring-boot-sample-session-redis/src/main/java/sample/session/redis/HelloRestController.java +++ b/spring-boot-samples/spring-boot-sample-session/src/main/java/sample/session/HelloRestController.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 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. @@ -14,7 +14,7 @@ * limitations under the License. */ -package sample.session.redis; +package sample.session; import java.util.UUID; diff --git a/spring-boot-samples/spring-boot-sample-session-redis/src/main/java/sample/session/redis/SampleSessionRedisApplication.java b/spring-boot-samples/spring-boot-sample-session/src/main/java/sample/session/SampleSessionApplication.java similarity index 80% rename from spring-boot-samples/spring-boot-sample-session-redis/src/main/java/sample/session/redis/SampleSessionRedisApplication.java rename to spring-boot-samples/spring-boot-sample-session/src/main/java/sample/session/SampleSessionApplication.java index a0691f7b6f..a79c96ea6b 100644 --- a/spring-boot-samples/spring-boot-sample-session-redis/src/main/java/sample/session/redis/SampleSessionRedisApplication.java +++ b/spring-boot-samples/spring-boot-sample-session/src/main/java/sample/session/SampleSessionApplication.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2017 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. @@ -14,16 +14,16 @@ * limitations under the License. */ -package sample.session.redis; +package sample.session; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication -public class SampleSessionRedisApplication { +public class SampleSessionApplication { public static void main(String[] args) throws Exception { - SpringApplication.run(SampleSessionRedisApplication.class); + SpringApplication.run(SampleSessionApplication.class); } } diff --git a/spring-boot-samples/spring-boot-sample-session-redis/src/main/resources/application.properties b/spring-boot-samples/spring-boot-sample-session/src/main/resources/application.properties similarity index 100% rename from spring-boot-samples/spring-boot-sample-session-redis/src/main/resources/application.properties rename to spring-boot-samples/spring-boot-sample-session/src/main/resources/application.properties diff --git a/spring-boot-samples/spring-boot-sample-session/src/main/resources/hazelcast.xml b/spring-boot-samples/spring-boot-sample-session/src/main/resources/hazelcast.xml new file mode 100644 index 0000000000..0f48adda0d --- /dev/null +++ b/spring-boot-samples/spring-boot-sample-session/src/main/resources/hazelcast.xml @@ -0,0 +1,20 @@ + + + + + principalName + + + principalName + + + + + + + + + + diff --git a/spring-boot-samples/spring-boot-sample-session-redis/src/test/java/sample/session/redis/SampleSessionRedisApplicationTests.java b/spring-boot-samples/spring-boot-sample-session/src/test/java/sample/session/SampleSessionApplicationTests.java similarity index 69% rename from spring-boot-samples/spring-boot-sample-session-redis/src/test/java/sample/session/redis/SampleSessionRedisApplicationTests.java rename to spring-boot-samples/spring-boot-sample-session/src/test/java/sample/session/SampleSessionApplicationTests.java index 560e5d8590..c842714c12 100644 --- a/spring-boot-samples/spring-boot-sample-session-redis/src/test/java/sample/session/redis/SampleSessionRedisApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-session/src/test/java/sample/session/SampleSessionApplicationTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package sample.session.redis; +package sample.session; import java.net.URI; @@ -23,7 +23,6 @@ import org.junit.Test; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.data.redis.RedisConnectionFailureException; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.RequestEntity; @@ -33,31 +32,20 @@ import org.springframework.web.client.RestTemplate; import static org.assertj.core.api.Assertions.assertThat; /** - * Tests for {@link SampleSessionRedisApplication}. + * Tests for {@link SampleSessionApplication}. * * @author Andy Wilkinson */ -public class SampleSessionRedisApplicationTests { +public class SampleSessionApplicationTests { @Test public void sessionExpiry() throws Exception { - - String port = null; - - try { - ConfigurableApplicationContext context = new SpringApplicationBuilder() - .sources(SampleSessionRedisApplication.class) - .properties("server.port:0") - .initializers(new ServerPortInfoApplicationContextInitializer()) - .run(); - port = context.getEnvironment().getProperty("local.server.port"); - } - catch (RuntimeException ex) { - if (!redisServerRunning(ex)) { - return; - } - throw ex; - } + ConfigurableApplicationContext context = new SpringApplicationBuilder() + .sources(SampleSessionApplication.class) + .properties("server.port:0") + .initializers(new ServerPortInfoApplicationContextInitializer()) + .run(); + String port = context.getEnvironment().getProperty("local.server.port"); URI uri = URI.create("http://localhost:" + port + "/"); RestTemplate restTemplate = new RestTemplate(); @@ -79,11 +67,4 @@ public class SampleSessionRedisApplicationTests { assertThat(uuid2).isNotEqualTo(uuid3); } - private boolean redisServerRunning(Throwable ex) { - if (ex instanceof RedisConnectionFailureException) { - return false; - } - return (ex.getCause() == null || redisServerRunning(ex.getCause())); - } - }