con*_*ter 3 java spring spring-profiles spring-boot
这是我的application.yml文件:
spring:
freemarker:
template-loader-path: classpath:/templates
datasource:
url: jdbc:postgresql://localhost:5432/myapp
username: postgres
password: password
driver-class-name: org.postgresql.Driver
jpa:
show-sql: true
properties:
hibernate:
enable_lazy_load_no_trans: false
jdbc:
lob:
non_contextual_creation: true
dialect: org.hibernate.dialect.PostgreSQLDialect
hibernate:
ddl-auto: create-drop
---
spring:
profiles:
active: development
---
spring:
profiles: staging
jpa:
show-sql: true
hibernate:
ddl-auto: update
logging:
level:
root: DEBUG
---
spring:
profiles: production
jpa:
show-sql: false
hibernate:
ddl-auto: update
Run Code Online (Sandbox Code Playgroud)
我使用以下命令运行该应用程序:
java -jar application.jar -Dspring.profiles.active=staging
Run Code Online (Sandbox Code Playgroud)
在日志中,我可以看到spring boot打印出来: 下列配置文件处于活动状态:开发
那么,即使我在命令行args中显式设置了活动配置文件,为什么也不将其配置为暂存?
顺序很重要。要设置系统属性,请使用
java -jar -Dspring.profiles.active=staging application.jar
Run Code Online (Sandbox Code Playgroud)
您提到的行传递了一个应用程序参数。
启动 Java 应用程序。通过命令
java [ options ] -jar file.jar [ arguments ]
Run Code Online (Sandbox Code Playgroud)
Spring 配置文件 spring-docs
Spring 环境有一个用于此目的的 API,但您通常会设置系统属性 (spring.profiles.active) 或操作系统环境变量 (SPRING_PROFILES_ACTIVE)。另外,您可以使用 -D 参数启动应用程序(记住将其放在主类或 jar 存档之前),如下所示:
$ java -jar -Dspring.profiles.active=production demo-0.0.1-SNAPSHOT.jar
Run Code Online (Sandbox Code Playgroud)
在 Spring Boot 中,您还可以在 application.properties 中设置活动配置文件,如下例所示:
spring.profiles.active=production
Run Code Online (Sandbox Code Playgroud)
您可以使用 spring.profiles.active 环境属性来指定哪些配置文件处于活动状态,您还可以使用以下开关在命令行上指定它:spring-docs
$ java -jar demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
Run Code Online (Sandbox Code Playgroud)
对于多个配置文件
$ java -jar demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev,hsqldb
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3511 次 |
| 最近记录: |