简体   繁体   English

Spring Boot的多个配置文件可在IDE中使用,但不能从命令行使用

[英]Spring Boot multiple profiles works in IDE but not from command line

I have a Spring Boot application with several Java Config classes. 我有一个带有几个Java Config类的Spring Boot应用程序。 Each class has its own @Profile name. 每个类都有其自己的@Profile名称。 When running the application from Eclipse with the Program Arguments set to --spring.profiles.active=config1,config2 the server runs as expected with all beans from both Config classes running. 程序参数设置为--spring.profiles.active=config1,config2的情况下从Eclipse运行应用程序时--spring.profiles.active=config1,config2服务器将按预期运行,同时运行两个Config类的所有bean。

But when I try to start the application from the command line with --spring.profiles... only one of the profiles' beans are running, and the order doesn't matter. 但是,当我尝试使用--spring.profiles...从命令行启动应用程序时--spring.profiles...只有一个配置文件的bean正在运行,顺序无关紧要。 It is only the alphabetically last profile that is running. 仅按字母顺序的最后一个概要文件正在运行。 Even though the log says 即使日志说

The following profiles are active: config1,config2

Still none of the beans from config1 is kicking in. 仍然没有config1的bean启动。

Also tried to set spring.profiles.include in the props file with the same result. 还尝试将props文件中的spring.profiles.include设置为相同的结果。

Any ideas? 有任何想法吗?

This problem still puzzles me. 这个问题仍然让我感到困惑。 If any profile is used isolated they work, but when these two are combined the config1 profile's beans are never created. 如果隔离使用了任何概要文件,则它们可以工作,但是将两者结合使用时,将永远不会创建config1概要文件的bean。 Debugging the Spring startup says that the config is found and the pre-creation is happening. 调试Spring启动程序时,表明已找到配置并且正在进行预创建。 But the real beans are never created and returned. 但是真正的bean永远不会被创建和返回。

I made a wrapper class that wraps one of the beans in the config1 Java Config class to force Spring to create the beans from that particular config. 我创建了一个包装器类,该包装器将config1 Java Config类中的一个bean包装起来,以强制Spring从该特定配置中创建bean。 This is not the answer nor a solution to the problem but a work around. 这不是问题的答案,也不是解决方案,而是解决方法。

@Service
@Profile("config1")
public class DataConsumerWrapperService {

    public DataConsumerWrapperService(final MessageParser mp) {
        // Do nothing, just trigger the parser bean creation
    }
}

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

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