Spring Redis auto-configuration example
Spring Boot provides auto-configuration for Redis. You can make use of the other projects, but you must configure them yourself. Refer to the appropriate reference documentation at spring.io/projects/spring-data.
for the Lettuce and Jedis client libraries and the abstractions on top of them provided
by Spring Data Redis.
There is a spring-boot-starter-data-redis “Starter” for collecting the dependencies in a convenient
way. By default, it uses Lettuce. That starter handles both traditional and reactive applications.
RedisTemplate instance as you would any other Spring Bean. By default, the instance tries to connect
to a Redis server at localhost:6379. The following listing shows an example of such a bean:
@Component
public class MyBean {
private StringRedisTemplate template;
@Autowired
public MyBean(StringRedisTemplate template) {
this.template = template;
}
// ...
}
You can also register an arbitrary number of beans that implement
LettuceClientConfigurationBuilderCustomizer for more advanced customizations.
If you use Jedis, JedisClientConfigurationBuilderCustomizer is also available.
If you add your own @Bean of any of the auto-configured types, it replaces the default (except in the
case of RedisTemplate, when the exclusion is based on the bean name, redisTemplate, not its type). By default, if commons-pool2 is on the classpath, you get a pooled connection factory.
Redis
Redis is a cache, message broker, and richly-featured key-value store. Spring Boot offers basic autoconfigurationfor the Lettuce and Jedis client libraries and the abstractions on top of them provided
by Spring Data Redis.
There is a spring-boot-starter-data-redis “Starter” for collecting the dependencies in a convenient
way. By default, it uses Lettuce. That starter handles both traditional and reactive applications.
Connecting to Redis
You can inject an auto-configured RedisConnectionFactory, StringRedisTemplate, or vanillaRedisTemplate instance as you would any other Spring Bean. By default, the instance tries to connect
to a Redis server at localhost:6379. The following listing shows an example of such a bean:
@Component
public class MyBean {
private StringRedisTemplate template;
@Autowired
public MyBean(StringRedisTemplate template) {
this.template = template;
}
// ...
}
You can also register an arbitrary number of beans that implement
LettuceClientConfigurationBuilderCustomizer for more advanced customizations.
If you use Jedis, JedisClientConfigurationBuilderCustomizer is also available.
If you add your own @Bean of any of the auto-configured types, it replaces the default (except in the
case of RedisTemplate, when the exclusion is based on the bean name, redisTemplate, not its type). By default, if commons-pool2 is on the classpath, you get a pooled connection factory.
Comments
Post a Comment