如何在不使用application.properties文件的情况下曝光Spring启动执行器端点?

发布于 2025-02-08 06:20:09 字数 274 浏览 2 评论 0原文

如果我无法访问application.properties文件,则如何在Spring Boot 2.5.8中启用和曝光执行器端点?

使用application.properties文件,只需配置

Management.endpoints.web.exposus.include =*

但是如果我无法访问application, .properties,我该如何用代码进行?

How can I enable and exposure actuator endpoints in Spring boot 2.5.8 application if I don't have access to application.properties file?

It's easy to do with application.properties file, just configuring

management.endpoints.web.exposure.include=*

But if I don't have access to application.properties, how can I do it in code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彼岸花似海 2025-02-15 06:20:09

您可以将属性添加为环境变量。只需要切换套管和 <代码> _

management_endpoints_web_exposure_include =*

You can add the properties as environment variables. Just need to switch casing and . to _

MANAGEMENT_ENDPOINTS_WEB_EXPOSURE_INCLUDE=*

后eg是否自 2025-02-15 06:20:09

您可以使用命令行进行相同的操作。

mvn spring-boot:run -Dspring-boot.run.jvmArguments='Dmanagement.endpoints.web.exposure.include=*'

You can use command line to do the same.

mvn spring-boot:run -Dspring-boot.run.jvmArguments='Dmanagement.endpoints.web.exposure.include=*'
夜清冷一曲。 2025-02-15 06:20:09

最后,我找到了2个解决方案。

  1. 在运行应用程序之前使用springApplication.setDefaultProperties()方法:

     公共类myapplication {
        公共静态void main(string [] args){
            SpringApplication App = new SpringApplication(MyApplication.Class);
            app.setDefaultProperties(collections.singletonMap(“ Management.Endpoints.web.exposure.include”,“*”));
            app.run(args);
        }
    }
    
     
  2. 第二种方法需要访问属性文件。

要使用propertiesConfiguration打开属性文件(此类可以:从文件中读取属性并将新属性/值写入文件)。
检查属性是否已经存在
如果不存在 - 添加。

PropertiesConfiguration config = getConfig(filePathToPropertiesFile);
if (null == config.getString("management.endpoints.web.exposure.include")) {
    config.setProperty("management.endpoints.web.exposure.include", "*");
    config.save();
}

在应用程序运行之前,所有这些操作还需要以主方法执行。

Finally, I found 2 solutions.

  1. To use SpringApplication.setDefaultProperties() method before run application:

    public class MyApplication {
        public static void main(String[] args) {
            SpringApplication app = new SpringApplication(MyApplication.class);
            app.setDefaultProperties(Collections.singletonMap("management.endpoints.web.exposure.include", "*"));
            app.run(args);
        }
    }
    
    
  2. Second approach requires access to properties file.

To open properties file with PropertiesConfiguration (this class can both: read properties from file and write new properties/values to file).
Checked if property already exist
If not exist - add it.

PropertiesConfiguration config = getConfig(filePathToPropertiesFile);
if (null == config.getString("management.endpoints.web.exposure.include")) {
    config.setProperty("management.endpoints.web.exposure.include", "*");
    config.save();
}

All this actions also need to perform in main method before application run.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文