Spring循环依赖导致Bean无法正确初始化

1. 报错信息如下

dart 复制代码
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webConfig' defined in file [E:\code_repository\target\classes\com\kym\kymconfig\config\WebConfig.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDetailsServiceImpl': Unsatisfied dependency expressed through field 'dictService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dictServiceImpl': Unsatisfied dependency expressed through field 'aService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'AServiceImpl': Unsatisfied dependency expressed through field 'orderService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'orderServiceImpl': Unsatisfied dependency expressed through field 'order'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.kym.kymconfig.sdk.ISdkOrder' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

2. 代码如下

java 复制代码
@Configuration
@Order(1)
public class WebConfig {
    private UserDetailsService userDetailsService;

    @Bean
    public Map<String, Object> initMap() {
        return new HashMap<>();
    }

    public WebConfig(UserDetailsService userDetailsService) {
        this.userDetailsService = userDetailsService;
    }
}

@Order(2)
@Configuration
public class SdkConfig {
    @Autowired
    private DictService dictService;

    @Bean
    public ISdkOrder init() {
        try {
            int dict = dictService.getDict();
            return new SdkOrder(dict);
        } catch (Exception e) {
            return null;
        }
    }
}

public interface ISdkOrder {

    void track(@NonNull int eventRecord);
}


public interface AService {
    int doS();
}


public interface DictService {

    int getDict();
}

public interface OrderService {

    void track(int num);
}

public interface UserDetailsService {
    int loadUserByUsername(String username);
}

public class SdkOrder implements ISdkOrder {
    private final int worker;

    public SdkOrder(final int sdkProducer) {
        worker = sdkProducer;
    }

    @Override
    public void track(@NonNull int eventRecord) {
        System.out.println(eventRecord);
    }
}


@Service
public class AServiceImpl implements AService {
    @Autowired
    private OrderService orderService;
    @Override
    public int doS() {
        orderService.track(8);
        return 0;
    }
}

@Service
public class DictServiceImpl implements DictService {
    @Autowired
    private AService aService;
    @Override
    public int getDict() {
        aService.doS();
        return 5;
    }
}

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    @Lazy
    private ISdkOrder order;
    @Override
    public void track(int num) {
        order.track(num);
    }
}

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private DictService dictService;
    @Override
    public int loadUserByUsername(String username) {
        dictService.getDict();
        return 1;
    }
}

3. 依赖链分析

  1. 完整循环路径

    text 复制代码
    WebConfig → UserDetailsServiceImpl → DictServiceImpl → AServiceImpl → OrderServiceImpl → IOrder(需SdkConfig初始化)

    最终 SdkConfig 又依赖 DictService 形成闭环。

  2. 关键问题点

    • OrderServiceImpl 需要注入 ISdkOrder,但 ISdkOrder 的初始化依赖 DictService
    • DictService 又通过 AService 间接依赖 OrderService,形成死锁

4. 解决方案

方案:打破循环依赖

java 复制代码
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    @Lazy
    private ISdkOrder order;
    @Override
    public void track(int num) {
        order.track(num);
    }
}

在自动注入ISdkOrder时添加@Lazy注解。

这样解决了循环依赖问题,原因在于 @Lazy 注解改变了 Spring 的依赖注入行为。具体原理如下:

  1. 延迟加载机制
    @Lazy 使 Spring 不会在容器启动时立即注入真实的 ISdkOrder Bean,而是先注入一个代理对象。只有当第一次实际调用 order.track() 方法时,才会触发真实 Bean 的初始化。

  2. 打破初始化死锁

    原始循环依赖链:

    text 复制代码
    OrderServiceImpl → ISdkOrder → DictService → AService → OrderServiceImpl

    添加 @Lazy 后,ISdkOrder 的初始化被推迟,使得其他 Bean 能先完成初始化。

  3. 代理对象特性

    Spring 会生成一个 CGLIB 代理对象暂时代替 ISdkOrder,这个代理对象不依赖 DictService 等下游组件,因此容器可以顺利完成初始化阶段。

  4. 运行时解析

    当实际调用 track() 方法时,代理对象会检查真实 Bean 是否已初始化。此时其他组件都已就绪,可以安全创建真实 ISdkOrder 实例。

这种方案适用于需要保留原有代码结构的场景,但要注意:

  • 代理对象会增加少量性能开销
  • 首次调用可能出现短暂延迟
  • 需确保被 @Lazy 标注的字段不会在 @PostConstruct 方法中使用
相关推荐
程序员一诺python1 小时前
【Django开发】django美多商城项目完整开发4.0第2篇:项目准备,配置【附代码文档】
后端·python·django·框架
l0sgAi5 小时前
最新SpringAI 1.0.0正式版-实现流式对话应用
后端
parade岁月5 小时前
从浏览器存储到web项目中鉴权的简单分析
前端·后端
用户91453633083916 小时前
ThreadLocal详解:线程私有变量的正确使用姿势
后端
IT_10246 小时前
SpringBoot扩展——发送邮件!
java·spring boot·后端
用户4099322502126 小时前
如何在FastAPI中实现权限隔离并让用户乖乖听话?
后端·ai编程·trae
阿星AI工作室6 小时前
n8n教程:5分钟部署+自动生AI日报并写入飞书多维表格
前端·人工智能·后端
郝同学的测开笔记6 小时前
深入理解 kubectl port-forward:快速调试 Kubernetes 服务的利器
后端·kubernetes
Ray666 小时前
store vs docValues vs index
后端