直接配置
在src/main/resources下添加配置文件application.properties
例如修改端口號
#端口號
server.port=8089
分環境配置
在src/main/resources下添加,application-pro.properties,application-dev.properties和application.properties三個文件
application.propertie
spring.profiles.active=dev
application-pro.properties
#端口號
server.port=80
#自定義端口號讀取
my.name=pzr.dev
application-dev.properties
#端口號
server.port=8089
#自定義端口號讀取
my.name=pzr.pro
當application.propertie設置spring.profiles.active=dev時,則說明是指定使用application-dev.properties文件進行配置
配置文件參數讀取
注解方式讀取
1.@PropertySource配置文件路徑設置,在類上添加注解,如果在默認路徑下可以不添加該注解
classpath:config/my.properties指的是src/main/resources目錄下config目錄下的my.properties文件
多配置文件引用,若取兩個配置文件中有相同屬性名的值,則取值為最后一個配置文件中的值
@PropertySource({"classpath:config/my.properties","classpath:config/config.properties"})
public class TestController
1.@Value屬性名,在屬性名上添加該注解
@Value("${my.name}")
private String myName;
對象映射方式讀取
1.首先建立對象與配置文件映射關系
2. 方法中使用自動注入方式,將對象注入,調用get方法獲取屬性值
3.注意:新版本的@ConfigurationProperties沒有了location屬性,使用@PropertySource來指定配置文件位置
4.prefix=”obj”指的是配置文件中的前綴,如obj.name,在定義對象屬性名時為private String name;
5.讀取配置文件中的集合時,使用List來接收數據,但List必須先實例化
TestController.java
package com.gyqc.ms.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gyqc.ms.entity.config.ObjectProperties;
/**
* 測試接口
* @author pzr
*
*/
@Controller
@RequestMapping("/testController")
@PropertySource({"classpath:config/my.properties","classpath:config/config.properties"})
public class TestController {
//在application.properties中的文件,直接使用@Value讀取即可,applicarion的讀取優先級最高
@Value("${my.name}")
private String myName;
//如果多個文件有重復的名稱的屬性話,最后一個文件中的屬性生效
@Value("${my.name1}")
private String myName1;
@Value("${my.name2}")
private String myName2;
@Autowired
ObjectProperties objectProperties;
@RequestMapping("/test")
@ResponseBody
String test() {
return "myName:"+myName+" myName1:"+myName1+" name:"+myName2;
}
/**
* 使用對象映射的方式讀取配置文件
* @return
*/
@RequestMapping("/test1")
@ResponseBody
String test1(){
StringBuffer sb = new StringBuffer();
sb.append(" name:");
sb.append(objectProperties.getName());
sb.append(" age:");
sb.append(objectProperties.getAge());
sb.append(" className:");
for(String str : objectProperties.getClassName()){
sb.append(str);
sb.append(",");
}
return sb.toString();
}
}
ObjectProperties.java
package com.gyqc.ms.entity.config;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* 配置文件映射對象
* @author pzr
*
*/
@Component
@PropertySource("classpath:config/object.properties")
@ConfigurationProperties(prefix="obj")
public class ObjectProperties {
private String name;
private String age;
//集合必須初始化,如果找不到就是空集合,會報錯
private List<String> className = new ArrayList<String>();
public List<String> getClassName() {
return className;
}
public void setClassName(List<String> className) {
this.className = className;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
object.properties
#自定義屬性讀取
obj.name=obj.name
obj.age=obj.age
obj.className[0]=obj.className[0]
obj.className[1]=obj.className[1]
原文:https://blog.csdn.net/flygoa/article/details/58075398