Spring Boot中不能够使用 @Profile 注解进行多环境配置
时间: 2023-11-22 21:53:22 浏览: 54
这种说法是不正确的。Spring Boot中可以使用@Profile注解进行多环境配置,通过在application.properties或者application.yml文件中设置不同的配置文件,Spring Boot可以根据不同的@Profile值加载不同的配置文件。例如:
```
# application-dev.properties
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=123456
# application-prod.properties
spring.datasource.url=jdbc:mysql://prod-server:3306/mydb
spring.datasource.username=prod-user
spring.datasource.password=prod-password
```
然后在启动类或者配置类上加上@Profile注解,指定不同的环境:
```
@Configuration
@Profile("dev")
public class DevConfig {
// dev环境下的配置
}
@Configuration
@Profile("prod")
public class ProdConfig {
// prod环境下的配置
}
```
这样,在启动应用时,可以通过指定不同的Spring profile来加载不同的配置文件,如:
```
java -jar myapp.jar --spring.profiles.active=dev
```
或者在application.properties/yml中设置:
```
spring.profiles.active=dev
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)