已检测到循环@Import 在使用spring boot版本1.3.0.RC1之前的几个月,我创建了spring boot自动配置库
其结构是: 核心库 它包含扫描库中组件的@Configuration类已检测到循环@Import 在使用spring boot版本1.3.0.RC1之前的几个月,我创建了spring boot自动配置库,spring,spring-boot,Spring,Spring Boot,其结构是: 核心库 它包含扫描库中组件的@Configuration类 @Configuration @ComponentScan public class CoreLibAutoConfiguration { } 使用上述(核心)库创建的库。 它包含另一个@Configuration类。它的定义是 @Configuration @Import(CoreLibAutoConfiguration.class) @ComponentScan public class MyLibAutoConfi
@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 web应用程序,它使用spring boot版本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
,它也可以正常工作
我搜索了一下背后的原因,找到了一个帖子。它说这是由于配置类的嵌套造成的
有人能为我的问题提供正确的解决方案吗?作为一种修复,我已经更新了自动配置库的配置 我遵循的步骤是:
- 为
CoreLibAutoConfiguration
- 添加
以使用CoreLibAutoConfiguration
spring.factories进行自动配置
应在MyLibAutoConfiguration
初始化后初始化CoreLibAutoConfiguration
@Configuration
@ComponentScan
@AutoConfigureAfter(value = {CoreLibAutoConfiguration.class})
public class MyLibAutoConfiguration {
}
春天工厂
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
CoreLibAutoConfiguration,\
MyLibAutoConfiguration
这对我来说很好