From 3e291ff04f571fea44f37e3faa6e11d2f7654a3f Mon Sep 17 00:00:00 2001 From: Vladimir Tsanev Date: Thu, 7 Jan 2016 13:58:39 +0200 Subject: [PATCH 1/2] Allow DevTools to find and configure Spring Session 1.1's RedisTemplate Previously, when Spring Session 1.1.0.M1 and DevTools were declared as dependencies the application failed to start, because sessionRedisTemplate could not be resolved. This commit relaxes the dependency for sessionRedisTemplate in restartCompatibleRedisSerializerConfigurer from RedisTemplate to RedisTemplate and uses the bean name in a @Qualifer to ensure that the right RedisTemplate bean gets injected. Fixes gh-4895 Closes gh-4896 --- .../LocalDevToolsAutoConfiguration.java | 4 +-- ...rtCompatibleRedisSerializerConfigurer.java | 6 ++-- .../LocalDevToolsAutoConfigurationTests.java | 35 +++++++++++++++---- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfiguration.java b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfiguration.java index e4e156742f..389f07abdc 100644 --- a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfiguration.java +++ b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfiguration.java @@ -21,6 +21,7 @@ import java.net.URL; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -42,7 +43,6 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.EventListener; import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.session.ExpiringSession; import org.springframework.util.StringUtils; /** @@ -169,7 +169,7 @@ public class LocalDevToolsAutoConfiguration { @Bean public RestartCompatibleRedisSerializerConfigurer restartCompatibleRedisSerializerConfigurer( - RedisTemplate sessionRedisTemplate) { + @Qualifier("sessionRedisTemplate") RedisTemplate sessionRedisTemplate) { return new RestartCompatibleRedisSerializerConfigurer( sessionRedisTemplate); } diff --git a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/RestartCompatibleRedisSerializerConfigurer.java b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/RestartCompatibleRedisSerializerConfigurer.java index c45172f7b1..a93e5e795d 100644 --- a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/RestartCompatibleRedisSerializerConfigurer.java +++ b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/RestartCompatibleRedisSerializerConfigurer.java @@ -26,7 +26,6 @@ import org.springframework.core.serializer.support.SerializingConverter; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.SerializationException; -import org.springframework.session.ExpiringSession; import org.springframework.util.ObjectUtils; /** @@ -42,12 +41,11 @@ import org.springframework.util.ObjectUtils; */ class RestartCompatibleRedisSerializerConfigurer implements BeanClassLoaderAware { - private final RedisTemplate redisTemplate; + private final RedisTemplate redisTemplate; private volatile ClassLoader classLoader; - RestartCompatibleRedisSerializerConfigurer( - RedisTemplate redisTemplate) { + RestartCompatibleRedisSerializerConfigurer(RedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; } diff --git a/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfigurationTests.java b/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfigurationTests.java index f3b5a1ee8b..c9472c6ec7 100755 --- a/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfigurationTests.java +++ b/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfigurationTests.java @@ -241,13 +241,27 @@ public class LocalDevToolsAutoConfigurationTests { } @Test - public void sessionRedisTemplateIsConfiguredWithCustomDeserializers() + public void sessionRedisTemplateIsConfiguredWithCustomDeserializers10() throws Exception { - SpringApplication application = new SpringApplication( - SessionRedisTemplateConfig.class, LocalDevToolsAutoConfiguration.class); + sessionRedisTemplateIsConfiguredWithCustomDeserializers( + SessionRedisTemplateConfig10.class); + } + + @Test + public void sessionRedisTemplateIsConfiguredWithCustomDeserializers11() + throws Exception { + sessionRedisTemplateIsConfiguredWithCustomDeserializers( + SessionRedisTemplateConfig11.class); + } + + private void sessionRedisTemplateIsConfiguredWithCustomDeserializers( + Object sessionConfig) throws Exception { + SpringApplication application = new SpringApplication(sessionConfig, + LocalDevToolsAutoConfiguration.class); application.setWebEnvironment(false); this.context = application.run(); - RedisTemplate redisTemplate = this.context.getBean(RedisTemplate.class); + RedisTemplate redisTemplate = this.context.getBean("sessionRedisTemplate", + RedisTemplate.class); assertThat(redisTemplate.getHashKeySerializer(), is(instanceOf(RestartCompatibleRedisSerializer.class))); assertThat(redisTemplate.getHashValueSerializer(), @@ -306,7 +320,7 @@ public class LocalDevToolsAutoConfigurationTests { } @Configuration - public static class SessionRedisTemplateConfig { + public static class SessionRedisTemplateConfig10 { @Bean public RedisTemplate sessionRedisTemplate() { @@ -314,7 +328,16 @@ public class LocalDevToolsAutoConfigurationTests { redisTemplate.setConnectionFactory(mock(RedisConnectionFactory.class)); return redisTemplate; } - } + @Configuration + public static class SessionRedisTemplateConfig11 { + + @Bean + public RedisTemplate sessionRedisTemplate() { + RedisTemplate redisTemplate = new RedisTemplate(); + redisTemplate.setConnectionFactory(mock(RedisConnectionFactory.class)); + return redisTemplate; + } + } } From 77e350acc05bb3e4f127c513232eac3f55d9300c Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 18 Jan 2016 15:35:38 +0000 Subject: [PATCH 2/2] Polish contribution --- .../LocalDevToolsAutoConfiguration.java | 9 ++++++--- ...RestartCompatibleRedisSerializerConfigurer.java | 2 +- .../LocalDevToolsAutoConfigurationTests.java | 14 +++++++++----- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfiguration.java b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfiguration.java index 389f07abdc..ff5c8ac008 100644 --- a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfiguration.java +++ b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2016 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. @@ -50,6 +50,7 @@ import org.springframework.util.StringUtils; * * @author Phillip Webb * @author Andy Wilkinson + * @author Vladimir Tsanev * @since 1.3.0 */ @Configuration @@ -164,12 +165,14 @@ public class LocalDevToolsAutoConfiguration { } @Configuration - @ConditionalOnBean(name = "sessionRedisTemplate") + @ConditionalOnBean(name = RedisRestartConfiguration.SESSION_REDIS_TEMPLATE_BEAN_NAME) static class RedisRestartConfiguration { + static final String SESSION_REDIS_TEMPLATE_BEAN_NAME = "sessionRedisTemplate"; + @Bean public RestartCompatibleRedisSerializerConfigurer restartCompatibleRedisSerializerConfigurer( - @Qualifier("sessionRedisTemplate") RedisTemplate sessionRedisTemplate) { + @Qualifier(SESSION_REDIS_TEMPLATE_BEAN_NAME) RedisTemplate sessionRedisTemplate) { return new RestartCompatibleRedisSerializerConfigurer( sessionRedisTemplate); } diff --git a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/RestartCompatibleRedisSerializerConfigurer.java b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/RestartCompatibleRedisSerializerConfigurer.java index a93e5e795d..6b4b0914fa 100644 --- a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/RestartCompatibleRedisSerializerConfigurer.java +++ b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/RestartCompatibleRedisSerializerConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2016 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. diff --git a/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfigurationTests.java b/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfigurationTests.java index c9472c6ec7..7f2c0ed9d5 100755 --- a/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfigurationTests.java +++ b/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/LocalDevToolsAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2016 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. @@ -68,6 +68,7 @@ import static org.mockito.Mockito.verify; * * @author Phillip Webb * @author Andy Wilkinson + * @author Vladimir Tsanev */ public class LocalDevToolsAutoConfigurationTests { @@ -244,14 +245,14 @@ public class LocalDevToolsAutoConfigurationTests { public void sessionRedisTemplateIsConfiguredWithCustomDeserializers10() throws Exception { sessionRedisTemplateIsConfiguredWithCustomDeserializers( - SessionRedisTemplateConfig10.class); + Session10RedisTemplateConfig.class); } @Test public void sessionRedisTemplateIsConfiguredWithCustomDeserializers11() throws Exception { sessionRedisTemplateIsConfiguredWithCustomDeserializers( - SessionRedisTemplateConfig11.class); + Session11RedisTemplateConfig.class); } private void sessionRedisTemplateIsConfiguredWithCustomDeserializers( @@ -320,7 +321,7 @@ public class LocalDevToolsAutoConfigurationTests { } @Configuration - public static class SessionRedisTemplateConfig10 { + public static class Session10RedisTemplateConfig { @Bean public RedisTemplate sessionRedisTemplate() { @@ -328,10 +329,11 @@ public class LocalDevToolsAutoConfigurationTests { redisTemplate.setConnectionFactory(mock(RedisConnectionFactory.class)); return redisTemplate; } + } @Configuration - public static class SessionRedisTemplateConfig11 { + public static class Session11RedisTemplateConfig { @Bean public RedisTemplate sessionRedisTemplate() { @@ -339,5 +341,7 @@ public class LocalDevToolsAutoConfigurationTests { redisTemplate.setConnectionFactory(mock(RedisConnectionFactory.class)); return redisTemplate; } + } + }