Spring boot 2.0 升级到 3.3.1 的相关问题 (七)

Spring boot 2.0 升级到 3.3.1 的相关问题 (七)

解决循环依赖的问题

问题描述

在3.3.1 将不再允许两个Bean 互相依赖或者在同一个配置类里直接注入自己管理的Bean。否则都会提示循环依赖。类似于下面这些提示信息

demo1
@Configuration
public class CycleDependencyTest {
    @Autowired
    @Qualifier("myName")
    private String myName;


    @Bean("myName")
    public String getMyName(){
        return "abc";
    }

}
2024-07-25T18:04:27.004+08:00  WARN 29756 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cycleDependencyTest': Unsatisfied dependency expressed through field 'myName': Error creating bean with name 'cycleDependencyTest': Requested bean is currently in creation: Is there an unresolvable circular reference?


***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

┌──->──┐
|  cycleDependencyTest (field private java.lang.String com.feiyizhan.CycleDependencyTest.myName)
└──<-──┘


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.

demo2
@Configuration
public class CycleDependencyTest2 {
    @Autowired
    private CycleDependencyTest3 cycleDependencyTest3;
}

@Configuration
public class CycleDependencyTest3 {
    @Autowired
    private CycleDependencyTest2 cycleDependencyTest2;
}
2024-07-25T18:07:45.905+08:00  WARN 17116 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cycleDependencyTest2': Unsatisfied dependency expressed through field 'cycleDependencyTest3': Error creating bean with name 'cycleDependencyTest3': Unsatisfied dependency expressed through field 'cycleDependencyTest2': Error creating bean with name 'cycleDependencyTest2': Requested bean is currently in creation: Is there an unresolvable circular reference?


***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

┌─────┐
|  cycleDependencyTest2 (field private com.feiyizhan.CycleDependencyTest3 com.feiyizhan.CycleDependencyTest2.cycleDependencyTest3)
↑     ↓
|  cycleDependencyTest3 (field private com.feiyizhan.CycleDependencyTest2 com.feiyizhan.CycleDependencyTest3.cycleDependencyTest2)
└─────┘


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.

问题解决

demo1 的解决方案

将构建Bean的地方和使用待注入Bean的地方不要放在一起,拆分为两个类就可以了。

@Configuration
public class CycleDependencyFixTest {

    @Bean("myName")
    public String getMyName(){
        return "abc";
    }

}

@Configuration
public class CycleDependencyTest {
    @Autowired
    @Qualifier("myName")
    private String myName;

}
demo2的解决方案
@Lazy

对某个注入的Bean增加@Lazy注解设置为懒加载,

@Configuration
public class CycleDependencyTest2 {
    @Autowired
    private CycleDependencyTest3 CycleDependencyTest3;
}

@Configuration
public class CycleDependencyTest3 {
    @Autowired
    @Lazy
    private CycleDependencyTest2 CycleDependencyTest2;
}
方法2 修改代码结构

引入一个中间层,让它分别持有这两个Bean。

@Configuration
public class CycleDependencyTest2 {
}

@Configuration
public class CycleDependencyTest3 {
}

@Configuration
@Data
public class CycleDependencyFix2Test {
    @Autowired
    private CycleDependencyTest3 cycleDependencyTest3;
    @Autowired
    private CycleDependencyTest2 cycleDependencyTest2;

    public void print(){
        System.out.println(cycleDependencyTest2);
        System.out.println(cycleDependencyTest3);
    }

}

方法3 启用支持循环依赖的配置(不推荐)

启用支持循环依赖的配置,这个可以作为应急手段,但最好还是对整个项目做改造。

#启用支持循环依赖
spring.main.allow-circular-references=true

Log4j2 和 Dubbo的冲突

问题描述

启动的时候会出现警告
WARN The use of package scanning to locate plugins is deprecated and will be removed in a future release

原因就是最新版的 Log4j2 和 Dubbo的存在一定到兼容问题,使用了已经废弃的属性配置
可以参考:
https://github.com/alibaba/nacos/pull/12332
https://logging.apache.org/log4j/2.x/manual/configuration.html#ConfigurationSyntax

解决方案

官方暂时没有解决,蹲后续。

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值