聊聊@value注解和@ConfigurationProperties注解的使用
@value注解和@ConfigurationProperties注解
@value讀取默認配置
yml文件內容如下(裝了STS插件以后即可直接使用,改后綴就行了)
user: username: xiongda sex: man age: 20 school: name: xtu location: hunan
備注:一定要注意之間要留空格,發(fā)現顏色變綠色了才是正確的格式,這個坑我剛踩
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; } }
調用的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ā)現有一點區(qū)別,我項目配置中并沒有配置hello.msg ,當使用第一段代碼時,啟動后讀取到msg為null,而第二段代碼則會拋出異常。
第二段代碼有個好處,就是防止我們配置項遺漏,當遺漏時,啟動程序肯定出錯,這樣避免了一些因為遺漏配置項導致的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; } }
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
java使用elasticsearch分組進行聚合查詢過程解析
這篇文章主要介紹了java使用elasticsearch分組進行聚合查詢過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-02-02解決SpringBoot application.yaml文件配置schema 無法執(zhí)行sql問題
這篇文章主要介紹了解決SpringBoot application.yaml文件配置schema 無法執(zhí)行sql問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08java?web實現簡單登錄注冊功能全過程(eclipse,mysql)
前期我們學習了javaweb項目用JDBC連接數據庫,還有數據庫的建表功能,下面這篇文章主要給大家介紹了關于java?web實現簡單登錄注冊功能的相關資料,需要的朋友可以參考下2022-07-07spring kafka框架中@KafkaListener 注解解讀和使用案例
Kafka 目前主要作為一個分布式的發(fā)布訂閱式的消息系統(tǒng)使用,也是目前最流行的消息隊列系統(tǒng)之一,這篇文章主要介紹了kafka @KafkaListener 注解解讀,需要的朋友可以參考下2023-02-02