從應用程式 yml 載入物件清單 Java Spring Boot
php小編西瓜帶你深入探討Java中應用程式yml如何載入物件清單的問題。在Java Spring Boot中,透過合理配置yml文件,可以實現物件清單的加載,並在應用程式中靈活應用。接下來,讓我們一起來掌握這項技巧,提昇在Spring Boot開發中的實用性和效率。
問題內容
我有這個設定類別:
@configuration @configurationproperties(prefix = "house") public class projectconfig{ private list<housetemplate> templates; // getters and setters }
登入後複製
housetemplate 類別:
public class housetemplate{ private string id; private string description; // getters and setters }
登入後複製
這是我的應用程式-test.yml
house: templates: - id: "colonial" description: "colonial house template" - id: "cottage" description: "cottage house template" - id: "apartment" description: "apartment house template" examplestring: hello
登入後複製
在我的測試類別中,我有以下內容:
@runwith(springrunner.class) @enableconfigurationproperties(value = projectconfig.class) @testpropertysource("classpath:application-test.yml") public class yamlapplicationcontextloadingspec { @value("${examplestring}") string example; @autowired projectconfig projectconfig; @test public void exampleshouldcontainhello(){ assertthat(example).isequaltoignoringcase("hello"); } @test public void appcontextcontainshousetemplates(){ list<housetemplate> housetemplates = projectconfig.gettemplates(); assertthat(housetemplates).isnotnull(); } }
登入後複製
關於 examplestring 的第一個測試通過,而第二個測試沒有通過。為什麼無法將yml對應到housetemplate清單?
編輯
<artifactId>spring-core</artifactId> <version>4.3.6.RELEASE</version> <artifactId>spring-boot</artifactId> <version>1.5.1.RELEASE</version> <artifactId>junit</artifactId> <version>4.12</version>
登入後複製
我知道它們真的很舊,我很想升級,但我不能…這就是我必須處理的。
解決方法
使用@configurationproperties
從yml讀取後,可以註冊為bean,然後可以透過applicationcontext
取得。
@configuration @configurationproperties(prefix = "house") public class testconfig { private list<housetemplate> templates; @bean public list<housetemplate> templates() { return templates; } // getter and setter } @restcontroller @requestmapping("/api/test") public class testcontroller { @autowired private applicationcontext applicationcontext; @getmapping("get") @suppresswarnings("unchecked") public list<housetemplate> get() { return (list<housetemplate>) applicationcontext.getbean("templates"); } }
登入後複製
如果你不想註冊為bean,也可以將變數宣告為靜態。
這是一個範例。
@Configuration @ConfigurationProperties(prefix = "House") public class TestConfig { private static List<HouseTemplate> templates; public static List<HouseTemplate> get() { return templates; } public List<HouseTemplate> getTemplates() { return templates; } public void setTemplates(List<HouseTemplate> templates) { TestConfig.templates = templates; } } @RestController @RequestMapping("/api/test") public class TestController { @GetMapping("get") public List<HouseTemplate> get() { return TestConfig.get(); } }
登入後複製
以上是從應用程式 yml 載入物件清單 Java Spring Boot的詳細內容。更多資訊請關注PHP中文網其他相關文章!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章
刺客信條陰影:貝殼謎語解決方案
4 週前
By DDD
Windows 11 KB5054979中的新功能以及如何解決更新問題
3 週前
By DDD
在哪裡可以找到原子中的起重機控制鑰匙卡
4 週前
By DDD
<🎜>:死鐵路 - 如何完成所有挑戰
1 個月前
By DDD
如何修復KB5055523無法在Windows 11中安裝?
2 週前
By DDD

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)