一猾编、創(chuàng)建配置文件
如圖所示疑故,我們在resources文件夾中新建配置文件application.yml
結構圖
二隅要、一些基本配置
server:
port: 8090 //配置端口
session-timeout: 30
tomcat.max-threads: 0
tomcat.uri-encoding: UTF-8
spring:
datasource: //數(shù)據(jù)庫配置
url : jdbc:mysql://localhost:3306/newbirds
username : root
password : mymysql
driverClassName : com.mysql.jdbc.Driver
注意:key后面的冒號,后面一定要跟一個空格
三 、自定義的配置
1埠偿、在application.yml文件中我們自己定義了age 透罢、name 、manInfo等參數(shù)冠蒋,其中manInfo引用了age羽圃、name,引用的格式"${參數(shù)名}"
server:
//端口
port: 8081
age: 18
name: jason
manInfo: "age:${age},name:${name}"
怎么使用這些配置呢抖剿?我們創(chuàng)建GetManInfo文件(參照上面結構圖)朽寞,
使用配置格式
@Value("${配置文件中的參數(shù)名}")
類型 參數(shù)名
詳細如下
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 = "配置文件里的參數(shù)名" )
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();
}
}
四孽拷、多環(huán)境配置
多環(huán)境配置
如上圖吨掌,創(chuàng)建application-dev.yml(測試環(huán)境)和application-prod.yml(生產(chǎn))環(huán)境
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)