已检测到循环@Import

时间:2016-06-22 08:58:53

标签: spring spring-boot

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

其结构是:

核心库

它包含@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版本1.3.5.RELEASE的spring-boot Web应用程序。

我尝试将几个月前创建的相同库集成到我的新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。它说它是由于嵌套配置类。

有人可以指导我解决问题的正确方法吗?

1 个答案:

答案 0 :(得分:1)

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

我遵循的步骤是:

  • 删除@Import
  • CoreLibAutoConfiguration
  • 使用CoreLibAutoConfiguration
  • 添加spring.factories进行自动配置 在MyLibAutoConfiguration初始化后,应初始化
  • CoreLibAutoConfiguration

最后代码将是:

<强> MyLibAutoConfiguration

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

}

<强> spring.factories

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

它适用于我。