Parameter 0 of method setFunctionDomainRedisTemplate in XXX required a single bean, but 2 were found

目录

    • 1. 问题描述
    • 问题原因
    • 2. 原因
    • 3. 解决方案

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/RedisAutoConfiguration R e d i s C o n f i g u r a t i o n . c l a s s ] − s t r i n g R e d i s T e m p l a t e : d e f i n e d b y m e t h o d ′ s t r i n g R e d i s T e m p l a t e ′ i n c l a s s p a t h r e s o u r c e [ o r g / s p r i n g f r a m e w o r k / b o o t / a u t o c o n f i g u r e / d a t a / r e d i s / R e d i s A u t o C o n f i g u r a t i o n RedisConfiguration.class] - stringRedisTemplate: defined by method 'stringRedisTemplate' in class path resource [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration RedisConfiguration.class]stringRedisTemplate:definedbymethodstringRedisTemplateinclasspathresource[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中文件结构
    Parameter 0 of method setFunctionDomainRedisTemplate in XXX required a single bean, but 2 were found_第1张图片
  • 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>
  • 修改后项目结构
    Parameter 0 of method setFunctionDomainRedisTemplate in XXX required a single bean, but 2 were found_第2张图片
  • 修改后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就不会再加载默认的了

你可能感兴趣的:(脱坑记)