SpringBoot 學(xué)習(xí)——如何設(shè)置和讀取配置文件中屬性
SpringBoot學(xué)習(xí)——如何設(shè)置和讀取配置文件中屬性
配置文件配置
直接配置
在src/main/resources下添加配置文件application.properties
例如修改端口號(hào)
#端口號(hào)
server.port=8089
分環(huán)境配置
在src/main/resources下添加,application-pro.properties,application-dev.properties和application.properties三個(gè)文件
application.propertie
spring.profiles.active=dev
application-pro.properties
#端口號(hào)
server.port=80
#自定義端口號(hào)讀取
my.name=pzr.dev
application-dev.properties
#端口號(hào)
server.port=8089
#自定義端口號(hào)讀取
my.name=pzr.pro
當(dāng)application.propertie設(shè)置spring.profiles.active=dev時(shí),則說(shuō)明是指定使用application-dev.properties文件進(jìn)行配置
配置文件參數(shù)讀取
注解方式讀取
- @PropertySource配置文件路徑設(shè)置,在類(lèi)上添加注解,如果在默認(rèn)路徑下可以不添加該注解
classpath:config/my.properties指的是src/main/resources目錄下config目錄下的my.properties文件多配置文件引用,若取兩個(gè)配置文件中有相同屬性名的值,則取值為最后一個(gè)配置文件中的值 @PropertySource({"classpath:config/my.properties","classpath:config/config.properties"}) public class TestController - @Value屬性名,在屬性名上添加該注解
@Value("${my.name}") private String myName;
對(duì)象映射方式讀取
- 首先建立對(duì)象與配置文件映射關(guān)系
- 方法中使用自動(dòng)注入方式,將對(duì)象注入,調(diào)用get方法獲取屬性值
- 注意:新版本的@ConfigurationProperties沒(méi)有了location屬性,使用@PropertySource來(lái)指定配置文件位置
- prefix="obj"指的是配置文件中的前綴,如obj.name,在定義對(duì)象屬性名時(shí)為private String name;
- 讀取配置文件中的集合時(shí),使用List來(lái)接收數(shù)據(jù),但List必須先實(shí)例化
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;
/**
* 測(cè)試接口
* @author pzr
*
*/
@Controller
@RequestMapping("/testController")
@PropertySource({"classpath:config/my.properties","classpath:config/config.properties"})
public class TestController {
//在application.properties中的文件,直接使用@Value讀取即可,applicarion的讀取優(yōu)先級(jí)最高
@Value("${my.name}")
private String myName;
//如果多個(gè)文件有重復(fù)的名稱(chēng)的屬性話,最后一個(gè)文件中的屬性生效
@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;
}
/**
* 使用對(duì)象映射的方式讀取配置文件
* @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;
/**
* 配置文件映射對(duì)象
* @author pzr
*
*/
@Component
@PropertySource("classpath:config/object.properties")
@ConfigurationProperties(prefix="obj")
public class ObjectProperties {
private String name;
private String age;
//集合必須初始化,如果找不到就是空集合,會(huì)報(bào)錯(cuò)
private List className = new ArrayList();
public List getClassName() {
return className;
}
public void setClassName(List 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]評(píng)論
圖片
表情
