是的,绝对有可能。基本上,您需要按需更改属性值而无需更改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