Skip to content

Commit

Permalink
Allow DevTools to find and configure Spring Session 1.1's RedisTemplate
Browse files Browse the repository at this point in the history
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 spring-projectsgh-4895
Closes spring-projectsgh-4896
  • Loading branch information
tsachev authored and wilkinsona committed Jan 18, 2016
1 parent da45041 commit 3e291ff
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand All @@ -42,7 +43,6 @@
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;

/**
Expand Down Expand Up @@ -169,7 +169,7 @@ static class RedisRestartConfiguration {

@Bean
public RestartCompatibleRedisSerializerConfigurer restartCompatibleRedisSerializerConfigurer(
RedisTemplate<String, ExpiringSession> sessionRedisTemplate) {
@Qualifier("sessionRedisTemplate") RedisTemplate<?, ?> sessionRedisTemplate) {
return new RestartCompatibleRedisSerializerConfigurer(
sessionRedisTemplate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
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;

/**
Expand All @@ -42,12 +41,11 @@
*/
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,27 @@ public void watchingAdditionalPaths() throws Exception {
}

@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(),
Expand Down Expand Up @@ -306,15 +320,24 @@ public static class WebResourcesConfig {
}

@Configuration
public static class SessionRedisTemplateConfig {
public static class SessionRedisTemplateConfig10 {

@Bean
public RedisTemplate<String, ExpiringSession> sessionRedisTemplate() {
RedisTemplate<String, ExpiringSession> redisTemplate = new RedisTemplate<String, ExpiringSession>();
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;
}
}
}

0 comments on commit 3e291ff

Please sign in to comment.