一、創建配置文件
如圖所示,我們在resources文件夾中新建配置文件application.yml

結構圖
二、一些基本配置
server:
port: 8090 //配置端口 session-timeout: 30 tomcat.max-threads: 0 tomcat.uri-encoding: UTF-8 spring: datasource: //數據庫配置 url : jdbc:mysql://localhost:3306/newbirds username : root password : mymysql driverClassName : com.mysql.jdbc.Driver
注意:key后面的冒號,后面一定要跟一個空格
三 、自定義的配置
1、在application.yml文件中我們自己定義了age 、name 、manInfo等參數,其中manInfo引用了age、name,引用的格式"${參數名}"
server:
//端口
port: 8081
age: 18
name: jason
manInfo: "age:${age},name:${name}"
怎么使用這些配置呢?我們創建GetManInfo文件(參照上面結構圖),
使用配置格式
@Value("${配置文件中的參數名}") 類型 參數名
詳細如下
package com.alun; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * Created by Administrator on 2017/5/28. */ @RestController public class GetManInfo { //獲取配置文件中的age @Value("${age}") private int age; //獲取配置文件中的name @Value("${name}") private String name; //獲取配置文件中的manInfo @Value("${manInfo}") private String manInfo; @RequestMapping(value = "/getAge",method= RequestMethod.GET) public int getAge(){ return age; } @RequestMapping(value = "/getName",method= RequestMethod.GET) public String getNme(){ return name; } @RequestMapping(value = "/getManInfo",method= RequestMethod.GET) public String getManInfo(){ return manInfo; } }
2、一個一個的@Value獲取覺得很煩,有辦法解決么?這個....當然有啊!
在application.yml我們改成這樣
server:
port: 8081
manInfo:
age: 18
name: jason
新建一個ManInfoProperties文件,(結構參照結構圖)使用
@Component
@ConfigurationProperties( prefix = "配置文件里的參數名" )
package com.alun; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * Created by Administrator on 2017/5/28. */ @Component @ConfigurationProperties( prefix = "manInfo" ) public class ManInfoProperties { private String age; private String name; public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
在GetManInfo里 使用 @Autowired
package com.alun; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * Created by Administrator on 2017/5/28. */ @RestController public class GetManInfo { @Autowired private ManInfoProperties manInfoProperties; @RequestMapping(value = "/getManInfo",method= RequestMethod.GET) public String getManInfo(){ return manInfoProperties.getAge(); } }
四、多環境配置

多環境配置
如上圖,創建application-dev.yml(測試環境)和application-prod.yml(生產)環境
application-dev.yml
server:
port: 8080
manInfo:
age: 18
name: jason
application-prod.yml
server:
port: 8081
manInfo:
age: 18
name: alun
而原有的application.yml則改成這樣:
spring:
profiles:
active: prod
spring.profiles.active: 配置文件名(比如這里是 prod或者dev)
作者:渝潼不肖生
鏈接:https://www.jianshu.com/p/fb6731ee53d2
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並注明出處。