欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

小白踩坑记:spring初始化时出现:factory-bean reference points back to the same bean definition

程序员文章站 2022-05-29 18:37:43
...

意思说的是你的factory-bean指向同一个bean定义,经过查看发现,我的类名和方法名一样(忽略大小写,因为当不指定名字时,默认是首字母小写作为bean的名字)

//
@Service
public class JedisPool {
    @Autowired
    RedisConfig redisConfig;
    @Bean
    public redis.clients.jedis.JedisPool jedisPool(){
        JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(redisConfig.getPoolMaxIdle());
        jedisPoolConfig.setMaxTotal(redisConfig.getPoolMaxTotal());
        jedisPoolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait()*1000 );
        //database表示库的个数
        redis.clients.jedis.JedisPool jp=new redis.clients.jedis.JedisPool(jedisPoolConfig,redisConfig.getHost(),redisConfig.getPort(),redisConfig.getTimeout()*1000,redisConfig.getPassword(),0);
        return jp;
    }
}

@Service和@Bean重复创建了一个名字叫jedisPool的bean,导致出现上述错误
解决:1、类名或方法名修改;
2、在创建bean时指定bean的名称(@Bean(“xxx”))

修改类名后,运行正常。

// An highlighted block
package com.dec14.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPoolConfig;
@Service
public class JedisPoolFactory {
    @Autowired
    RedisConfig redisConfig;
    @Bean
    public redis.clients.jedis.JedisPool jedisPool(){
        JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(redisConfig.getPoolMaxIdle());
        jedisPoolConfig.setMaxTotal(redisConfig.getPoolMaxTotal());
        jedisPoolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait()*1000 );
        //database表示库的个数
        redis.clients.jedis.JedisPool jp=new redis.clients.jedis.JedisPool(jedisPoolConfig,redisConfig.getHost(),redisConfig.getPort(),redisConfig.getTimeout()*1000,redisConfig.getPassword(),0);
        return jp;
    }
}

2020-12-18 15:44:36.552  INFO 19908 --- [           main] com.dec14.Demo01Application              : Starting Demo01Application using Java 12.0.2 on Fafasir with PID 19908 (D:\java-project\miaosha01\target\classes started by Administrator in D:\java-project\miaosha01)
2020-12-18 15:44:36.553  INFO 19908 --- [           main] com.dec14.Demo01Application              : No active profile set, falling back to default profiles: default
2020-12-18 15:44:37.490  INFO 19908 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-12-18 15:44:37.501  INFO 19908 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-12-18 15:44:37.501  INFO 19908 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2020-12-18 15:44:37.586  INFO 19908 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-12-18 15:44:37.586  INFO 19908 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 988 ms
2020-12-18 15:44:37.867  INFO 19908 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-12-18 15:44:38.006  INFO 19908 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-12-18 15:44:38.006  INFO 19908 --- [           main] com.dec14.Demo01Application              : Started Demo01Application in 1.733 seconds (JVM running for 2.401)

为中华之崛起而读书!!!
为了梦想
为了鸣鸣
fighting!!!!!
????????????????????????