Springboot指定配置文件位置
时间: 2024-03-03 11:26:12 浏览: 352
在Spring Boot项目中,我们可以通过指定不同的配置文件来控制应用程序的行为。以下是指定配置文件位置的几种方法:
1. 在命令行中指定配置文件位置,例如:
```
java -jar myproject.jar --spring.config.location=classpath:/application.properties,classpath:/other.properties
```
2. 在application.properties或application.yml文件中指定配置文件位置,例如:
```
spring.config.location=classpath:/application.properties,classpath:/other.properties
```
3. 在代码中使用@PropertySource注解指定配置文件位置,例如:
```
@Configuration
@PropertySource("classpath:/other.properties")
public class MyConfig {
// ...
}
```
4. 在启动类中使用SpringApplication.setDefaultProperties()方法指定配置文件位置,例如:
```
@SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MyApp.class);
app.setDefaultProperties(Collections.singletonMap("spring.config.location", "classpath:/other.properties"));
app.run(args);
}
}
```
以上是几种常见的指定配置文件位置的方法,你可以根据自己的需求选择其中一种。
阅读全文
相关推荐

















