Vaisakh PS
Vaisakh PS

Reputation: 1201

Error while creating Bean even though the Dependency Bean is there

I have my Configuration Class with some Dependent Beans

public class WebConfig{
    @Bean
        @Qualifier("geojedis")
        public StringRedisTemplate geoJedisTemplate(
                @Qualifier("geographyJedisConnectionFactory") final JedisConnectionFactory connectionFactory) {

            // Create a RedisTemplate implementation which is basically of string
            // data structure.
            StringRedisTemplate redisTemplate = new StringRedisTemplate(connectionFactory);

            return redisTemplate;
        }

    @Bean
        @Qualifier("capacityStringRedisTemplate")
        public StringRedisTemplate capacityStringRedisTemplate(
                @Qualifier("capacityJedisConnectionFactory") final JedisConnectionFactory connectionFactory) {

            // Create a RedisTemplate implementation which is basically of string
            // data structure.
            StringRedisTemplate redisTemplate = new StringRedisTemplate(connectionFactory);

            return redisTemplate;
        }

    @Bean
        public JedisConnectionFactory geographyJedisConnectionFactory() {
            JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
            return connectionFactory;
        }

    @Bean
        public JedisConnectionFactory capacityJedisConnectionFactory() {
            JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
            return connectionFactory;
        }
    }

But I am getting the below error. When i checked the configurations all are fine and I have also defined the Qualifier for mapping the correct dependencies. Any help is much appreciated.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisTemplate' defined in class path resource [org/springframework/boot/autoconfigure/redis/RedisAutoConfiguration$RedisConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.data.redis.connection.RedisConnectionFactory]: : No qualifying bean of type [org.springframework.data.redis.connection.RedisConnectionFactory] is defined: expected single matching bean but found 2: geographyJedisConnectionFactory,capacityJedisConnectionFactory; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.data.redis.connection.RedisConnectionFactory] is defined: expected single matching bean but found 2: geographyJedisConnectionFactory,capacityJedisConnectionFactory

Upvotes: 4

Views: 17337

Answers (3)

satyadeep singh
satyadeep singh

Reputation: 41

use @EnableAutoConfiguration(exclude = RedisAutoConfiguration.class) above your config class and provide the custom connection properties

Upvotes: 1

Yoory N.
Yoory N.

Reputation: 5484

There is a bean inside RedisAutoConfiguration that is created if there is no default "redisTemplate" in Spring Context.

@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(
        RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
    RedisTemplate<Object, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(redisConnectionFactory);
    return template;
}

This one needs a single RedisConnectionFactory, but finds two.

As a work-around you can create a dummy RedisTemplate with the name "redisTemplate" and not use it.

Since it checks by bean name, the following could be enough as long as nothing tries to @Autowire it:

@Bean
public Object redisTemplate() {
    return new Object();
}

Upvotes: 3

Maciej Kowalski
Maciej Kowalski

Reputation: 26572

You can simply call the connection factory bean creation method instead of injection:

    @Bean       
    public StringRedisTemplate capacityStringRedisTemplate() {

        // Create a RedisTemplate implementation which is basically of string
        // data structure.
        StringRedisTemplate redisTemplate = 
             new StringRedisTemplate(capacityJedisConnectionFactory());

        return redisTemplate;
    }

This will point directly to the one your looking for

Upvotes: 1

Related Questions