天天看點

筆記-springboot-02-配置檔案說明

在springboot的配置中,有很多預設配置,是以使我們在程式中減少了很多配置,友善我們進行開發。但是也有一些配置需要自己進行配置的,所有需要記錄這些配置的簡單使用。現在springboot常用的有兩種配置類型,一種是properties檔案,一種是yml檔案,兩種方式看個人愛好習慣配置,不評價哪一種好壞!

先說properties檔案,這個做過javaWeb開發的人都熟悉!

建一個子產品,springboot-2-properties。

啟動類:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
 * Created by chenyuncong on 2018/5/21.
 */
@SpringBootApplication
public class ProApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProApplication.class, args);
    }
}
           

配置檔案:

# 設定端口号,預設的容器是tomcat,是以預設端口是8080,
server.port=9989

config.my.name=XiaoMingStudents
config.my.sex=male

config.my.hobby=games
config.my.address=Chinese Mainland
[email protected]
           
對象接收類:      
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
/**
 * Created by chenyuncong on 2018/5/21.
 */
@Component
//@EnableConfigurationProperties
@ConfigurationProperties(prefix = "config.my")
public class MyConfig {

    private String name;
    private String sex;
    private String hobby;
    private String address;
    private String email;
    //get set 構造 省略。。。
}
           

map接收:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
 * Created by chenyuncong on 2018/5/21.
 */
@Component
//@EnableConfigurationProperties
@ConfigurationProperties(prefix = "config")
public class MyConfigMap {
    private Map<String,String> my=new HashMap<>();
    public Map<String, String> getMy() {
        return my;
    }
    public void setMy(Map<String, String> my) {
        this.my = my;
    }
}
           

Controller類:

package com.cn.main.web;
import com.cn.main.config.MyConfig;
import com.cn.main.config.MyConfigMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
 * Created by chenyuncong on 2018/5/21.
 */
@RestController
@RequestMapping("test")
public class TestController {

    @Value("${config.my.name}")
    private String name;
    @Value("${config.my.sex}")
    private String sex;
    
    @Autowired
    private MyConfig myConfig;
    @Autowired
    private MyConfigMap myConfigMap;

    @RequestMapping("getInfo")
    public Map<String,Object> getInfo(){
        Map<String,Object> map=new HashMap<>();
        map.put("name",name);
        map.put("sex",sex);
        return map;
    }

    @RequestMapping("getMyConfig")
    public MyConfig getMyConfig(){
        return myConfig;
    }

    @RequestMapping("getMyConfigMap")
    public Map<String,String> getMyConfigMap(){
        Map<String,String> map=myConfigMap.getMy();
        return map;
    }

}
           

運作結果:

http://127.0.0.1:9989/test/getInfo 

筆記-springboot-02-配置檔案說明

http://127.0.0.1:9989/test/getMyConfig

筆記-springboot-02-配置檔案說明

http://127.0.0.1:9989/test/getMyConfigMap

筆記-springboot-02-配置檔案說明

下面說說yml檔案,套路基本一樣!

把properties檔案修改成yml檔案:

# 設定端口号,預設的容器是tomcat,是以預設端口是8080,
server:
  port: 9989
config:
  my:
    name: XiaoMingStudents
    sex: male
    hobby: games
    address: Chinese Mainland
    email: [email protected]
           

運作結果,和上面一樣!

繼續閱讀