1. 程式人生 > 實用技巧 >springboot application.yml配置學習

springboot application.yml配置學習

一、背景

為了更好的使用springboot,所以看一下application.yml配置這塊。主要是看資料繫結這塊。

主要參考:https://www.hangge.com/blog/cache/detail_2459.html

二、專案主要內容

1、controller

package com.shuimutong.learn.springboot.yml.controller;

import com.alibaba.fastjson.JSON;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController
public class HelloController { @GetMapping("/hello2")
public String hello2() {
return "Hello, Jack!";
}
}

2、啟動類

package com.shuimutong.learn.springboot.yml;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class YmlApplication {
public static void main(String[] args) {
SpringApplication.run(YmlApplication.class, args);
}
}

三、開始使用application.yml

1、從新建一個application.yml開始

在resources目錄下新建application.yml,並寫入以下內容,

server:
port: 8081

啟動服務,從日誌看出埠變更為8081,訪問url說明配置生效。

2、在application.yml中新增幾個屬性看看

1)application.yml增加內容

my:
name: Big大
age: 20
info: name:${my.name}--age:${my.age}

2)新建一個MyData類

package com.shuimutong.learn.springboot.yml.bean;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component
@Data
public class MyData {
@Value("${my.name}")
private String name;
@Value("${my.age}")
private int age;
@Value("${my.info}")
private String info;
}

使用@Value註解繫結application.yml中的配置

3)HelloController增加對應的使用

    @Resource
private MyData myData; @GetMapping("/getData")
public MyData getMyData() {
return myData;
}

4)啟動服務,並訪問對應的url

{"name":"Big大","age":20,"info":"name:Big大--age:20"}

說明值繫結正常。

3、單個屬性繫結太麻煩?試試物件繫結

1)application.yml增加內容

classroom:
clazz: 一年級
grade: 3班
seatNum: 30
courses:
- 語文
- 數學
- 英語
- 化學
- 體育
- 美術
students:
- name: 張三
age: 8
- name: 李四
age: 9

2)編寫對應的bean,並新增註解

//Classroom類
package com.shuimutong.learn.springboot.yml.bean; import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; import java.util.List; @Data
@Component
@ConfigurationProperties(prefix = "classroom")
public class Classroom {
private String clazz;
private String grade;
private int seatNum;
private List<String> courses;
private List<Student> students;
} //Student類
package com.shuimutong.learn.springboot.yml.bean;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; @Data
public class Student {
private String name;
private int age;
}

3)controller新增對應的方法

    @Resource
private Classroom classroom; @GetMapping("/classInfo")
public Classroom getClassroomInfo() {
System.out.println("this is stu:" + JSON.toJSONString(student));
return classroom;
}

4)啟動服務看看效果

{"clazz":"一年級","grade":"3班","seatNum":30,"courses":["語文","數學","英語","化學","體育","美術"],"students":[{"name":"張三","age":8},{"name":"李四","age":9}]}

符合預期。

4、配置太多,能不能拆成多個檔案?

可以的!

1)增加application-classroom.yml檔案

將內容從application.yml中抽出來。

2)修改application.yml

增加以下內容:

spring:
profiles:
active:
- classroom

配置那塊你沒看錯,yml檔名一定要以“application-”開頭。

3)啟動程式驗證

5、還有沒有其他招數?

有,請移步git:https://github.com/shuimutong/spring_learn/tree/master/spring_boot/yml