SpringBoot读取十二种配置文件的姿势
2023-12-09 16:01:13
一、配置文件读取方式
SpringBoot支持多种配置文件的读取方式,包括:
1. application.properties :默认的配置文件,位于项目根目录或src/main/resources目录下。
2. application.yml :YAML格式的配置文件,与application.properties功能相同,但更加简洁。
3. 环境变量 :操作系统中的环境变量,可以通过System.getenv()方法获取。
4. 系统属性 :Java虚拟机启动时传入的系统属性,可以通过System.getProperties()方法获取。
5. 命令行参数 :在启动SpringBoot应用程序时传入的命令行参数,可以通过args参数获取。
6. JNDI :Java命名和目录接口,可以通过JndiTemplate类获取。
7. ServletContextInitParams :Web应用程序的初始化参数,可以通过ServletContext.getInitParameter()方法获取。
8. profiles :profiles是Spring Boot中的一种配置隔离机制,可以通过在配置文件中指定profile来激活或禁用某些配置。
9. bootstrap.properties :bootstrap.properties文件用于加载引导程序的配置,该文件位于项目根目录或src/main/resources目录下,在应用上下文中它的优先级高于application.properties和application.yml。
10. bootstrap.yml :bootstrap.yml文件是bootstrap.properties的YAML格式,该文件位于项目根目录或src/main/resources目录下,在应用上下文中它的优先级高于application.properties和application.yml。
11. configData :configData是Spring Cloud Config Server中的配置数据,可以通过ConfigClient类获取。
12. 自定义配置文件 :除了上述内置的配置文件读取方式外,SpringBoot还支持自定义配置文件的读取方式,只需要实现一个ConfigFileApplicationListener接口即可。
二、配置文件的加载顺序
SpringBoot的配置文件加载顺序如下:
- bootstrap.properties 或 bootstrap.yml
- application.properties 或 application.yml
- Environment variables
- System properties
- Command-line args
- JNDI
- ServletContextInitParams
- Profiles
- configData
- 自定义配置文件
三、配置文件的优先级
SpringBoot的配置文件优先级如下:
- bootstrap.properties 或 bootstrap.yml
- application.properties 或 application.yml
- Environment variables
- System properties
- Command-line args
- JNDI
- ServletContextInitParams
- Profiles
- configData
- 自定义配置文件
四、使用示例
以下是一个使用application.properties配置文件的示例:
@Configuration
@PropertySource("classpath:application.properties")
public class MyConfiguration {
@Value("${my.property}")
private String myProperty;
public String getMyProperty() {
return myProperty;
}
}
以下是一个使用环境变量的示例:
@Configuration
public class MyConfiguration {
@Value("${MY_PROPERTY}")
private String myProperty;
public String getMyProperty() {
return myProperty;
}
}
以下是一个使用系统属性的示例:
@Configuration
public class MyConfiguration {
@Value("${java.home}")
private String javaHome;
public String getJavaHome() {
return javaHome;
}
}
以下是一个使用命令行参数的示例:
@Configuration
public class MyConfiguration {
public static void main(String[] args) {
SpringApplication.run(MyConfiguration.class, args);
}
@Value("${my.property}")
private String myProperty;
public String getMyProperty() {
return myProperty;
}
}
以下是一个使用JNDI的示例:
@Configuration
public class MyConfiguration {
@JndiTemplate
private JndiTemplate jndiTemplate;
public String getMyProperty() {
return (String) jndiTemplate.lookup("java:comp/env/my.property");
}
}
以下是一个使用ServletContextInitParams的示例:
@Configuration
public class MyConfiguration {
@ServletContext("my.property")
private String myProperty;
public String getMyProperty() {
return myProperty;
}
}
以下是一个使用profiles的示例:
@Configuration
@Profile("dev")
public class MyConfiguration {
@Value("${my.property}")
private String myProperty;
public String getMyProperty() {
return myProperty;
}
}
以下是一个使用configData的示例:
@Configuration
@RefreshScope
public class MyConfiguration {
@Value("${my.property}")
private String myProperty;
public String getMyProperty() {
return myProperty;
}
}
以下是一个使用自定义配置文件的示例:
@Configuration
public class MyConfiguration implements ConfigFileApplicationListener {
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
List<PropertySource<?>> propertySources = event.getEnvironment().getPropertySources();
for (PropertySource<?> propertySource : propertySources) {
if (propertySource instanceof FilePropertySource) {
FilePropertySource filePropertySource = (FilePropertySource) propertySource;
if (filePropertySource.getName().equals("my.properties")) {
System.out.println("Found my.properties file");
}
}
}
}
}
五、总结
SpringBoot支持多种配置文件的读取方式,包括application.properties、application.yml、环境变量、系统属性、命令行参数、JNDI、ServletContextInitParams、profiles、configData和自定义配置文件。每种读取方式都有自己的优缺点,适合不同的场景。用户可以根据自己的需要选择合适的读取方式。