MALOUDA-PSA 2024-07-15 07:40 采纳率: 96.7%
浏览 2
已结题

sprintboot的https问题3


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

   tomcatServletWebServerFactory defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryConfiguration$EmbeddedTomcat.class]
┌─────┐
|  cn.mzhong.springboot.hotssl.config.HotSslTomcatAutoConfiguration (field private javax.net.ssl.X509TrustManager cn.mzhong.springboot.hotssl.config.HotSslTomcatAutoConfiguration.x509TrustManager)
└─────┘


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.
  • 写回答

2条回答 默认 最新

  • 关注

    上午好☀️☀️☀️️
    本答案参考ChatGPT-3.5

    这个问题是在描述Spring Boot应用中的依赖循环,这是非常危险的行为,并且违反了默认的规范。在这种情况下,建议直接删除这两个依赖项之间的循环。

    为了解决这个问题,你可以使用Spring Boot提供的@PreDestroy注解来提前清理这个对象(在这个例子中是TomcatServletWebServerFactory)。这是一个非常简单的方法:

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
        @PreDestroy
        public void destroy() {
            System.out.println("Cleaning up TomcatServletWebServerFactory...");
            try {
                TomcatServletWebServerFactory factory = (TomcatServletWebServerFactory) Class.forName("cn.mzhong.springboot.hotssl.config.HotSslTomcatAutoConfiguration");
                factory.stop();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    此外,你也可以在创建bean时添加@BeanDefinition elimination注解以自动消除依赖关系。这将确保依赖关系始终只存在于一个 bean 中。

    @BeanDefinition elimination = true
    public HotsslTomcatAutoConfiguration hotslTomcatAutoConfiguration() {
        return new HotsslTomcatAutoConfiguration();
    }
    
    public class MyApplication {
    
        @Bean(name = "myTomcat")
        public Tomcat myTomcat() {
            return new Tomcat();
        }
    }
    

    请注意,如果你的应用程序规模较大或包含多个组件,那么手动处理循环可能是必要的。例如,你可以为每个组件创建一个新的实例,并确保它们之间有明确的依赖关系。这样可以减少可能存在的环路。

    以上方法只是帮助你解决了问题的一部分。如果你的问题依然存在,你可能需要更深入地研究你的配置和应用程序的设计。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月26日
  • 已采纳回答 7月18日
  • 创建了问题 7月15日

悬赏问题

  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见
  • ¥15 一共有五道问题关于整数幂的运算还有房间号码 还有网络密码的解答?(语言-python)
  • ¥20 sentry如何捕获上传Android ndk 崩溃
  • ¥15 在做logistic回归模型限制性立方条图时候,不能出完整图的困难
  • ¥15 G0系列单片机HAL库中景园gc9307液晶驱动芯片无法使用硬件SPI+DMA驱动,如何解决?