欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot詳解配置文件的用途與用法

 更新時間:2022年06月20日 10:02:25   作者:獨一無二的哈密瓜  
SpringBoot項目是一個標準的Maven項目,它的配置文件需要放在src/main/resources/下,其文件名必須為application,其存在兩種文件形式,分別是properties和yaml(或者yml)文件

1. SpringBoot 配置文件

1.1 配置文件的作用

配置文件中配置了項目中重要的數(shù)據(jù), 例如:

  • 數(shù)據(jù)庫的連接信息 (用戶名密碼)
  • 項目的啟動端口
  • 第三方系統(tǒng)的調(diào)用密鑰等信息
  • 用于發(fā)現(xiàn)和定位問題的普通日志和異常日志等

Spring Boot項目沒有配置信息, 就不能連接和操作數(shù)據(jù)庫, 甚至不能保存可以用于排查問題的關(guān)鍵日志. 所以配置文件非常重要.

1.2 配置文件的格式

在Spring Boot 中配置文件主要分為兩種:

  • .properties (主要是key=value格式)
  • .yml (主要是key: value格式)

注意:

  • 當項目中既有 .properties.yml , 且兩個配置文件中有相同的配置項, Spring Boot 會優(yōu)先考慮 .properties , 因為 .properties 的優(yōu)先級更高一些.
  • 一個項目中允許存在兩種不同的配置文件, .properties .yml, 但是在項目中建議只使用一種配置文件的格式.

1.3 properties 配置文件說明

1.3.1 properties 基本語法

properties 是以 key=value 這種格式配置的.

server.port=9090
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/2022-6-1
spring.datasource.username=root
spring.datasource.password=1234

配置文件的注釋信息使用 “#”

1.3.2 讀取配置文件

讀取配置文件的內(nèi)容, 可以使用 @Value 注解來實現(xiàn)

@Value 注解使用 "${}" 的格式讀取.

@Component
public class Read implements InitializingBean {
    @Value("${server.port}")
    private String port;
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println();
        System.out.println(port);
        System.out.println();
    }
}

1.4 yml 配置文件說明

yml 是 YMAL 是縮寫, 它的全稱是 Yet Another Markup Language, 譯為 另一種標記語言.

1.4.1 yml 基本語法

yml 是樹形結(jié)構(gòu)的配置文件, 它的基礎(chǔ)語法是 key: value, 這里的:后面跟著一個空格.

server:
  port: 9090
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/2022-6-1
    username: root
    password: 1234

1.4.2 yml 使用進階

# ~代表null
null.value: ~

查看一段代碼

string:
  str1: Hello \n World
  str2: 'Hello \n World'
  str3: "Hello \n World"

讀取yml中的這段代碼

@Component
public class Read1 implements InitializingBean {
    @Value("${string.str1}")
    private String str1;
    @Value("${string.str2}")
    private String str2;
    @Value("${string.str3}")
    private String str3;
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println();
        System.out.println("str1: "+str1);
        System.out.println("str2: "+str2);
        System.out.println("str3: "+str3);
        System.out.println();
    }
}

運行結(jié)果:

字符串加上雙引號, 會執(zhí)行\(zhòng)n 換行.

1.4.3 配置對象

yml 中配置對象

student:
  id: 1
  name: zhangsan
  age: 18

讀取配置的對象, 就需要用到另一個注解: @ConfigurationProperties

@Component
@ConfigurationProperties("student")
public class User {
    private int id;
    private String name;
    private int age;
	// 一堆getter setter
}

讀取

@Component
public class Read2 implements InitializingBean {
    @Autowired
    private Student student;
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println();
        System.out.println(student);
        System.out.println();
    }
}

1.4.4 配置集合

yml 中 配置集合

mylist:
  colors:
    - RED
    - GREEN
    - BLACK

讀取配置集合

@Component
@ConfigurationProperties("mylist")
public class MyList {
    private List<String> colors;
    // 一堆getter 和 setter
}

打印代碼

@Component
public class Read3 implements InitializingBean {
    @Autowired
    private MyList myList;
    @Override
    public void afterPropertiesSet() throws Exception {
        for (String val : myList.getColors()){
            System.out.println(val);
        }
    }
}

1.4.5 yml的另一種寫法(行內(nèi)寫法)

配置對象

student: {id: 1,name: zhangsan,age: 18}

配置集合

mylist: {colors: [RED,GREEN,BLACK]}

1.5 properties 和 yml 比較

properties 的語法更復(fù)雜, yml 語法更簡潔

yml通用性更好, 支持更多的語言, 如 Java, Go, Python等

yml支持更多的數(shù)據(jù)類型

yml格式的配置文件寫的時候容易出錯(在:之后有一個空格), 而properties寫法傳統(tǒng)比較復(fù)制,但不太容易出錯

2. 讀取 SpringBoot 配置文件的方法

2.1 使用 @Value 讀取配置文件

只能讀取一個

@Component
public class Read implements InitializingBean {
    @Value("${server.port}")
    private String port;
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println();
        System.out.println(port);
        System.out.println();
    }
}

2.2 使用@ConfigurationProperties

直接在類上寫

@Component
@ConfigurationProperties("mylist")
public class MyList {
    private List<String> colors;
    public List<String> getColors() {
        return colors;
    }
    public void setColors(List<String> colors) {
        this.colors = colors;
    }
}

2.3 @PropertySource讀取指定配置文件

jdbc.username=root2
jdbc.password=root1

@Component
@PropertySource(value = {"classpath:application.properties"})
public class JDBC implements InitializingBean {
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println(username + " " + password);
    }
}

到此這篇關(guān)于Spring Boot詳解配置文件的用途與用法的文章就介紹到這了,更多相關(guān)Spring Boot配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論