Don't apply multiple session configurations

Add additional @ConditionalOnMissingBean guards to session
configurations to ensure that the first wins. Also reorder imports
to prefer Redis over JDBC.

See gh-5158
pull/5836/merge
Phillip Webb 9 years ago
parent 6466c0afa5
commit 2d243f0668

@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.session;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
@ -33,6 +34,7 @@ import org.springframework.session.config.annotation.web.http.EnableSpringHttpSe
@Configuration
@EnableSpringHttpSession
@Conditional(SessionCondition.class)
@ConditionalOnMissingBean(SessionRepository.class)
class HashMapSessionConfiguration {
@Bean

@ -20,8 +20,10 @@ import com.hazelcast.core.HazelcastInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.SessionRepository;
import org.springframework.session.hazelcast.config.annotation.web.http.HazelcastHttpSessionConfiguration;
/**
@ -32,6 +34,7 @@ import org.springframework.session.hazelcast.config.annotation.web.http.Hazelcas
* @author Stephane Nicoll
*/
@Configuration
@ConditionalOnMissingBean(SessionRepository.class)
@ConditionalOnBean(HazelcastInstance.class)
@Conditional(SessionCondition.class)
class HazelcastSessionConfiguration {

@ -20,8 +20,10 @@ import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.SessionRepository;
import org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration;
/**
@ -31,6 +33,7 @@ import org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessi
* @author Stephane Nicoll
*/
@Configuration
@ConditionalOnMissingBean(SessionRepository.class)
@ConditionalOnBean(DataSource.class)
@Conditional(SessionCondition.class)
class JdbcSessionConfiguration {

@ -18,9 +18,11 @@ package org.springframework.boot.autoconfigure.session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.session.SessionRepository;
import org.springframework.session.data.mongo.config.annotation.web.http.MongoHttpSessionConfiguration;
/**
@ -30,6 +32,7 @@ import org.springframework.session.data.mongo.config.annotation.web.http.MongoHt
* @author Stephane Nicoll
*/
@Configuration
@ConditionalOnMissingBean(SessionRepository.class)
@ConditionalOnBean(MongoOperations.class)
@Conditional(SessionCondition.class)
class MongoSessionConfiguration {

@ -18,10 +18,12 @@ package org.springframework.boot.autoconfigure.session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.session.SessionRepository;
import org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration;
/**
@ -33,6 +35,7 @@ import org.springframework.session.data.redis.config.annotation.web.http.RedisHt
* @author Stephane Nicoll
*/
@Configuration
@ConditionalOnMissingBean(SessionRepository.class)
@ConditionalOnBean({ RedisTemplate.class, RedisConnectionFactory.class })
@Conditional(SessionCondition.class)
class RedisSessionConfiguration {

@ -44,9 +44,9 @@ import org.springframework.session.SessionRepository;
* @since 1.4.0
*/
@Configuration
@ConditionalOnMissingBean(SessionRepository.class)
@ConditionalOnClass(Session.class)
@ConditionalOnWebApplication
@ConditionalOnMissingBean(SessionRepository.class)
@EnableConfigurationProperties(SessionProperties.class)
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, HazelcastAutoConfiguration.class,
MongoAutoConfiguration.class, RedisAutoConfiguration.class })

@ -26,24 +26,24 @@ package org.springframework.boot.autoconfigure.session;
public enum StoreType {
/**
* JDBC backed sessions.
* Redis backed sessions.
*/
JDBC,
REDIS,
/**
* Mongo backed sessions.
* Hazelcast backed sessions.
*/
MONGO,
HAZELCAST,
/**
* Redis backed sessions.
* Mongo backed sessions.
*/
REDIS,
MONGO,
/**
* Hazelcast backed sessions.
* JDBC backed sessions.
*/
HAZELCAST,
JDBC,
/**
* Simple in-memory map of sessions.

@ -56,6 +56,7 @@ public class SampleSessionRedisApplicationTests {
if (!redisServerRunning(ex)) {
return;
}
throw ex;
}
URI uri = URI.create("http://localhost:" + port + "/");

Loading…
Cancel
Save