简体   繁体   English

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

[英]Consider defining a bean of type 'redis.clients.jedis.JedisPool' in your configuration. error while integrating Redis Jedis

I am implementing Redis Jedis with spring boot application.我正在使用 spring 引导应用程序实现 Redis Jedis。 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. "执行此操作时出现错误“考虑在您的配置中定义一个类型为 'redis.clients.jedis.JedisPool' 的 bean。

While reading the redis host, port and password from yml manually then it works fine.在手动从 yml 读取 redis 主机、端口和密码时,它工作正常。 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.但由于我使用的是spring-boot-starter-data-redis依赖项,所以我不想从 yml 文件中读取 redis 主机、端口、密码,实际上它应该自动与 @configuration 和 @bean 一起使用。 In fact Redis Lettuce also woking with autoconfigyration fine but Redis Jedis is not.事实上 Redis 生菜也可以自动配置,但 Redis Jedis 不是。 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);
    }
}

You have to configure Spring to use the Redis and should use the RedisTemplate when using spring-data-redis .您必须配置 Spring 才能使用Redis并且在使用spring-data-redis时应该使用RedisTemplate Using the template provides easy configurations and enables quick setup and use redis in the Spring applications.使用该模板可提供简单的配置并实现快速设置,并在 Spring 应用中使用 redis。

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,最后,主要的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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 应用程序未运行,“考虑在您的配置中定义一个类型的 bean。” - Application not running, ''Consider defining a bean of type in your configuration." Jedis 和 Redis 配置与 Springboot - Jedis and Redis configuration with Springboot 使用 Logger 时出现错误 - “考虑在您的配置中定义类型为‘org.slf4j.Logger’的 bean。” - Getting error while using Logger - "Consider defining a bean of type 'org.slf4j.Logger' in your configuration." Redis问题考虑在您的配置中定义类型为“ org.springframework.data.redis.core.HashOperations”的bean - Redis Issue Consider defining a bean of type 'org.springframework.data.redis.core.HashOperations' in your configuration 考虑在配置中重新访问以上条目或定义类型为“ org.springframework.data.redis.core.RedisTemplate”的bean - Consider revisiting the entries above or defining a bean of type 'org.springframework.data.redis.core.RedisTemplate' in your configuration 考虑在您的配置中定义“io.ipl.amaresh.data.JobCompletionNotificationListener”类型的 bean。 那个豆子找不到 - Consider defining a bean of type 'io.ipl.amaresh.data.JobCompletionNotificationListener' in your configuration. The bean could not be found in that 错误redis客户端jedis HostAndPort无法解析localhost地址 - Error redis clients jedis HostAndPort cant resolve localhost address 使用 Redis 运行 Storm 时出错:java.lang.NoClassDefFoundError: Lredis/clients/jedis/Jedis - Error running Storm with Redis: java.lang.NoClassDefFoundError: Lredis/clients/jedis/Jedis 考虑在您的配置中定义一个“org.hibernate.SessionFactory”类型的 bean。 - 春天 - Consider defining a bean of type 'org.hibernate.SessionFactory' in your configuration. - Spring 考虑在您的配置中定义“com.project.airCompanies.repo.AirCompanyRepository”类型的 bean。 带MySQL数据库 - Consider defining a bean of type 'com.project.airCompanies.repo.AirCompanyRepository' in your configuration. With MySQL database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM