No qualifying bean of type 'org.springframework.data.redis.core.RedisTemplate' available: expected single matching bean but found 2: redisTemplate,stringRedisTemplate org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.data.redis.core.RedisTemplate' available: expected single matching bean but found 2: redisTemplate,stringRedisTemplate
时间: 2023-09-28 21:05:28 浏览: 80
这个问题是因为在 Spring 容器中存在多个类型为 `RedisTemplate` 的 Bean,而代码中又没有明确指定要注入哪一个 Bean,导致无法确定应该注入哪一个,从而抛出了 `NoUniqueBeanDefinitionException` 异常。
可以通过在代码中明确指定要注入的 Bean 的名称或者使用 `@Qualifier` 注解来解决这个问题。例如,假设存在名为 `redisTemplate1` 和 `redisTemplate2` 的两个 `RedisTemplate` Bean,可以按照以下方式明确指定要注入 `redisTemplate1`:
```java
@Autowired
@Qualifier("redisTemplate1")
private RedisTemplate redisTemplate;
```
或者使用 `@Qualifier` 注解:
```java
@Autowired
@Qualifier("redisTemplate1")
private RedisTemplate redisTemplate;
```
相关问题
No qualifying bean of type 'org.springframework.transaction.TransactionManager' available: expected single matching bean but found 2: dataTransactionManager,mongoTransactionManager
这个错误提示表明在 Spring 应用程序上下文中找到了两个类型为 `org.springframework.transaction.TransactionManager` 的 Bean,但是在使用时需要有且仅有一个匹配的 Bean。
解决这个问题的方法之一是使用 `@Primary` 注解来标识一个首选的 Bean,例如:
```
@Bean
@Primary
public PlatformTransactionManager primaryTransactionManager() {
// ...
}
```
另外,你还可以在注入 `TransactionManager` 的地方使用 `@Qualifier` 注解来指定使用哪个 Bean,例如:
```
@Autowired
@Qualifier("dataTransactionManager")
private PlatformTransactionManager transactionManager;
```
这样就可以避免因为存在多个匹配的 Bean 而导致的错误了。
org.springframework.beans.factory.nouniquebeandefinitionexception: no qualifying bean of type 'org.springframework.transaction.transactionmanager' available: expected single matching bean but found 2: transactionmanager,getdatasourcetransactionmanager
出现了这个异常是因为在 Spring 容器中找到了 2 个类型为 'org.springframework.transaction.transactionmanager' 的 bean,但是在需要使用时只能使用一个。这个异常的原因是没有符合要求的 bean。
相关推荐
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![epub](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)