Spring Boot中讀取配置文件有以下五種方式:
- 使用 @Value 讀取配置文件
- 使用 @ConfigurationProperties 讀取配置文件
- 使用 Environment 讀取配置文件
- 使用 @PropertySource 讀取配置文件
- 使用原生方式讀取配置文件
它們的具體使用方法如下磕昼,為了方便測(cè)試千所,我們?cè)?Spring Boot 配置文件 application.properties 添加以下內(nèi)容:
profile.name=Spring
profile.desc=Spring Desc.
1. @Value
使用 @Value 可以讀取單個(gè)配置項(xiàng)咏花,如下代碼所示:
@SpringBootApplication
public class DemoApplication implements InitializingBean {
@Value("${profile.name}")
private String name;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("My Profile Name:" + name);
}
}
2. @ConfigurationProperties
@ConfigurationProperties 和 @Value 的使用略微不同亚铁,@Value 是讀取單個(gè)配置項(xiàng)的,而 @ConfigurationProperties 是讀取一組配置項(xiàng)的纽乱,我們可以使用 @ConfigurationProperties 加實(shí)體類讀取一組配置項(xiàng)间雀,如下代碼所示:
@Component
@ConfigurationProperties(prefix = "profile")
@Data
public class Profile {
private String name;
private String desc;
}
其中 prefix 表示讀取一組配置項(xiàng)的根 name群发,相當(dāng)于 Java 中的類名,最后再把此配置類链瓦,注入到某一個(gè)類中就可以使用了拆魏,如下代碼所示:
@SpringBootApplication
public class DemoApplication implements InitializingBean {
@Autowired
private Profile profile;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Profile Object:" + profile);
}
}
3. 使用 Environment 讀取配置文件
Environment 是 Spring Core 中的一個(gè)用于讀取配置文件的類,將此類使用 @Autowired 注入到類中就可以使用它的 getProperty 方法來獲取某個(gè)配置項(xiàng)的值了慈俯,如下代碼所示:
@SpringBootApplication
public class DemoApplication implements InitializingBean {
@Autowired
private Environment environment;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Profile Name:" + environment.getProperty("profile.name"));
}
}
4. 使用 @PropertySource 讀取配置文件
使用 @PropertySource 注解可以用來指定讀取某個(gè)配置文件渤刃,比如指定讀取 application.properties 配置文件的配置內(nèi)容,具體實(shí)現(xiàn)代碼如下:
@SpringBootApplication
@PropertySource("classpath:application.properties")
public class DemoApplication implements InitializingBean {
@Value("${profile.name}")
private String name;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Name:" + name);
}
}
注意點(diǎn):
- 中文亂碼
如果配置文件中出現(xiàn)中文亂碼的情況贴膘,可通過指定編碼格式的方式來解決中文亂碼的問題卖子,具體實(shí)現(xiàn)如下:
@PropertySource(value = "dev.properties", encoding = "utf-8")
- 注意事宜
@PropertySource 注解默認(rèn)是只支持 properties 格式配置文件的讀取的
5. 使用原生方式讀取配置文件
我們還可以使用最原始的方式 Properties 對(duì)象來讀取配置文件,如下代碼所示:
@SpringBootApplication
public class DemoApplication implements InitializingBean {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
Properties props = new Properties();
try {
InputStreamReader inputStreamReader = new InputStreamReader(
this.getClass().getClassLoader().getResourceAsStream("application.properties"),
StandardCharsets.UTF_8);
props.load(inputStreamReader);
} catch (IOException e1) {
System.out.println(e1);
}
System.out.println("Properties Name:" + props.getProperty("profile.name"));
}
}
總結(jié)
其中最常用的是前 3 種刑峡,如果讀取某一個(gè)配置項(xiàng)可使用 @Value洋闽,如果讀取一組配置項(xiàng)可使用 @ConfigurationProperties,如果要指定讀取某一個(gè)具體的配置文件可使用 @PropertySource 來指定