Parameter 0 of method setFunctionDomainRedisTemplate in XXX required a single bean, but 2 were found
程序员文章站
2022-07-16 17:11:06
...
1. 问题描述
Parameter 0 of method setFunctionDomainRedisTemplate in com.uhope.hzz.util.RedisOperateUtil required a single bean, but 2 were found:
- redisTemplate: defined by method ‘redisTemplate’ in class path resource [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationRedisConfiguration.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
问题原因
- RedisAutoConfiguration源码
/**
* Standard Redis configuration.
*/
@Configuration
protected static class RedisConfiguration {
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean(StringRedisTemplate.class)
public StringRedisTemplate stringRedisTemplate(
RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
ConditionalOnMissingBean
这个注释表示如果容器没有这个类则加载,我的程序没有定义redisTemplate的Bean,所以这个地方自动加载了,但是容器里又有两个redisConnectionFactory,所以才报了上面的错误
2. 原因
- 项目maven中文件结构
- pom配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.uhope.rl</groupId>
<artifactId>rl-core-parent</artifactId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>rl-worklog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>${project.artifactId} v${project.version}</name>
<dependencies>
<dependency>
<groupId>com.uhope</groupId>
<artifactId>common-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.uhope.uip</groupId>
<artifactId>uip-base-service-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.uhope.rl</groupId>
<artifactId>rl-app-basicdata-service-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.uhope.rl</groupId>
<artifactId>rl-watersource-service-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 整合redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 阿波罗 -->
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
- 修改后项目结构
- 修改后pom依赖
<!-- 整合redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
改为
<dependency>
<groupId>com.uhope.uip</groupId>
<artifactId>redis-client</artifactId>
<version>1.0.1</version>
</dependency>
3. 解决方案
- 依赖的maven项目,有多个这样的RedisTemplate(可能这个项目里面配了一个,另外一个项目又配了一个,如果spring boot的话特别注意他自动配置里面的一个,一般都是那个和某个其他项目冲突了)
- 将配置的其中一个redisTemplate Bean的名称改为
redisTemplate
,这样RedisAutoConfiguration就不会再加载默认的了
下一篇: floodlight安装教程