聊聊@value注解和@ConfigurationProperties注解的使用
@value注解和@ConfigurationProperties注解
@value讀取默認配置
yml文件內(nèi)容如下(裝了STS插件以后即可直接使用,改后綴就行了)
user: username: xiongda sex: man age: 20 school: name: xtu location: hunan
備注:一定要注意之間要留空格,發(fā)現(xiàn)顏色變綠色了才是正確的格式,這個坑我剛踩
package com.example.demo.service.impl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.example.demo.service.ReadConfigService;
@Service
public class ReadConfigServiceImpl implements ReadConfigService {
@Value(value="${user.username}")
private String username;
@Value(value="${user.sex}")
private String sex;
@Value(value="${user.age}")
private String age;
@Value(value="${school.name}")
private String name;
@Value(value="${school.location}")
private String location;
@Override
public String getUserMessage() {
return "user ="+username+" sex ="+sex+" age="+age;
}
@Override
public String getAuthorMessage() {
return "schoolname="+name+"location="+location;
}
}
@ConfigurationProperties讀取默認配置
package com.example.demo.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="user")
public class HelloConfig {
private String username;
private String sex;
private String age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
調(diào)用的controller層
package com.example.demo.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.config.HelloConfig;
import com.example.demo.service.ReadConfigService;
@RestController
@RequestMapping("hello")
public class HelloController {
@Autowired
ReadConfigService readConfigService;
@Autowired
HelloConfig helloConfig;
@RequestMapping("/user")
public String say() {
return "username "+helloConfig.getUsername()+" sex "+helloConfig.getSex()+" age "+helloConfig.getAge();
}
@RequestMapping("/school")
public String school() {
return readConfigService.getAuthorMessage();
}
}
@ConfigurationProperties和@Value使用上的一點區(qū)別
@ConfigurationProperties和@Value的一個共同點就是從配置文件中讀取配置項。
發(fā)現(xiàn)有一點區(qū)別,我項目配置中并沒有配置hello.msg ,當使用第一段代碼時,啟動后讀取到msg為null,而第二段代碼則會拋出異常。
第二段代碼有個好處,就是防止我們配置項遺漏,當遺漏時,啟動程序肯定出錯,這樣避免了一些因為遺漏配置項導(dǎo)致的BUG.
第一段代碼
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("hello")
public class HelloProperties {
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
第二段代碼
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Hello2Properties {
@Value("${hello.msg}")
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
劍指Offer之Java算法習(xí)題精講二叉樹的構(gòu)造和遍歷
跟著思路走,之后從簡單題入手,反復(fù)去看,做過之后可能會忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會發(fā)現(xiàn)質(zhì)的變化2022-03-03
maven profile自動切換環(huán)境參數(shù)的2種方法詳解
這篇文章主要給大家介紹了關(guān)于maven profile自動切換環(huán)境參數(shù)的2種方法,文中通過示例代碼將這兩種方法介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04
eclipse+maven+spring mvc項目基本搭建過程
這篇文章主要介紹了eclipse+maven+spring mvc項目基本搭建過程,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09

