代码之家  ›  专栏  ›  技术社区  ›  wonhee

在SpringBoot2.1中注入默认的“management.endpoints.web.base path”值

  •  1
  • wonhee  · 技术社区  · 7 年前

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

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

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

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

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

    我使用的是SpringBoot2.1.0.RELEASE。

    1 回复  |  直到 7 年前
        1
  •  3
  •   wonhee    7 年前

    这里是自动应答。

    WebEndpointProperties加载所有management.endpoints.web前缀属性,因此

    @Autowired
    private WebEndpointProperties webEndpointProperties;
    

    在课堂上,然后

    String actuatorWebBasePath = this.webEndpointProperties.getBasePath();
    

    在我的方法中,给了我一个基本路径(/exactor)。