Spring Boot:配置类被简单地忽略而不加载 [英] Spring Boot: Configuration Class is simply ignored and not loaded
问题描述
我的一些 @SpringBootApplication
的类路径上有以下 @Configuration
类:
I have the following @Configuration
class on the classpath of a few of my @SpringBootApplication
s:
@Configuration
@Import({MainConfig.class, RestConfig.class})
public class ApiConfig {
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public Client client() throws ExecutionException, InterruptedException {
return service.create(Client.class);
}
}
我有两个使用此配置的服务(具有不同名称的 Client
类).
I have two services that use this config (with differently named Client
classes).
服务 1 正确启动并加载此配置.我可以在启动期间看到 ApiConfig
类型的 bean 被急切地初始化.
Service 1 starts correctly and loads this config. I can see during start up that a bean of type ApiConfig
was eagerly initialized.
Service 2 启动不正确:上面的配置类只是被忽略了,没有初始化.
Service 2 starts incorrectly: the above configuration class is simply ignored and not initialized.
服务在单独的 JVM 中启动.
The services are started in separate JVMs.
这些服务具有几乎相同的非常小的 application.properties
文件:
Ther services have nearly identical, very small application.properties
files:
spring.application.name=xxx-api
server.port=0
eureka.name=xxx.api
# Only for reading properties from a central location
context.initializer.classes=com.package.contextClass
我什至不确定我可以在问题中写入什么样的附加信息.我已经浏览了几个小时的日志,没有看到明显的区别,只是它明显忽略了我的 @Configuration
类.
I'm not even sure what kind of additional information I could write into the question. I have been going through logs for a couple of hours now and see no discernible difference, simply that it plainly ignores my @Configuration
class.
以前有人遇到过这个问题吗?
Has anyone had this issue before?
推荐答案
默认情况下,@SpringBootApplication 注解(或者,更准确地说是推断的 @ComponentScan 注解)仅扫描被注解的类旁边和下方的类路径.
The @SpringBootApplication annotation (or, more precisely the inferred @ComponentScan annotation) by default only scans the classpath next to and below the annotated class.
因此,您的配置类必须放在 Application 类的旁边或子包中.
So, your configuration class must be placed next to or in a sub package of you Application class.
这篇关于Spring Boot:配置类被简单地忽略而不加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!