简体   繁体   English

Spring Boot:简单地忽略配置类并且不加载

[英]Spring Boot: Configuration Class is simply ignored and not loaded

I have the following @Configuration class on the classpath of a few of my @SpringBootApplication s: 我在@SpringBootApplication的一些类路径上有以下@Configuration类:

@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);
    }

}

I have two services that use this config (with differently named Client classes). 我有两个使用此配置的服务(具有不同命名的Client类)。

Service 1 starts correctly and loads this config. 服务1正确启动并加载此配置。 I can see during start up that a bean of type ApiConfig was eagerly initialized. 我可以在启动时看到ApiConfig类型的bean被急切初始化。

Service 2 starts incorrectly: the above configuration class is simply ignored and not initialized. 服务2启动不正确:上述配置类被简单地忽略并且未初始化。

The services are started in separate JVMs. 这些服务在单独的JVM中启动。

Ther services have nearly identical, very small application.properties files: Ther服务有几乎相同的,非常小的application.properties文件:

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

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. 我现在已经经历了几个小时的日志并且没有看到明显的区别,只是它明显地忽略了我的@Configuration类。

Has anyone had this issue before? 以前有人有这个问题吗?

The @SpringBootApplication annotation (or, more precisely the inferred @ComponentScan annotation) by default only scans the classpath next to and below the annotated class. 默认情况下,@ SpringBootApplication注释(或更确切地说是推断的@ComponentScan注释)仅扫描注释类旁边和下面的类路径。

So, your configuration class must be placed next to or in a sub package of you Application class. 因此,您的配置类必须放在Application类的子包旁边或其中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM