【问题标题】:how to package the springboot application with dynamic end points on application.properties file如何在 application.properties 文件中使用动态端点打包 springboot 应用程序
【发布时间】:2018-11-14 09:43:37
【问题描述】:

我有 1 个弹簧启动应用程序。在此应用程序中,我已将 1 个电子商务系统配置为弹性路径(在 application.properties 文件中配置弹性路径的端点 url)。现在我必须将我的 Spring Boot 应用程序提供给其他一些人。它将部署在tomcat服务器上。我不想给出源代码。所以我可以制作战争文件,但现在的问题是他们有自己的弹性路径电子商务,他们想要配置自己的电子商务。

我想外部化一些将覆盖现有属性的属性。

我的 springboot 应用程序有 2 个模块: 1) 具有 elasticpath-application.properties 的弹性路径模块 2)salesforce-salesforce-application.properties

现在我必须外部化“C:\apache-tomcat-8.5.29\conf\ep-external.properties”文件,该文件将覆盖现有属性。现在的问题是 @PropertySource 正在加载到最后一个位置。所以我的外部文件无法覆盖该属性。

@SpringBootApplication
@PropertySource(value = {"classpath:application.properties", "classpath:elasticpath-application.properties", "classpath:salesforce-application.properties")
public class SpringBootDemo extends SpringBootServletInitializer implements CommandLineRunner {
private static final Logger LOG = LoggerFactory.getLogger(SpringBootDemo.class);
    private ServletContext servletContext;

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        //application = application.properties("file:C:\\apache-tomcat-8.5.29\\conf\\ep-external.properties");
        return application.sources(SpringBootDemo.class);
    }

    @Override
       public void onStartup(ServletContext servletContext) throws ServletException {
           this.servletContext = servletContext;
           super.onStartup(servletContext);
       }

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemo.class, args);
    }   

    @Override
    public void run(String... args) throws Exception {
    }

}

【问题讨论】:

    标签: spring spring-boot


    【解决方案1】:

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

    为 jar 传递命令行参数
    将您的 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);
       }
    }
    
    1. 像往常一样访问该属性,如下所示:

      @Value("${test.property}")

    2. 在启动 tomcat 之前设置名为 test_property 的环境变量。就是这样

    另外:

    如果你想提供完整的文件作为外部文件,你也可以像下面这样传递属性。

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

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

    【讨论】:

    • 感谢 bittu 的快速回复。我们需要将它部署在 Tomcat 服务器上。所以我认为我们只能在 tomcat 服务器上部署 war 而不是 jar。
    • 啊,对。 SpringBootApplicationBuilder.addCommandLineProperties 将允许您这样做。配置环境变量(配置路径或任何属性)并在运行时读取相同然后通过上述方法设置相同应该会有所帮助。
    • 谢谢bittu。你能帮我举个例子吗?
    • 谢谢bittu。我更新了我的问题。请再读一遍。我的 springboot 应用程序有 2 个模块:1)具有 elasticpath-application.properties 的 elasticpath 模块 2)salesforce - salesforce-application.properties 现在我必须外部化“C:\apache-tomcat-8.5.29\conf\ep-external .properties”文件,它将覆盖现有属性。现在的问题是 @PropertySource 正在加载到最后一个位置。所以我的外部文件无法覆盖该属性。
    • elasticpath-application.properties 应替换为外部 ep-external.properties。这是你想要的吗?
    猜你喜欢
    • 2019-05-20
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    • 2020-04-28
    相关资源
    最近更新 更多