1

I have a Spring Boot application with several Java Config classes. Each class has its own @Profile name. 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.

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. 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.

Also tried to set spring.profiles.include in the props file with the same result.

Any ideas?

Sven
  • 892
  • 14
  • 29
  • tra using `-Dspring.profiles.active=config1,config2` from command line ehile running – pvpkiran Mar 05 '18 at 13:49
  • Could you please try using `-Dspring.profiles.active=profile1,profile2` instead? See if this helps. – higuaro Mar 05 '18 at 13:58
  • Doesn't matter if the profiles are set as a D-parameter or a program argument, same thing. Set `config1` only, works fine. Set `config2` only, works fine. Set both, just one config's beans are created. Order of the profiles doesn't matter. In Eclipse the config is *inclusive* and both profiles are used. On the command line there is an *override* behavior. – Sven Mar 05 '18 at 14:05
  • Could you try again but switching the order of the profiles? (was checking just [this other answer](https://stackoverflow.com/questions/23617831/what-is-the-order-of-precedence-when-there-are-multiple-springs-environment-pro)) – higuaro Mar 05 '18 at 14:20
  • There is no difference, always the Config class annotated with `config2` that runs but not the `config1` Config. And in Eclipse the order doesn't makes a difference, both Configs are running (as expected). – Sven Mar 05 '18 at 14:35
  • Are you sure things are working in Eclipse? The override behaviour sounds to me like you have multiple beans with the same name. In that case, the last bean that is defined with a particular name will win. It would be very easy to tell if you had shared a [minimal, complete, and verifiable example](/help/mcve). – Andy Wilkinson Mar 05 '18 at 14:51
  • The two profiles are doing completely different things, `config1` is configuring a data source consumer and `config2` a Netty server for TCP streaming. No beans are duplicated. They both run as expected from within any IDE but when started from the command line only the Netty server is started. I can start two instances with each profile and both runs perfectly, but when running both profiles there is no data consumer running. – Sven Mar 05 '18 at 14:58

1 Answers1

0

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. Debugging the Spring startup says that the config is found and the pre-creation is happening. But the real beans are never created and returned.

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. 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
    }
}
Sven
  • 892
  • 14
  • 29