【问题标题】:Which Spring Boot @ConfigurationProperties annotated bean holds the spring.profiles.active property?哪个 Spring Boot @ConfigurationProperties 注释 bean 拥有 spring.profiles.active 属性?
【发布时间】:2020-02-10 22:16:16
【问题描述】:

我不想那样检索这个属性:

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

我想知道是否真的有一个 Spring Boot 属性 bean - 即用 @ConfigurationProperties 注释 - 它包含 spring.profiles.active(这是一个 Spring 属性,而不是 Spring Boot 的)。

与我们的方式相同,例如SecurityProperties 持有 spring.security 前缀。

谢谢。

【问题讨论】:

  • @MaxPeng 我不认为这完全是重复的。我正在寻找一个基于 Spring Boot 的解决方案,因此我使用 @ConfigurationProperties 引用了其他问题未提及的 bean。
  • 没有 bean 持有此属性,因为它是 Environment 的一部分。
  • @M.Deinum 那么你将如何解释SecurityProperties bean?
  • Profiles 是 Spring 的核心特性,并不特定于 Spring Boot。另一方面,Spring Security 没有传递属性的默认方式,因此可以配置其中的一部分。 Spring Boot 需要一些(可选)属性来配置 Spring Security,因此 SecurityProperties.

标签: java spring spring-boot configurationproperties


【解决方案1】:

你可以使用Environment:

@Autowired
Environment environment;

然后:

String[] activeProfiles = environment.getActiveProfiles();

【讨论】:

  • 我可以。但现在我意识到这并不完全像我预期的那样工作,即它不会返回 actual 活动配置文件,这意味着如果活动配置文件是,您必须自己查询 getDefaultProfiles() 方法没有设置。再加上整个事情不能是@Validated,所以我想我必须由 mysef 对其进行编码。谢谢。
猜你喜欢
  • 2020-10-26
  • 1970-01-01
  • 2015-12-14
  • 2020-07-13
  • 2018-08-15
  • 1970-01-01
  • 1970-01-01
  • 2017-06-24
  • 2021-04-25
相关资源
最近更新 更多