二:SpringBoot的相關配置介紹

SpringBoot雖然去掉了 XML 但未做到零配置,它體現出了一種約定優於配置,是一種軟件設計範式,旨在減少軟件開發人員做決定的數量,而又不失靈活性一般情況下默認的配置足夠滿足日常開發所需,但在特殊的情況下,我們往往需要用到自定義屬性配置、自定義文件配置、多環境配置 等一系列功能。

 1. 自定義屬性配置

 1.1. application.properties 配置文件

my.age=23
my.name=wzp

1.2. 定義Properties.java文件,用來映射我們在 application.properties 中的內容,我們就可以通過操作對象的方式來獲得配置文件的內容

package com.wzp.demo;

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

@Component
@ConfigurationProperties(prefix = "application")
public class Properties {

    private int age;
    private String name;
    // 省略 get set 方法

    @Override
    public String toString() {
        return "Properties{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

1.3. 定義 PropertiesController 用來注入 Properties 測試我們編寫的代碼,Spring4.x以後,推薦使用構造函數的形式注入屬性…

package com.wzp.demo;

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

@RestController (這個註解在上一篇已經說過了,就不再解釋啦)
@RequestMapping("/properties")
public class PropertiesController {
    private static final Logger log = LoggerFactory.getLogger(PropertiesController.class);

    private final Properties properties;

    @Autowired
    public PropertiesController(Properties properties) {
        this.properties = properties;
    }

    @GetMapping("/propertie")
    public Properties myProperties1() {
        log.info("========================================");
        log.info(properties.toString());
        log.info("========================================");
        return properties;
    }
}

1.4. 打開瀏覽器,輸入如下地址:http://localhost:8080/properties/propertie,觀察控制檯,如果出現如下內容,那麼恭喜你——成功啦!!!

2019-01-08 10:25:57.215  INFO 8264 --- [nio-8080-exec-3] com.wzp.demo.PropertiesController        : ========================================
2019-01-08 10:25:57.215  INFO 8264 --- [nio-8080-exec-3] com.wzp.demo.PropertiesController        : Properties{age=0, name='null'}
2019-01-08 10:25:57.215  INFO 8264 --- [nio-8080-exec-3] com.wzp.demo.PropertiesController        : ========================================

2. 配置文件的其它命名

2.1 將application.properties改爲application.yml文件,則內容書寫格式如下

my:
 name: wzp
 age: 23

2.2. 讀取配置文件的相關信息也可在controller裏操作

package com.wzp.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/properties")
@RestController
public class PropertiesController {
    private static final Logger log = LoggerFactory.getLogger(PropertiesController.class);

    @Value("${my.name}")
    private String name;
    @Value("${my.age}")
    private int age;


    @RequestMapping(value = "/propertie")
    public String propertie(){
        return name+":"+age;
    }
}

2.3. 打開瀏覽器,訪問 http://localhost:8080/properties/propertie 出現如下結果 則成功讀取配置文件信息

3. 多環境化配置

3.1. 在真實的應用中,常常會有多個環境(如:開發,測試,生產等),不同的環境數據庫連接都不一樣,這個時候就需要用到 spring.profile.active 的強大功能了,它的格式爲application-{profile}.properties,這裏的application爲前綴不能改,{profile}是我們自己定義的。分別刻意創建以下多種配置滿足不同的需求

3.1.1. application-dev.properties 開發環境

server.servlet.context-path=/dev

3.1.2. application-prod.properties 生產環境

server.servlet.context-path=/prod

3.1.3. application-test.properties 測試環境

server.servlet.context-path=/test

3.2. 在 application.properties 配置文件中寫入spring.profiles.active=dev,這個時候我們在次訪問 http://localhost:8080/properties/propertie 就沒用了,新的路徑就是 http://localhost:8080/dev/properties/propertie  讀取的配置文件也是 application-dev.properties 裏面的內容

4. 結語

嗯...看了很多大佬的教程,結合我自己的總結了一波,當然了,不足之處請多包涵,也請多指教...如有雷同,也請多包涵...嘻嘻...

注:如有需要,可自行轉載,但是要加上原創作者及原文章鏈接哦...

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章