V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
wozhidaole
V2EX  ›  Java

关于 Spring 循环依赖的问题

  •  
  •   wozhidaole · Mar 4, 2024 · 4415 views
    This topic created in 784 days ago, the information mentioned may be changed or developed.

    最近发现一个问题

    Linux 服务器通过 maven 打出来的部分项目,部署的时候会出现循环依赖报错 但是同样的代码,在 Windows 机器上打出来的 jar 包,部署在相同服务器上不会有循环依赖的报错 没有问题的项目中,也有部分存在循环依赖,但是也能正常部署,只有极个别的项目有上述的问题。

    springboot 版本 2.0.5 jdk 1.8

    Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'applyServiceImpl': Bean with name 'applyServiceImpl' has been injected into other beans [contractServiceImpl] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example

    8 replies    2024-03-04 19:09:05 +08:00
    suwu
        1
    suwu  
       Mar 4, 2024
    给个 dome ?
    BBCCBB
        2
    BBCCBB  
       Mar 4, 2024
    这个说不准的, 和他的加载顺序也有关系..
    有时候不同的机器可能就会出现.. A 机器没问题, B 机器可能就出现了.

    要么梳理代码, 避免循环依赖, 不过这个一般不可能, 要么就是 @Lazy 大法..
    yichengxian
        3
    yichengxian  
       Mar 4, 2024   ❤️ 3
    你这个啊 @Lazy 大法好啊
    nodododo
        4
    nodododo  
       Mar 4, 2024   ❤️ 1
    我记得我也遇到过,就是运行环境的加载顺序问题,因为 win 和 linux 的路径是不一样的,所以加载顺序导致了循环依赖的出现
    hengyunabc
        5
    hengyunabc  
       Mar 4, 2024
    升个高版本点的 spring boot 应该可以解决,它会保存依赖 jar 的顺序,避免不同环境加载 jar 顺序不一致。
    cookii
        6
    cookii  
       Mar 4, 2024 via Android
    是不是用了 @Async 注解?
    ychost
        7
    ychost  
       Mar 4, 2024
    这很正常的,代码里面循环依赖加个 Lazy 吧,我也遇到过本地能启动线上报错的情况,Spring 对 Bean 的加载 顺序是不定的
    bsg1992
        8
    bsg1992  
       Mar 4, 2024
    循环依赖这种情况 就应该避免。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5672 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 09:00 · PVG 17:00 · LAX 02:00 · JFK 05:00
    ♥ Do have faith in what you're doing.