ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# 69.6 设置生效的Spring profiles Spring `Environment`有一个API可以设置生效的profiles,但通常你会通过系统属性(`spring.profiles.active`)或OS环境变量(`SPRING_PROFILES_ACTIVE`)设置。比如,使用一个`-D`参数启动应用程序(记着把它放到`main`类或jar文件之前): ```text $ java -jar -Dspring.profiles.active=production demo-0.0.1-SNAPSHOT.jar ``` 在Spring Boot中,你也可以在`application.properties`里设置生效的profile,例如: ```java spring.profiles.active=production ``` 通过这种方式设置的值会被系统属性或环境变量替换,但不会被`SpringApplicationBuilder.profiles()`方法替换。因此,后面的Java API可用来在不改变默认设置的情况下增加profiles。 想要获取更多信息可查看'Spring Boot特性'章节的[Chapter 25, Profiles](https://github.com/cwiki-us-spring-guides/Spring-Boot-Reference-Guide/tree/0047aa8098a650dde0c93f4d2e91754c83468c4b/IV.%20Spring%20Boot%20features/25.%20Profiles.md)。