I have below Cache Repo with other methods,
@Component
@CacheConfig(cacheNames = "enroll", cacheManager = "springtoolCM")
public class EnrollCasheRepository {
/** The string redis template. */
@Autowired
private StringRedisTemplate stringRedisTemplate;
}
I am using spring-boot-starter-redis in the pom.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
I am using EnrollCasheRepository
in my filter with @Autowired
.
Even I comment out redis properties in application.properties
and also my redis server is down, but I still get an EnrollCasheRepository
object. What would be a better way to check whether redis is install in my machine and proceed with EnrollCasheRepository
if so.
I am looking for a better way other than handle Exception thrown if redis not installed and proceed?
Spring-boot redis configuration is provided by RedisAutoConfiguration
This class creates the connection factory and initializes the StringRedisTemplate
bean.
The configuration is dependant on having Jedis available on the classpath.
It appears that there is no test to check if the connection details are valid or not.
If you want to have your EnrollCasheRepository
bean created dependant on jedis being configured, the simplest way to achieve it is probably going to be annotating it with @ConditionalOnProperty
and creating a feature flag config value.
@Component
@ConditionalOnProperty("redis.enabled")
@CacheConfig(cacheNames = "enroll", cacheManager = "springtoolCM")
public class EnrollCasheRepository {
Add the flag to your application.properties(or equivalent)
redis.enabled=true
If you want to be more intelligent about it, like detecting if the configured redis server is available before creating the bean, then that would be more complex.
You could look at using @Conditional
with your own implementation of Condition
, but that is probably more trouble than it is worth.
You are probably better off creating the bean, then testing if it works after the fact.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With