成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

面試突擊:SpringBoot 有幾種讀取配置文件的方法?

開發 前端
在 Spring Boot 中讀取配置文件有以下 5 種方法:使用 @Value 讀取配置文件;使用 @ConfigurationProperties 讀取配置文件;使用 @PropertySource 讀取配置文件; 使用 Environment 讀取配置文件;使用原生方式讀取配置文件。

Spring Boot 中讀取配置文件有以下 5 種方法:

  • 使用 @Value 讀取配置文件。
  • 使用 @ConfigurationProperties 讀取配置文件。
  • 使用 Environment 讀取配置文件。
  • 使用 @PropertySource 讀取配置文件。
  • 使用原生方式讀取配置文件。

它們的具體使用方法如下,為了方便測試,我們在 Spring Boot 配置文件 application.properties 添加以下內容:

profile.name=Spring Boot Profile
profile.desc=Spring Boot Profile Desc.

1.使用 @Value 讀取配置文件

使用 @Value 可以讀取單個配置項,如下代碼所示:

@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 是讀取單個配置項的,而 @ConfigurationProperties 是讀取一組配置項的,我們可以使用 @ConfigurationProperties 加實體類讀取一組配置項,如下代碼所示:

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "profile")
@Data
public class Profile {
private String name;
private String desc;
}

其中 prefix 表示讀取一組配置項的根 name,相當于 Java 中的類名,最后再把此配置類,注入到某一個類中就可以使用了,如下代碼所示:

@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 中的一個用于讀取配置文件的類,將此類使用 @Autowired 注入到類中就可以使用它的 getProperty 方法來獲取某個配置項的值了,如下代碼所示:

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;

@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 注解可以用來指定讀取某個配置文件,比如指定讀取 application.properties 配置文件的配置內容,具體實現代碼如下:

@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);
}
}

以上程序的執行結果如下圖所示:

圖片

中文亂碼

如果配置文件中出現中文亂碼的情況,可通過指定編碼格式的方式來解決中文亂碼的問題,具體實現如下:

@PropertySource(value = "dev.properties", encoding = "utf-8")

注意事項

@PropertySource 注解默認是只支持 properties 格式配置文件的讀取的。

5.使用原生方式讀取配置文件

我們還可以使用最原始的方式 Properties 對象來讀取配置文件,如下代碼所示:

import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

@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"));
}
}

以上程序的執行結果如下圖所示:

圖片

總結

在 Spring Boot 中讀取配置文件有以下 5 種方法:

  • 使用 @Value 讀取配置文件。
  • 使用 @ConfigurationProperties 讀取配置文件。
  • 使用 @PropertySource 讀取配置文件。
  • 使用 Environment 讀取配置文件。
  • 使用原生方式讀取配置文件。

其中最常用的是前 3 種,如果讀取某一個配置項可使用 @Value,如果讀取一組配置項可使用 @ConfigurationProperties,如果要指定讀取某一個具體的配置文件可使用 @PropertySource 來指定。

責任編輯:武曉燕 來源: Java面試真題解析
相關推薦

2022-04-18 07:36:37

TimeUnit線程休眠

2022-04-11 07:40:45

synchroniz靜態方法程序

2022-05-23 07:35:15

單例模式懶漢模式靜態內部類

2022-09-19 06:16:23

事務隔離級別Spring

2023-08-07 16:14:32

propertiesSpring框架

2022-03-07 07:33:16

線程池Java語言

2022-08-24 07:06:36

SpringSetter項目

2010-08-02 16:58:08

Flex配置文件

2022-05-11 07:41:55

死鎖運算線程

2022-05-05 07:38:32

volatilJava并發

2022-06-06 07:35:26

MySQLInnoDBMyISAM

2013-07-30 11:30:42

Windows PhoWindows Pho

2022-06-01 12:00:54

HTTP狀態碼服務端

2022-09-25 22:12:07

事務SpringBoot

2022-09-12 22:27:05

編程式事務聲明式事務對象

2009-08-13 09:58:55

C#讀取配置文件

2009-08-13 09:16:57

C#讀取配置文件

2022-08-22 07:06:32

MyBatisSQL占位符

2021-04-01 10:23:45

SpringBootbootstrapapplication

2023-01-13 16:57:50

SpringBoot配置核心
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品久久久久免费 | 色婷婷亚洲一区二区三区 | 国产精品一码二码三码在线 | 自拍 亚洲 欧美 老师 丝袜 | 久久精品国产一区老色匹 | 久久久久久看片 | 成人精品视频在线观看 | 罗宾被扒开腿做同人网站 | a级黄色片在线观看 | 午夜伦理影院 | 国产免费拔擦拔擦8x高清 | 日韩精品免费看 | 三极网站| 久色| 日日爱av | 中文字幕第100页 | 久久精品国产久精国产 | 免费在线观看成人 | 麻豆va| 91免费版在线观看 | 国产伦精品一区二区 | 欧美高清dvd | 久草福利 | 成人精品一区二区 | 黄色免费av | 欧美日韩亚洲国产 | 一区二区三区精品在线视频 | 国产精品a久久久久 | 免费国产黄 | 综合欧美亚洲 | 亚洲欧美v | www.五月天婷婷.com | 久久久久一区二区三区四区 | 成人免费视频观看 | 操久久 | 九九国产 | 精品欧美一区二区三区免费观看 | 久久精品亚洲欧美日韩精品中文字幕 | 欧美一区二区大片 | 国产精品久久久久久久久久久久冷 | 在线观看亚洲精品 |