1

我正在尝试将“management.endpoints.web.base-path”注入我需要知道的班级字段。我做了几个小时的搜索,但所有答案都是“如何通过在 application.xml(或 yaml)中设置 management.endpoints.web.base-path 来自定义您的端点”,而不是“如何获得默认值” management.endpoints.web.base-path”。

下面的简单代码期望在 Spring Boot 应用程序启动时获取加载的任何变量,但在变量中没有检索到任何内容。



@Autowired
private Environment environment;

public void myMethod(){
    final String actuatorBase = environment.getProperty("management.endpoints.web.base-path");
}

如果我在 application.properties 中定义它,我应该可以肯定地加载它,但我想知道为什么这里不能检索到 default("/actuator")。但是,当我运行应用程序时,我可以通过 /actuator 访问所有与执行器相关的功能端点。

由于它不起作用,因此使用 @Value 注释注入变量也不起作用。

当我在调试器中检查环境变量时,我能够看到 application.yaml 已加载并且所有覆盖变量也都在那里,但并非所有默认的“管理”内容都在那里。

任何想法?这个应用程序有一些自定义配置,没有使用所有 AutoConfigurer 的东西,所以想知道是否需要使用特定的自动配置来实现它。

我正在使用 Spring Boot 2.1.0.RELEASE。

4

1 回答 1

3

在这里自答。

WebEndpointProperties 是加载所有 management.endpoints.web 前缀属性的属性,所以很简单

@Autowired
private WebEndpointProperties webEndpointProperties;

在课堂上,然后

String actuatorWebBasePath = this.webEndpointProperties.getBasePath();

在我的方法中给了我一个基本路径(/执行器)。

于 2018-12-04T01:19:32.123 回答