Deployment Spring引导中特定于环境的外部化属性文件

Deployment Spring引导中特定于环境的外部化属性文件,deployment,spring-boot,environment-variables,configuration-management,Deployment,Spring Boot,Environment Variables,Configuration Management,我有一个Spring Boot应用程序,通过以下脚本(节选)作为jar运行: 配置属性文件(目前为2个:application.properties和submit enrollment.properties)位于应用程序根目录中的上述jar文件中,并且通过@PropertySource和@Value注释将这些文件中的属性值读入各种bean,例如: 提交注册。属性: marshaller.contextPaths=c.f.e.a_e.e_1, c.f.e.a._1 default.Uri=http

我有一个Spring Boot应用程序,通过以下脚本(节选)作为jar运行:

配置属性文件(目前为2个:application.propertiessubmit enrollment.properties)位于应用程序根目录中的上述jar文件中,并且通过@PropertySource和@Value注释将这些文件中的属性值读入各种bean,例如:

提交注册。属性:

marshaller.contextPaths=c.f.e.a_e.e_1, c.f.e.a._1
default.Uri=https://i.h.c.com/I1/AEService  
soap.action=http://wsdl.business.services.ee.ffe.c.h.com/AEPT/CAMIPR
auditLog.server=10.172.x.xx
auditLog.port=8062
spring.main.show-banner=false
server.port=9278
logging.level.org.springframework.ws.client.core = DEBUG
logging.level.com.sun.xml.internal.messaging = DEBUG
#logging.level.org.springframework.ws.client.MessageTracing = TRACE
#logging.level.org.springframework.ws.client.MessageTracing.received = TRACE
应用程序。属性:

marshaller.contextPaths=c.f.e.a_e.e_1, c.f.e.a._1
default.Uri=https://i.h.c.com/I1/AEService  
soap.action=http://wsdl.business.services.ee.ffe.c.h.com/AEPT/CAMIPR
auditLog.server=10.172.x.xx
auditLog.port=8062
spring.main.show-banner=false
server.port=9278
logging.level.org.springframework.ws.client.core = DEBUG
logging.level.com.sun.xml.internal.messaging = DEBUG
#logging.level.org.springframework.ws.client.MessageTracing = TRACE
#logging.level.org.springframework.ws.client.MessageTracing.received = TRACE
=================================

@Configuration
@ComponentScan({"c.f.e", "c.z.submit.enrollment"})
@PropertySource("classpath:/submit-enrollment.properties")
public class SubmitEnrollmentConfig {

    @Value("${marshaller.contextPaths}")
    private String[] marshallerContextPaths;

    @Value("${default.Uri}")
    private String defaultUri;
@Service("soapClient")
public class FmfSoapClient extends WebServiceGatewaySupport {

@Value("${soap.action}")
private String soapAction;


@Value("${auditLog.server}")
private String auditLogServer;

@Value("${auditLog.port}")
private String auditLogPort;    
==================================

@Configuration
@ComponentScan({"c.f.e", "c.z.submit.enrollment"})
@PropertySource("classpath:/submit-enrollment.properties")
public class SubmitEnrollmentConfig {

    @Value("${marshaller.contextPaths}")
    private String[] marshallerContextPaths;

    @Value("${default.Uri}")
    private String defaultUri;
@Service("soapClient")
public class FmfSoapClient extends WebServiceGatewaySupport {

@Value("${soap.action}")
private String soapAction;


@Value("${auditLog.server}")
private String auditLogServer;

@Value("${auditLog.port}")
private String auditLogPort;    
随着应用程序的增长,将添加其他.properties文件,可能与使用的spring组件类似,即security.properties、web-services.properties、web-properties等,而不是单个submit-registration.properties文件

这是当前的设置。我要找的是以下内容:

问题:

marshaller.contextPaths=c.f.e.a_e.e_1, c.f.e.a._1
default.Uri=https://i.h.c.com/I1/AEService  
soap.action=http://wsdl.business.services.ee.ffe.c.h.com/AEPT/CAMIPR
auditLog.server=10.172.x.xx
auditLog.port=8062
spring.main.show-banner=false
server.port=9278
logging.level.org.springframework.ws.client.core = DEBUG
logging.level.com.sun.xml.internal.messaging = DEBUG
#logging.level.org.springframework.ws.client.MessageTracing = TRACE
#logging.level.org.springframework.ws.client.MessageTracing.received = TRACE
我希望能够将.properties文件外部化,以驻留在jar文件外部的文件系统上,并以反映环境的方式命名它们:

application-dev.properties
submit-enrollment-dev.properties

我想在shell脚本中向Spring Boot传递一个参数,告诉我在哪个环境中运行它:

java -jar submit-enrollment.jar -environment=uat

并让Spring根据与上述参数对应的-env-值自动解析适当的属性文件。这是目前作为Spring引导功能的一部分存在的东西,如果是的话,请有人告诉我一些类似的示例设置。如果您认为这不是一种可行的方法,那么与其让Spring解析具有不同名称的.properties文件,不如将它们隔离到特定于环境的目录中,同时让它们在不同环境中具有相同的名称,我应该在Maven或Jenkins中使用它,而不是让Spring Boot在运行时使用它吗?有更好的解决方案吗?

问题是您想更改文件名,并让Spring Boot自动选择您指定的环境

弹簧靴支持开箱即用。因此,如果启用
uat
配置文件,它将在常用位置查找
应用程序uat.properties

如果不喜欢将
应用程序
作为前缀,可以指定
SPRING\u CONFIG\u NAME
system/os属性


如果这对您不起作用,您可以使用
SPRING\u CONFIG\u LOCATION
告诉SPRING Boot在哪里查找东西。然后可以列出目录或单个文件。最后一个属性为您提供了最大的灵活性,可能会满足您的要求,但您需要做更多的工作。

谢谢。如果我将classpath:/submit-enrollment.properties和application.properties中的所有配置属性合并到application.properties中,并在调用jar时使用--spring.config.location=./application.properties,那么行为似乎忽略了@PropertySource(“classpath:/submit-enrollment.properties”)全部注释。所以,若需要更细粒度的.properties文件,我只需要在“--spring.config.location=”标志所指向的目录中使用多个.properties文件,对吗?不确定要用
/
做什么。正如我已经说过的,问题是您想要更改文件名,并让Spring Boot自动选择您指定的环境。如果要在一个目录中加载多个文件,需要通过
spring.config.location
属性yes指定它们。Spring Boot不会扫描整个目录中的所有
.properties
文件。Stephane Nicoll,你能回答这个问题吗?