2

I'd like to know if there's any programmatical way of getting all Spring profile names, active and inactive ?

There are several questions where only the names of active profiles were needed. My question isn't answered by such questions as I also need the names of inactive profiles.

Reason: I want to display all such profile names in a dropdown from which user can select which profile to use.

Ahmad
  • 12,886
  • 30
  • 93
  • 146
  • To whoever down voted, can you explain why you did so ? I think this is a fair question. – Ahmad Aug 18 '16 at 01:05
  • The *active* configuration files are loaded at *startup*. inactive profiles are not loaded. Also, you cannot change active profiles at runtime. It requires a restart of the application, or more precisely a reload of the spring container, e.g. see http://stackoverflow.com/q/28541138/5221149. – Andreas Aug 18 '16 at 01:06
  • This is a fair question to me as well.I want to display all possible profiles for a command line app. Calling the command without any argument will list all profiles. For the next command execution, user could provide the argument from the list shown in the console output. – Adi Sutanto Feb 14 '17 at 10:03

1 Answers1

4

There is no such thing.

When Spring initializes an ApplicationContext, it maintains the active profiles, that are easily retrievable from the system property spring.profiles.active.

As it goes through components to register corresponding bean definitions, it checks that the component matches some set of Condition objects. One of these is a ProfileCondition, which is

Condition that matches based on the value of a @Profile annotation.

For example, if you had a @Bean method annotated with @Profile

@Bean
@Profile("foo-profile")
public Foo foo() {
    return new Foo();
}

Spring would use the ProfileCondition to match foo-profile against the active profiles. If there is no match, Spring skips this bean definition.

It never actually stores all the possible @Profile values.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724