springboot如何导入其他配置文件
时间: 2023-10-13 14:25:27 浏览: 32
Spring Boot可以使用`@PropertySource`注解来导入其他配置文件,示例代码如下:
1. 定义一个配置文件,比如jdbc.properties:
```
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/demo
jdbc.username=root
jdbc.password=
```
2. 在应用程序的启动类上使用`@PropertySource`注解来导入配置文件:
```
@SpringBootApplication
@PropertySource("classpath:/jdbc.properties")
public class MyApp {
// 程序入口
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
```
这样就可以在Spring Boot应用中使用`@Value`注解来注入这些属性值了,示例代码如下:
```
@Service
public class MyService {
@Value("${jdbc.driverClassName}")
private String driverClassName;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
// ...
}
```
这样就可以在用Spring Boot开发应用时方便地管理和注入各种配置信息。