mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-15 01:07:30 +08:00
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<String, ExpiringSession> 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
This commit is contained in:
parent
da4504118e
commit
3e291ff04f
@ -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<String, ExpiringSession> sessionRedisTemplate) {
|
||||
@Qualifier("sessionRedisTemplate") RedisTemplate<?, ?> sessionRedisTemplate) {
|
||||
return new RestartCompatibleRedisSerializerConfigurer(
|
||||
sessionRedisTemplate);
|
||||
}
|
||||
|
@ -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<String, ExpiringSession> redisTemplate;
|
||||
private final RedisTemplate<?, ?> redisTemplate;
|
||||
|
||||
private volatile ClassLoader classLoader;
|
||||
|
||||
RestartCompatibleRedisSerializerConfigurer(
|
||||
RedisTemplate<String, ExpiringSession> redisTemplate) {
|
||||
RestartCompatibleRedisSerializerConfigurer(RedisTemplate<?, ?> redisTemplate) {
|
||||
this.redisTemplate = redisTemplate;
|
||||
}
|
||||
|
||||
|
@ -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<String, ExpiringSession> sessionRedisTemplate() {
|
||||
@ -314,7 +328,16 @@ public class LocalDevToolsAutoConfigurationTests {
|
||||
redisTemplate.setConnectionFactory(mock(RedisConnectionFactory.class));
|
||||
return redisTemplate;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class SessionRedisTemplateConfig11 {
|
||||
|
||||
@Bean
|
||||
public RedisTemplate<Object, Object> sessionRedisTemplate() {
|
||||
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<Object, Object>();
|
||||
redisTemplate.setConnectionFactory(mock(RedisConnectionFactory.class));
|
||||
return redisTemplate;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user