您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何使用application.properties文件上的动态端点打包springboot应用程序

如何使用application.properties文件上的动态端点打包springboot应用程序

是的,绝对有可能。基本上,您需要按需更改属性值而无需更改jar / war

将spring boot应用程序打包为jar并将外部application.properties文件放在任何位置,并与命令行参数传递相同的位置如下 :

 java -jar app.jar --spring.config.location=file:<property_file_location>

这将获取外部属性

1.如下扩展SpringBootServletInitializer

@SpringBootApplication
class DemoApp extends SpringBootServletInitializer {
    private ServletContext servletContext;
    public static void main(String[] args){SpringApplication.run(DemoApp.class,args);}
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        builder = builder.properties("test.property:${test_property:defaultValue}");
        return builder.sources(DemoApp.class)
   }
   @Override
   public void onStartup(ServletContext servletContext) throws ServletException {
       this.servletContext = servletContext;
       super.onStartup(servletContext);
   }
}

@Value(“ $ {test.property}”)

另外:

如果您想提供完整的文件作为外部文件,也可以传递如下所示的属性

.properties("spring.config.location:${config:null}")

有关外部化配置的进一步阅读:https ://docs.spring.io/spring-boot/docs/current/reference/html/boot-features- external-config.html

Java 2022/1/1 18:16:08 有484人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶