聊聊@value注解和@ConfigurationProperties注解的使用
@value注解和@ConfigurationProperties注解
@value讀取默認(rèn)配置
yml文件內(nèi)容如下(裝了STS插件以后即可直接使用,改后綴就行了)
user: username: xiongda sex: man age: 20 school: name: xtu location: hunan
備注:一定要注意之間要留空格,發(fā)現(xiàn)顏色變綠色了才是正確的格式,這個(gè)坑我剛踩
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讀取默認(rèn)配置
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使用上的一點(diǎn)區(qū)別
@ConfigurationProperties和@Value的一個(gè)共同點(diǎn)就是從配置文件中讀取配置項(xiàng)。
發(fā)現(xiàn)有一點(diǎn)區(qū)別,我項(xiàng)目配置中并沒(méi)有配置hello.msg ,當(dāng)使用第一段代碼時(shí),啟動(dòng)后讀取到msg為null,而第二段代碼則會(huì)拋出異常。
第二段代碼有個(gè)好處,就是防止我們配置項(xiàng)遺漏,當(dāng)遺漏時(shí),啟動(dòng)程序肯定出錯(cuò),這樣避免了一些因?yàn)檫z漏配置項(xiàng)導(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;
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
劍指Offer之Java算法習(xí)題精講二叉樹的構(gòu)造和遍歷
跟著思路走,之后從簡(jiǎn)單題入手,反復(fù)去看,做過(guò)之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化2022-03-03
RocketMQ的push消費(fèi)方式實(shí)現(xiàn)示例
這篇文章主要為大家介紹了RocketMQ的push消費(fèi)方式實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪<BR>2022-08-08
Flowable?ReceiveTask使用場(chǎng)景分析
這篇文章主要為大家介紹了Flowable?ReceiveTask使用場(chǎng)景分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Java操作xls替換文本或圖片的功能實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于Java操作xls替換文本或圖片功能實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼講解了文件上傳、文件處理和Excel文件生成,需要的朋友可以參考下2024-12-12
maven profile自動(dòng)切換環(huán)境參數(shù)的2種方法詳解
這篇文章主要給大家介紹了關(guān)于maven profile自動(dòng)切換環(huán)境參數(shù)的2種方法,文中通過(guò)示例代碼將這兩種方法介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04
1小時(shí)快速上手RabbitMQ(簡(jiǎn)介及安裝過(guò)程)
RabbitMQ簡(jiǎn)稱MQ全稱是Message Queue(消息隊(duì)列),是在消息的傳輸過(guò)程中保存消息的容器,多用于分布式系統(tǒng)之間進(jìn)行通信,本文給大家講解了RabbitMQ簡(jiǎn)介與安裝,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友跟隨小編一起看看吧2023-01-01
eclipse+maven+spring mvc項(xiàng)目基本搭建過(guò)程
這篇文章主要介紹了eclipse+maven+spring mvc項(xiàng)目基本搭建過(guò)程,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09

