在 Spring Boot 2.1 中注入默认 "management.endpoints.web.base-path" 值

Injecting default "management.endpoints.web.base-path" value in Spring Boot 2.1

我正在尝试将 "management.endpoints.web.base-path" 注入到我需要知道的 class 字段中。我搜索了几个小时,但所有答案都是 "how to customize your endpoint",方法是在 application.xml(或 yaml)中设置 management.endpoints.web.base-path,而不是 "how to get a default of management.endpoints.web.base-path"。

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



@Autowired
private Environment environment;

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

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

因为它不起作用,所以用@Value注解注入变量也不起作用。

当我在调试器中检查环境变量时,我能够看到 application.yaml 已加载并且所有覆盖变量也都在那里,但不是所有默认的 "management" 东西都在那里.

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

我正在使用 Spring Boot 2.1。0.RELEASE。

在这里自我回答。

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

@Autowired
private WebEndpointProperties webEndpointProperties;

在class然后

String actuatorWebBasePath = this.webEndpointProperties.getBasePath();

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