如圖所示,咱們在resources文件夾中新建配置文件application.ymlmysql
結構圖web
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後面的冒號,後面必定要跟一個空格spring
一、在application.yml文件中咱們本身定義了age 、name 、manInfo等參數,其中manInfo引用了age、name,引用的格式"${參數名}"sql
server: //端口 port: 8081 age: 18 name: jason manInfo: "age:${age},name:${name}"
怎麼使用這些配置呢?咱們建立GetManInfo文件(參照上面結構圖),
使用配置格式數據庫
@Value("${配置文件中的參數名}") 類型 參數名
詳細以下tomcat
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; } }
二、一個一個的@Value獲取以爲很煩,有辦法解決麼?這個....固然有啊!session
在application.yml咱們改爲這樣app
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裏 使用 @Autowiredthis
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)