当前位置: 首页 > 知识库问答 >
问题:

考虑在您的配置中定义一个类型为“redis.clients.jedis.jedispool”的bean。集成Redis Jedis时出错

范鸿畅
2023-03-14
<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);
    }
}

共有1个答案

田谦
2023-03-14

您必须将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
    }
}
 类似资料: