Search results
Disabling Redis Auto-configuration in Spring Boot applications
1. OVERVIEW
I was recently working on a Spring Boot web application where it was decided to reduce external services dependencies to speedup development, caching using Redis in this specific case.
The interaction between the web application and a Redis server is done using Spring’s RedisTemplate, an implemention of the Template design pattern, used similarly to RestTemplate, JdbcTemplate, JmsTemplate, etc..
A RedisTemplate and related beans are auto-configured by Spring Boot when including spring-boot-starter-data-redis as a dependency for the application. This posts covers some tips to prevent such connection from being auto-configured.
2. EXCLUDING RedisAutoConfiguration
In general Spring Boot beans auto-configuration could be disabled or prevented when excluding them through properties or via annotation:
application.yml
Redis connection-related beans won’t get auto-configured when using the Spring profile no-redis
.
exclude
attribute of the @SpringBootApplication annotation, something like:
@SpringBootApplication( exclude = { RedisAutoConfiguration.class } )
But running the application using spring.profiles.active=no-redis
fails to initialize the Spring context due to this exception:
Even though it gets logged as a WARN
, the application doesn’t start.
3. SOLVING THE ISSUE
After some troubleshooting, it is not only required to exclude RedisAutoConfiguration but also RedisRepositoriesAutoConfiguration, which could be accomplished in two ways:
3.1. PREVENTING RedisRepositoriesAutoConfiguration AUTO-CONFIGURATION USING exclude
Via properties file:
or via @SpringBootApplication annotation:
3.2. PREVENTING RedisRepositoriesAutoConfiguration AUTO-CONFIGURATION USING REDIS-SPECIFIC PROPERTY
Thanks for reading and as always, feedback is very much appreciated. If you found this post helpful and would like to receive updates when content like this gets published, sign up to the newsletter.
4. SOURCE CODE
Accompanying source code for this blog post can be found at: