java - 考虑在你的配置中定义一个“redis.clients.jedis.JedisPool”类型的bean。集成 Redis Jedis 时出错
问题描述
我正在使用 Spring Boot 应用程序实现 Redis Jedis。我正在使用以下依赖项和配置。执行此操作时出现错误“考虑在您的配置中定义一个类型为 'redis.clients.jedis.JedisPool' 的 bean。 ”
从 yml 手动读取 redis 主机、端口和密码时,它可以正常工作。但由于我使用的是spring-boot-starter-data-redis依赖项,所以我不想从 yml 文件中读取 redis 主机、端口、密码,实际上它应该自动与 @configuration 和 @bean 一起使用。事实上,Redis Lettuce 也可以自动配置,但 Redis Jedis 不是。请帮忙。
<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>
下面是我的配置文件
@Configuration public class SchedulerConfiguration
{
@Bean public LockProvider lockProvider(JedisPool jedisPool)
{
return new JedisLockProvider(jedisPool);
}
}
解决方案
您必须将 Spring 配置为使用并且在使用时Redis
应该使用。使用模板提供了简单的配置,并可以在 Spring 应用程序中快速设置和使用 redis。RedisTemplate
spring-data-redis
如果您使用基于注释的配置,则配置应如下所示
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;
}
}
然后在服务中使用
@Service
public class RedisService {
@Autowired
private RedisTemplate< String, Object > template;
// Methods to add and get objects from redis goes here
}
最后,主要课程,
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
}
}