gpt4 book ai didi

java - 考虑在您的配置中定义类型为 'redis.clients.jedis.JedisPool' 的 bean。集成Redis Jedis时出错

转载 作者:行者123 更新时间:2023-12-02 08:39:56 28 4
gpt4 key购买 nike

我正在使用 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-data-redis 时应使用 RedisTemplate。使用模板可以提供简单的配置,并可以在 Spring 应用程序中快速设置和使用 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
}
}

关于java - 考虑在您的配置中定义类型为 'redis.clients.jedis.JedisPool' 的 bean。集成Redis Jedis时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61443879/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com