Reputation: 329
I am implementing Redis Jedis with spring boot application. I am using below dependency and configuration. while doing this i am getting error "Consider defining a bean of type 'redis.clients.jedis.JedisPool' in your configuration."
While reading the redis host, port and password from yml manually then it works fine. But since i am using spring-boot-starter-data-redis dependency so i do not want to read the redis host, port, password from yml file in fact it should work with @configuration and @bean automatically. In fact Redis Lettuce also woking with autoconfigyration fine but Redis Jedis is not. Please help.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
and below is my configuration file
@Configuration public class SchedulerConfiguration
{
@Bean public LockProvider lockProvider(JedisPool jedisPool)
{
return new JedisLockProvider(jedisPool);
}
}
Upvotes: 0
Views: 4427
Reputation: 1731
You have to configure Spring to use the Redis
and should use the RedisTemplate
when using spring-data-redis
. Using the template provides easy configurations and enables quick setup and use redis in the Spring applications.
Here's how the configuration should look like if you are using annotation based configurations
package test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@ComponentScan("test") // Component Scan Base Package
public class RedisConfiguration{
// Better if you can use a properties file and inject these values
private String redisHost = "localhost";
private int redisPort = 6379;
@Bean
JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setHostName(redisHost);
factory.setPort(redisPort);
factory.setUsePool(true);
return factory;
}
@Bean
RedisTemplate< String, Object > redisTemplate() {
final RedisTemplate< String, Object > template = new RedisTemplate< String, Object >();
template.setConnectionFactory( jedisConnectionFactory() );
template.setKeySerializer( new StringRedisSerializer() );
template.setHashValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );
template.setValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );
return template;
}
}
And then use within a service
@Service
public class RedisService {
@Autowired
private RedisTemplate< String, Object > template;
// Methods to add and get objects from redis goes here
}
And finally, the main class,
public class MainClassTest{
public static void main(String... args) throws InterruptedException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(RedisConfiguration.class);
RedisService redisService = context.getBean(RedisService.class);
// Use the service here
}
}
Upvotes: 2