redisConfiguration报错
来源:13-6 service的迁移
Vtech
2021-12-12
Description:
The dependencies of some of the beans in the application context form a cycle:
┌──->──┐
| redisConfiguration (field private redis.clients.jedis.JedisPoolConfig com.imooc.o2o.config.redis.RedisConfiguration.jedisPoolConfig)
└──<-──┘
Action:
Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘redisConfiguration’: Unsatisfied dependency expressed through field ‘jedisPoolConfig’; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘redisConfiguration’: Requested bean is currently in creation: Is there an unresolvable circular reference?
报错原因说好像产生循环注入/依赖了?
我看视频直接用的最后项目给的配置,我用的也是最后项目的配置,应该没问题呀。properties也加了的。
是不是2.6版本Spring的检测特性?
我照着他的建议加一个设置允许循环注入试试。
在配置中加了个spring.main.allow-circular-references=true,可以了
是2.6版本Spring的特性,不允许循环注入/依赖,有相同问题的同学可以这样解决
3回答
-
qq_慕的地8417648
2022-01-27
大佬牛
10 -
qq_慕瓜4505296
2022-07-11
循环依赖产生的原因是创建和调用都在同一个类中进行的,不妨改一下注入的方式,在方法的形参当中使用自动注入即可,下面这个是我修改后的代码,单元测试执行成功
package com.JeTAime.o2oschoolshop.config.redis; import com.JeTAime.o2oschoolshop.cache.JedisPoolWriper; import com.JeTAime.o2oschoolshop.cache.JedisUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import redis.clients.jedis.JedisPoolConfig; /** * spring-redis.xml里的配置 * * @author xiangze */ @Configuration public class RedisConfiguration { @Value("${redis.hostname}") private String hostname; @Value("${redis.port}") private int port; @Value("${redis.pool.maxActive}") private int maxTotal; @Value("${redis.pool.maxIdle}") private int maxIdle; @Value("${redis.pool.maxWait}") private long maxWaitMillis; @Value("${redis.pool.testOnBorrow}") private boolean testOnBorrow; @Value("${redis.password}") private String password; /** * 创建redis连接池的设置 * * @return */ @Bean(name = "jedisPoolConfig") public JedisPoolConfig createJedisPoolConfig() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); // 控制一个pool可分配多少个jedis实例 jedisPoolConfig.setMaxTotal(maxTotal); // 连接池中最多可空闲maxIdle个连接 ,这里取值为20, // 表示即使没有数据库连接时依然可以保持20空闲的连接, // 而不被清除,随时处于待命状态。 jedisPoolConfig.setMaxIdle(maxIdle); // 最大等待时间:当没有可用连接时, // 连接池等待连接被归还的最大时间(以毫秒计数),超过时间则抛出异常 jedisPoolConfig.setMaxWaitMillis(maxWaitMillis); // 在获取连接的时候检查有效性 jedisPoolConfig.setTestOnBorrow(testOnBorrow); return jedisPoolConfig; } /** * 创建Redis连接池,并做相关配置 * * @return */ @Bean(name = "jedisWritePool") public JedisPoolWriper createJedisPoolWriper(@Autowired JedisPoolConfig jedisPoolConfig) { JedisPoolWriper jedisPoolWriper = new JedisPoolWriper(jedisPoolConfig, hostname, port, password); return jedisPoolWriper; } /** * 创建Redis工具类,封装好Redis的连接以进行相关的操作 * * @return */ @Bean(name = "jedisUtil") public JedisUtil createJedisUtil(@Autowired JedisPoolWriper jedisWritePool) { JedisUtil jedisUtil = new JedisUtil(); jedisUtil.setJedisPool(jedisWritePool); return jedisUtil; } /** * Redis的key操作 * * @return */ @Bean(name = "jedisKeys") public JedisUtil.Keys createJedisKeys(@Autowired JedisUtil jedisUtil) { JedisUtil.Keys jedisKeys = jedisUtil.new Keys(); return jedisKeys; } /** * Redis的Strings操作 * * @return */ @Bean(name = "jedisStrings") public JedisUtil.Strings createJedisStrings(@Autowired JedisUtil jedisUtil) { JedisUtil.Strings jedisStrings = jedisUtil.new Strings(); return jedisStrings; } }
00 -
翔仔
2021-12-13
感谢同学的分享,可能redis类包的bean出现了循环依赖:)
00
相似问题