1. 程式人生 > >六、spring boot 2.x 配置繫結

六、spring boot 2.x 配置繫結

1、在application.properties檔案中填加自己的定義的配置資訊,如

#自定義配置
self.user.name=ldy
self.user.age=27
self.user.phones[0]=139000000000
self.user.phones[1]=138000000000
self.user.phones[2]=135000000000
#上面的多個電話資訊也可以用逗號分隔的形勢
#self.user.phones=139000000000,138000000000,135000000000

2、讀取配置資訊

(1)獲取單個配置資訊

        可以通過@Value註解,繫結單個引數,如:

@Value(value = "${self.user.name}") 
private String name;

        通過以上方法獲取單個引數的值,不用寫get個set方法

(2)將配置資訊注入到對應的實體類

        編寫要繫結的實體:UserProperties.java實體類

package com.ldy.bootv2.demo.entity;

import java.util.List;

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

@Component
@ConfigurationProperties(prefix="self.user")
public class UserProperties {

    private String name;
    private int age;
    private List<String> phones;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

	public List<String> getPhones() {
		return phones;
	}

	public void setPhones(List<String> phones) {
		this.phones = phones;
	}
    
}

3、編寫測試介面,獲取自定義配置資訊

package com.ldy.bootv2.demo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ldy.bootv2.demo.entity.UserProperties;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(tags = "測試介面-API")
public class HelloController {
	
	private static Logger logger = LoggerFactory.getLogger(HelloController.class);
	
	@Value(value = "${self.user.name}") 
	private String name;
	
	@Autowired
	private UserProperties userProperties;

	@GetMapping("/hello")
	@ApiOperation("hello的測試介面")
	@ApiImplicitParam(name = "name", value = "名稱", required = true, dataType = "String")
	public String index(@RequestParam(required = true) final String name) {
		logger.info("您呼叫了hello 介面");
		return "hello " + name;
	}

	@PostMapping("/sum")
	@ApiOperation("兩整數求和介面")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "a", value = "引數a", required = true, dataType = "int"),
			@ApiImplicitParam(name = "b", value = "引數b", required = true, dataType = "int") })
	public String sum(@RequestParam(required = true) final Integer a, @RequestParam(required = true) final Integer b) {
		logger.info("您呼叫了sum 介面");
		int sum = a + b;
		return "a + b = " + sum;
	}
	
	@GetMapping("/getName")
	@ApiOperation("獲取配置檔案中的使用者名稱稱")
	public String getName() {
		return "hello " + name;
	}
	
	@GetMapping("/getUser")
	@ApiOperation("獲取配置檔案中的使用者完整資訊")
	public UserProperties getUser() {
		return userProperties;
	}

}

4、執行專案,通過swagger頁面,呼叫測試介面,測試執行情況

    

(1)獲取單個配置資訊結果如圖:

(2)獲取使用者全部配置資訊結果如圖: