--spring.profiles.active=prod 读取外部配置文件
时间: 2024-12-18 07:28:31 浏览: 66
`--spring.profiles.active=prod` 是 Spring Boot 中用于指定激活哪个配置文件的参数。在 Spring Boot 应用中,可以通过不同的配置文件来管理不同环境下的配置信息,例如开发环境、测试环境和生产环境等。
通过设置 `--spring.profiles.active=prod`,Spring Boot 会加载名为 `application-prod.properties` 或 `application-prod.yml` 的配置文件。这些文件通常包含特定于生产环境的配置,如数据库连接信息、日志级别、外部服务地址等。
### 示例
假设你有以下两个配置文件:
1. `application.properties`(默认配置)
2. `application-prod.properties`(生产环境配置)
#### application.properties
```properties
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/dev_db
spring.datasource.username=dev_user
spring.datasource.password=dev_pass
```
#### application-prod.properties
```properties
server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/prod_db
spring.datasource.username=prod_user
spring.datasource.password=prod_pass
```
当你启动 Spring Boot 应用时,使用以下命令:
```bash
java -jar myapp.jar --spring.profiles.active=prod
```
此时,Spring Boot 会加载 `application-prod.properties` 中的配置,而不是 `application.properties` 中的默认配置。
### 相关问题
1. 如何在 Spring Boot 中定义和使用多个配置文件?
2. 如何在不同环境中切换 Spring Boot 的配置文件?
3. 为什么需要使用 Spring Profiles 来管理不同环境的配置?
阅读全文
相关推荐


















