gpt4 book ai didi

spring - 检测到循环@Import

转载 作者:行者123 更新时间:2023-12-04 12:46:40 27 4
gpt4 key购买 nike

  • 我在使用 spring-boot 版本前几个月创建了 spring-boot 自动配置库 1.3.0.RC1 .

  • 它的结构是:

    核心库

    它包含扫描库中组件的@Configuration 类。
    @Configuration
    @ComponentScan
    public class CoreLibAutoConfiguration {

    }

    使用上述(核心)库创建的库。

    它包含另一个@Configuration 类。它的定义是
    @Configuration
    @Import(CoreLibAutoConfiguration.class)
    @ComponentScan
    public class MyLibAutoConfiguration {

    }

    它还包含 spring.factories在它的类路径中。其中包含在应用程序启动时自动配置此库的条目。
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=MyLibAutoConfiguration
    现在我已将此库集成到我的 spring-boot web 应用程序中,该应用程序再次使用相同的 spring-boot 版本 1.3.0.RC1 .

    一切都按预期进行。

    但是,现在正在开发另一个使用 spring-boot 版本的 spring-boot web 应用程序 1.3.5.RELEASE .

    我尝试将几个月前创建的同一个库集成到我的新 Web 应用程序中。但我因与循环引用相关的错误而失败。堆栈跟踪是
    org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: A circular @Import has been detected: Illegal attempt by @Configuration class 'MyLibAutoConfiguration' to import class 'MyLibAutoConfiguration' as 'MyLibAutoConfiguration' is already present in the current import stack [MyLibAutoConfiguration->Application]
    Offending resource: class path resource [mylib/MyLibAutoConfiguration.class]
    at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:475) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:278) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:232) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:191) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:272) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:232) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:510) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:278) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:232) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.processImports(ConfigurationClassParser.java:510) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.processDeferredImportSelectors(ConfigurationClassParser.java:455) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:186) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:321) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:98) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:678) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:520) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at myapp.Application.main(Application.java:15) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_45]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_45]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_45]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_45]
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:478) [spring-boot-maven-plugin-1.3.5.RELEASE.jar:1.3.5.RELEASE]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_45]

    如果我将 spring-boot 的版本更改为 1.3.0.RC1对于我的新应用程序,它再次正常工作。

    我搜索了背后的原因,发现了一个帖子 https://jira.spring.io/browse/SPR-13101 .它说这是由于配置类的嵌套。

    任何人都可以指导我解决我的问题吗?

    最佳答案

    作为修复,我更新了我的自动配置库的配置。

    我遵循的步骤是:

  • 删除 @ImportCoreLibAutoConfiguration
  • 添加 CoreLibAutoConfiguration使用 spring.factories 自动配置
  • MyLibAutoConfiguration应该在 CoreLibAutoConfiguration 之后初始化被初始化。

  • 最后的代码将是:

    MyLibAutoConfiguration
    @Configuration
    @ComponentScan
    @AutoConfigureAfter(value = {CoreLibAutoConfiguration.class})
    public class MyLibAutoConfiguration {

    }

    spring.factories
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    CoreLibAutoConfiguration,\
    MyLibAutoConfiguration

    它对我来说很好用。

    关于spring - 检测到循环@Import,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37963213/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com