如何使用application.properties文件上的动态端点打包springboot应用程序
我有一个spring启动应用程序。在这个应用程序中,我将1个电子商务系统配置为弹性路径(在application.properties文件中配置弹性路径的端点url)。现在我必须把我的spring boot应用程序交给其他人。它将部署在tomcat服务器上。我不想给出源代码。所以我可以制作war文件,但现在的问题是,他们有自己的弹性路径电子商务,他们想配置自己的电子商务 我想将一些属性外部化,这些属性将覆盖现有属性如何使用application.properties文件上的动态端点打包springboot应用程序,spring,spring-boot,Spring,Spring Boot,我有一个spring启动应用程序。在这个应用程序中,我将1个电子商务系统配置为弹性路径(在application.properties文件中配置弹性路径的端点url)。现在我必须把我的spring boot应用程序交给其他人。它将部署在tomcat服务器上。我不想给出源代码。所以我可以制作war文件,但现在的问题是,他们有自己的弹性路径电子商务,他们想配置自己的电子商务 我想将一些属性外部化,这些属性将覆盖现有属性 @SpringBootApplication @PropertySource(v
@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 {
}
}
我的springboot应用程序有两个模块:
1) 具有elasticpath-application.properties的elasticpath模块 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 {
}
}
是的,绝对可能。基本上,您需要的是在不更改jar/war的情况下根据需要更改属性值
为jar传递命令行参数
将spring boot应用程序打包为jar,将external application.properties文件放在任意位置,并将其传递到与命令行参数相同的位置,如下所示:
java -jar app.jar --spring.config.location=file:<property_file_location>
.properties("spring.config.location:${config:null}")
进一步阅读外部化配置:感谢bittu的快速回复。我们需要在tomcat服务器上部署它。所以我认为我们只能在tomcat服务器上部署war而不是jar。啊,对了。SpringBootApplicationBuilder.addCommandLineProperties将允许您这样做。配置env变量(配置路径或任何属性)并在运行时读取该变量,然后通过上述方法设置该变量应该会有所帮助。谢谢。你能帮我举个例子吗?谢谢你。我更新了我的问题。请再读一遍。我的springboot应用程序有两个模块:1)elasticpath模块有elasticpath-application.properties 2)salesforce-salesforce-application.properties现在我必须将“C:\apache-tomcat-8.5.29\conf\ep external.properties”文件外部化,该文件将覆盖现有属性。现在的问题是@PropertySource正在最后一个位置加载。因此,我的外部文件无法覆盖property.elasticpath-application.properties应替换为external ep-external.properties。这是你想要的吗?