Polish "Add support for selecting the Redis client to use"

See gh-22569
pull/22740/head
Stephane Nicoll 4 years ago
parent ac651442fa
commit 589669d0cc

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -43,7 +43,6 @@ import org.springframework.util.StringUtils;
*
* @author Mark Paluch
* @author Stephane Nicoll
* @author Chris Bono
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ GenericObjectPool.class, JedisConnection.class, Jedis.class })

@ -50,7 +50,6 @@ import org.springframework.util.StringUtils;
*
* @author Mark Paluch
* @author Andy Wilkinson
* @author Chris Bono
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(RedisClient.class)

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -21,6 +21,7 @@ import java.net.UnknownHostException;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -52,6 +53,7 @@ public class RedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
@ -61,6 +63,7 @@ public class RedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();

@ -77,9 +77,9 @@ public class RedisProperties {
private String clientName;
/**
* Type of client to use.
* Type of client to use. By default, auto-detected according to the classpath.
*/
private ClientType clientType = ClientType.Lettuce;
private ClientType clientType;
private Sentinel sentinel;
@ -191,14 +191,15 @@ public class RedisProperties {
public enum ClientType {
/**
* Use the Lettuce client
* Use the Lettuce redis client.
*/
Lettuce,
LETTUCE,
/**
* Use the Jedis client
* Use the Jedis redis client.
*/
Jedis
JEDIS
}
/**

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -25,6 +25,7 @@ import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration.JedisClientConfigurationBuilder;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
@ -43,11 +44,17 @@ class RedisAutoConfigurationJedisTests {
.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class));
@Test
void testDefaultRedisConfiguration() {
void connectionFactoryDefaultsToJedis() {
this.contextRunner.run((context) -> assertThat(context.getBean("redisConnectionFactory"))
.isInstanceOf(JedisConnectionFactory.class));
}
@Test
void connectionFactoryIsNotCreatedWhenLettuceIsSelected() {
this.contextRunner.withPropertyValues("spring.redis.client-type=lettuce")
.run((context) -> assertThat(context).doesNotHaveBean(RedisConnectionFactory.class));
}
@Test
void testOverrideRedisConfiguration() {
this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.database:1").run((context) -> {

@ -37,6 +37,7 @@ import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
@ -53,8 +54,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link RedisAutoConfiguration} when both Jedis and Lettuce are on the
* classpath.
* Tests for {@link RedisAutoConfiguration}.
*
* @author Dave Syer
* @author Christian Dupuis
@ -74,10 +74,10 @@ class RedisAutoConfigurationTests {
@Test
void testDefaultRedisConfiguration() {
this.contextRunner.run((context) -> {
assertThat(context.getBean("redisTemplate", RedisOperations.class)).isNotNull();
assertThat(context.getBean(StringRedisTemplate.class)).isNotNull();
assertThat(context.getBean("redisConnectionFactory")).isInstanceOf(LettuceConnectionFactory.class);
assertThat(context.getBeanProvider(JedisConnectionConfiguration.class).getIfAvailable()).isNull();
assertThat(context.getBean("redisTemplate")).isInstanceOf(RedisOperations.class);
assertThat(context).hasSingleBean(StringRedisTemplate.class);
assertThat(context).hasSingleBean(RedisConnectionFactory.class);
assertThat(context.getBean(RedisConnectionFactory.class)).isInstanceOf(LettuceConnectionFactory.class);
});
}
@ -187,17 +187,18 @@ class RedisAutoConfigurationTests {
}
@Test
void testRedisConfigurationWithClientNameJedis() {
this.contextRunner.withPropertyValues("spring.redis.client-type:jedis")
.run((context) -> assertThat(context.getBean("redisConnectionFactory"))
.isInstanceOf(JedisConnectionFactory.class));
void connectionFactoryWithJedisClientType() {
this.contextRunner.withPropertyValues("spring.redis.client-type:jedis").run((context) -> {
assertThat(context).hasSingleBean(RedisConnectionFactory.class);
assertThat(context.getBean(RedisConnectionFactory.class)).isInstanceOf(JedisConnectionFactory.class);
});
}
@Test
void testRedisConfigurationWithClientNameLettuce() {
void connectionFactoryWithLettuceClientType() {
this.contextRunner.withPropertyValues("spring.redis.client-type:lettuce").run((context) -> {
assertThat(context.getBean("redisConnectionFactory")).isInstanceOf(LettuceConnectionFactory.class);
assertThat(context.getBeanProvider(JedisConnectionConfiguration.class).getIfAvailable()).isNull();
assertThat(context).hasSingleBean(RedisConnectionFactory.class);
assertThat(context.getBean(RedisConnectionFactory.class)).isInstanceOf(LettuceConnectionFactory.class);
});
}

Loading…
Cancel
Save