考虑在您的配置中定义“redis.clients.jedis.jedispool”类型的bean集成redis绝地时出错

2skhul33  于 2021-06-09  发布在  Redis
关注(0)|答案(1)|浏览(518)

我正在用spring boot应用程序实现redis jedis。我正在使用下面的依赖关系和配置。在执行此操作时,我得到一个错误“考虑在配置中定义一个'redis.clients.jedis.jedispool'类型的bean。”
当从yml手动读取redis主机、端口和密码时,它工作正常。但是由于我使用的是springbootstarter数据redis依赖,所以我不想从yml文件中读取redis主机、端口和密码,事实上它应该自动与@configuration和@bean一起工作。事实上,redis莴苣也可以与自动配置很好,但redis绝地不是。请帮忙。

<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);
    }
}
dffbzjpn

dffbzjpn1#

您必须配置spring才能使用 Redis 应该使用 RedisTemplate 使用时 spring-data-redis . 使用该模板提供了简单的配置,并支持在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
    }
}

相关问题