@Qualifier DI Error
See original GitHub issueI defined it via bean name and used @Qulifier annotation. But it sends the following message
@Configuration
public class RedisConfig {
@Value("${spring.redis.session.host}")
private String host;
@Value("${spring.redis.session.port}")
private int sessionPort;
@Bean(name = "redisSessionConnectionFactory")
public RedisConnectionFactory redisSessionConnectionFactory() {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
configuration.setHostName(host);
configuration.setPort(sessionPort);
return new LettuceConnectionFactory(configuration);
}
@Bean
public RedisTemplate<String, Object> redisTemplate(@Qualifier(value = "redisSessionConnectionFactory") RedisConnectionFactory redisSessionConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setConnectionFactory(redisSessionConnectionFactory);
return redisTemplate;
}
}
@Configuration
public class CacheConfig {
@Value("${spring.redis.cache.host}")
private String cacheHost;
@Value("${spring.redis.cache.port}")
private int cachePort;
@Bean(name = "redisCacheConnectionFactory")
public RedisConnectionFactory redisCacheConnectionFactory() {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
configuration.setHostName(cacheHost);
configuration.setPort(cachePort);
return new LettuceConnectionFactory(configuration);
}
@Bean
public RedisCacheManager cacheManager(@Qualifier(value = "redisCacheConnectionFactory") RedisConnectionFactory redisCacheConnectionFactory) {
RedisCacheConfiguration defaultConfig = RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
Map<String, RedisCacheConfiguration> redisCacheConfigMap = new HashMap<>();
redisCacheConfigMap.put("board", defaultConfig.entryTtl(Duration.ofHours(1)));
redisCacheConfigMap.put("user", defaultConfig.entryTtl(Duration.ofSeconds(30L)));
RedisCacheManager redisCacheManager = RedisCacheManager.builder(redisCacheConnectionFactory)
.withInitialCacheConfigurations(redisCacheConfigMap)
.build();
return redisCacheManager;
}
}
Error Message
Parameter 1 of method sessionRepositoryFilterRegistration in org.springframework.boot.autoconfigure.session.SessionRepositoryFilterConfiguration required a single bean, but 2 were found:
- redisCacheConnectionFactory: defined by method 'redisCacheConnectionFactory' in class path resource [com/cwg/test/config/CacheConfig.class]
- redisSessionConnectionFactory: defined by method 'redisSessionConnectionFactory' in class path resource [com/cwg/test/config/RedisConfig.class]
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
Issue Analytics
- State:
- Created a year ago
- Comments:14 (8 by maintainers)
Top Results From Across the Web
A question about qualifier in Spring DI - Stack Overflow
The reason is simple: Spring DI first search determine all autowire candidates. if there is exactly one, the it uses this candidate ...
Read more >Db2 12 - Troubleshooting - Error qualifier - IBM
An error qualifier exists to provide more information about what was going on. The error qualifier can be found in the LOC keyword...
Read more >Dependency injection and programmatic lookup
Let's explore how the container determines which bean to inject in more advanced cases. We'll start by taking a closer look at qualifiers....
Read more >Contexts and Dependency Injection - Quarkus
Quarkus DI solution (also called ArC) is based on the Contexts and Dependency Injection for Java 2.0 specification. However, it is not a...
Read more >Spring @Qualifier with Constructors - Studytonight
The @Qualifier annotation in Spring is used to differentiate a bean among the same type of bean objects as shown in the following...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
We can’t fix this with
@ConditionalOnSingleCandidate
as the injection point is in Spring Session. https://github.com/spring-projects/spring-session/issues/2102 will hopefully result in@SpringSessionRedisConnectionFactory
being documented. #31514 and #31515 are tracking some failure analysis improvements. I don’t think there’s anything more that we can do here.You need to mark the one that Spring Boot should use using
@Primary
. I wonder if failing is what we should be doing vs. changing the condition to@ConditionalOnSingleCandidate
.