【发布时间】:2016-10-08 14:16:43
【问题描述】:
如果未设置 -Dspring.profiles.active,我希望我的默认活动配置文件为 production。
我在application.properties 中尝试了以下操作,但没有成功:
spring.profiles.default=production
Spring-boot 版本 = 1.3.5.RELEASE
【问题讨论】:
标签: java spring spring-boot
如果未设置 -Dspring.profiles.active,我希望我的默认活动配置文件为 production。
我在application.properties 中尝试了以下操作,但没有成功:
spring.profiles.default=production
Spring-boot 版本 = 1.3.5.RELEASE
【问题讨论】:
标签: java spring spring-boot
您在此处所做的是将默认 default 配置文件(如果您未指定 @Profile 注释,则在任何 bean 上使用的配置文件)设置为 production。
您真正需要做的是设置默认的active配置文件,如下所示:
spring.profiles.active=production
【讨论】:
-Dspring.profiles.active=development 覆盖它,这就是我想要的
添加--spring.profiles.active=production
例子:
java -jar file.jar --spring.profiles.active=production
【讨论】:
java -jar file.jar -Dspring.profiles.active=production,但这对我不起作用。
我们在java中设置spring.profiles.active时遇到了类似的问题。
这是我们在尝试了四种不同的提供spring.profiles.active的方式后最终得出的结论。
在java-8
$ java --spring.profiles.active=dev -jar my-service.jar
Gives unrecognized --spring.profiles.active option.
$ java -jar my-service.jar --spring.profiles.active=dev
# This works fine
$ java -Dspring.profiles.active=dev -jar my-service.jar
# This works fine
$ java -jar my-service.jar -Dspring.profiles.active=dev
# This doesn't works
在java-11
$ java --spring.profiles.active=dev -jar my-service.jar
Gives unrecognized --spring.profiles.active option.
$ java -jar my-service.jar --spring.profiles.active=dev
# This doesn't works
$ java -Dspring.profiles.active=dev -jar my-service.jar
# This works fine
$ java -jar my-service.jar -Dspring.profiles.active=dev
# This doesn't works
注意:如果您在 application.properties 文件中指定 spring.profiles.active,请确保如上所述向 java 提供 spring.config.location 或 spring.config.additional-location 选项。
【讨论】:
如果您使用的是 maven,我会这样做:
成为生产您的默认个人资料:
<properties>
<activeProfile>production</activeProfile>
</properties>
作为其他配置文件的示例:
<profiles>
<!--Your default profile... selected if none specified-->
<profile>
<id>production</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<activeProfile>production</activeProfile>
</properties>
</profile>
<!--Profile 2-->
<profile>
<id>development</id>
<properties>
<activeProfile>development</activeProfile>
</properties>
</profile>
<!--Profile 3-->
<profile>
<id>otherprofile</id>
<properties>
<activeProfile>otherprofile</activeProfile>
</properties>
</profile>
<profiles>
您必须在 application.properties 中设置:
spring.profiles.active=@activeProfile@
这对我每次都有效,希望它能解决你的问题。
【讨论】:
maven package -P{profilename} - 例如maven package -Pproduction 用于基于生产的配置文件。
我是这样做的
System.setProperty("spring.profiles.default", "dev");
在main(...)的开头
【讨论】:
org.springframework.core.env.AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME 而不是它的值。
首先,通过下面的解决方案,有必要了解spring boot总是会读取application.properties文件。所以对方的配置文件只会补充和替换之前定义的属性。
考虑以下文件:
application.properties
application-qa.properties
application-prod.properties
1) 非常重要。 application.properties 和只是这个文件,必须有以下行:
spring.profiles.active=@spring.profiles.active@
2) 在 QA 和 PROD 配置文件中更改您想要的内容,以查看环境之间的差异。
3) 通过命令行,使用以下任何选项启动 spring boot 应用程序:
它将使用默认的application.properties 文件启动应用程序:
mvn spring-boot:run
它将加载默认的application.properties 文件和application-qa.properties 文件之后,替换和/或补充默认配置:
mvn spring-boot:run -Dspring.profiles.active=qa
此处相同,但使用的是生产环境而不是 QA:
mvn spring-boot:run -Dspring.profiles.active=prod
【讨论】:
把这个放到App.java中:
public static void main(String[] args) throws UnknownHostException {
SpringApplication app = new SpringApplication(App.class);
SimpleCommandLinePropertySource source = new SimpleCommandLinePropertySource(args);
if (!source.containsProperty("spring.profiles.active") &&
!System.getenv().containsKey("SPRING_PROFILES_ACTIVE")) {
app.setAdditionalProfiles("production");
}
...
}
JHipster 是这样处理的
【讨论】:
如果您将 AWS Lambda 与 SprintBoot 一起使用,则必须在环境变量下声明以下内容:
键:JAVA_TOOL_OPTIONS & 值:-Dspring.profiles.active=dev
【讨论】:
目前使用 Maven + Spring Boot。我们的解决方案如下:
application.properties
#spring.profiles.active= # comment_out or remove
securityConfig.java
@Value(${spring.profiles.active:[default_profile_name]}")
private String ACTIVE_PROFILE_NAME;
信用以MartinMlima 开头。这里提供了类似的答案:
How do you get current active/default Environment profile programmatically in Spring?
【讨论】:
@Profile 注释中还可以有多个列表
@Profile({"dev","default"})
如果将“default”设置为附加值,则不必指定 spring.profiles.active
【讨论】:
无需每次更改源代码的简洁方法是使用操作系统环境变量SPRING_PROFILES_ACTIVE:
export SPRING_PROFILES_ACTIVE=production
【讨论】:
如果正在创建 Spring Boot 应用程序,则可以根据环境拥有单独的应用程序属性文件。 例如 - 开发环境的属性文件, application-dev.properties:
spring.hivedatasource.url=<hive dev data source url>
spring.hivedatasource.username=dev
spring.hivedatasource.password=dev
spring.hivedatasource.driver-class-name=org.apache.hive.jdbc.HiveDriver
application-test.properties:
spring.hivedatasource.url=<hive dev data source url>
spring.hivedatasource.username=test
spring.hivedatasource.password=test
spring.hivedatasource.driver-class-name=org.apache.hive.jdbc.HiveDriver
还有一个主要的 application.properties 文件来选择配置文件:
application.properties:
spring.profiles.active=dev
server.tomcat.max-threads = 10
spring.application.name=sampleApp
如下定义数据库配置:
@Configuration
@ConfigurationProperties(prefix="spring.hivedatasource")
public class DBConfig {
@Profile("dev")
@Qualifier("hivedatasource")
@Primary
@Bean
public DataSource devHiveDataSource() {
System.out.println("DataSource bean created for Dev");
return new BasicDataSource();
}
@Profile("test")
@Qualifier("hivedatasource")
@Primary
@Bean
public DataSource testHiveDataSource() {
System.out.println("DataSource bean created for Test");
return new BasicDataSource();
}
这将根据 application.properties 文件中设置的活动配置文件自动创建 BasicDataSource bean。 运行 Spring-boot 应用程序并进行测试。
请注意,这将在调用 getConnection() 之前创建一个空 bean。 一旦连接可用,您就可以使用该 DataSource bean 获取 url、驱动程序类等。
【讨论】:
@Profile 的对象,它就会获得与活动配置文件对应的属性。
试试这个:
@PropertySource("classpath:${spring.profiles.active:production}_file.properties")
【讨论】:
如果您在配置中使用 application.yml,请将其添加到其中,以设置默认的活动配置文件:
spring:
profiles:
active: production
【讨论】: