1

I would like to NOT retrieve this property that way:

@Value("${spring.profiles.active:local}")
private String profile;

I wonder if there's actually a Spring Boot property bean - i.e. annotated with @ConfigurationProperties - which holds the spring.profiles.active (which is a Spring property, not Spring Boot's).

The same way we have e.g. SecurityProperties holding the spring.security prefix.

Thanks.

maxxyme
  • 2,164
  • 5
  • 31
  • 48
  • 2
    Possible duplicate of [How do you get current active/default Environment profile programmatically in Spring?](https://stackoverflow.com/questions/9267799/how-do-you-get-current-active-default-environment-profile-programmatically-in-sp) – Max Peng Oct 14 '19 at 09:58
  • @MaxPeng I don't think that's exactly a duplicate. I'm looking for a Spring Boot based solution, hence my reference to beans with `@ConfigurationProperties` which the other question don't talk about. – maxxyme Oct 14 '19 at 11:23
  • There is no bean holding this property as it is part of the `Environment`. – M. Deinum Oct 14 '19 at 11:40
  • @M.Deinum how would you explain the `SecurityProperties` bean then? – maxxyme Oct 14 '19 at 15:53
  • Profiles are a core Spring feature and not specific to Spring Boot. Spring Security on the other hand has no default way of passing properties so one can configure parts of it. Spring Boot needs some (optional) properties to configure Spring Security, hence the `SecurityProperties`. – M. Deinum Oct 15 '19 at 05:40

1 Answers1

4

You could use Environment:

@Autowired
Environment environment;

And then:

String[] activeProfiles = environment.getActiveProfiles();
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • I could. But now I'm realizing that's not exactly working as I might expect i.e. it doesn't return the *actual* active profiles, meaning you have to query the `getDefaultProfiles()` method on your own if the active profiles aren't set. Plus the whole thing can't be `@Validated` so I think I'll have to code it by mysef. Thanks. – maxxyme Oct 14 '19 at 15:53