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

Krishna :

I am implementing Redis Jedis with spring boot application. 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."

While reading the redis host, port and password from yml manually then it works fine. 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. In fact Redis Lettuce also woking with autoconfigyration fine but Redis Jedis is not. 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);
    }
}
Klaus :

You have to configure Spring to use the Redis and should use the RedisTemplate when using spring-data-redis. Using the template provides easy configurations and enables quick setup and use redis in the Spring applications.

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,

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
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Consider revisiting the entries above or defining a bean of type 'org.springframework.data.redis.core.RedisTemplate' in your configuration

Consider defining a bean of type 'package' in your configuration [Spring-Boot]

Redisson vs Jedis for redis

Consider defining a bean of type 'service' in your configuration [Spring boot]

Consider defining a bean of type in your configuration

Consider defining a bean of type 'org.springframework.data.mongodb.core.MongoTemplate' in your configuration

Redis Key expire notification with Jedis

Spring Boot - Injecting Repository into Controller throws Consider defining a bean of type 'Type' in your configuration

Redis/Jedis - Delete by pattern?

Consider defining a bean of type 'UserConverter' in your configuration

Consider defining a bean of type * in your configuration

JedisCluster : redis.clients.jedis.exceptions.JedisNoReachableClusterNodeException: No reachable node in cluster

Redis/Jedis not serving properly

Field in required a bean of type that could not be found consider defining a bean of type in your configuration

Redis Issue Consider defining a bean of type 'org.springframework.data.redis.core.HashOperations' in your configuration

Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration

redis.clients.jedis.exceptions.JedisException: Could not return the resource to the pool

redis.clients.jedis.exceptions.JedisConnectionException: java.net.UnknownHostException

SpringBootTest - Consider defining a bean of type 'java.lang.String' in your configuration

Consider defining a bean of type 'javax.servlet.ServletContext' in your configuration

redis.clients.jedis.exceptions.JedisDataException: ERR Error compiling script (new function): user_script:1: malformed number near

APPLICATION FAILED TO START - Consider defining a bean of type in your configuration [SpringBoot]

Connect refused for jedis with redis cluster

Flink: java.io.NotSerializableException: redis.clients.jedis.JedisCluster

Consider defining a bean of type 'io.ipl.amaresh.data.JobCompletionNotificationListener' in your configuration. The bean could not be found in that

Consider defining a bean of type 'int' in your configuration[SpringBoot]

Consider defining a bean of type '[3rd party dependency]' in your configuration

Consider defining a bean of type 'org.modelmapper.ModelMapper' in your configuration

Jedis TLS connection to Redis Cluster

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive