SpringBoot bean依賴屬性配置詳細(xì)介紹
創(chuàng)建實(shí)體類
@Data public class Cat { private String name; private Integer age; }
@Data public class Mouse { private String name; private Integer age; }
配置文件application.yml使用固定格式為屬性注入數(shù)據(jù)
cartoon:
cat:
name: "圖多蓋洛"
age: 5
mouse:
name: "泰菲"
age: 6
業(yè)務(wù)類定義業(yè)務(wù)功能bean,在引導(dǎo)類通常使用@Import導(dǎo)入,解耦強(qiáng)制加載bean
使用@EnableConfigurationProperties注解設(shè)定使用屬性類時(shí)加載bean
@Data //當(dāng)加載CartoonCatAndMouse類時(shí)將CartoonProperties類加載成bean,不用不加載,解耦合 @EnableConfigurationProperties(CartoonProperties.class) public class CartoonCatAndMouse { private Cat cat; private Mouse mouse; private CartoonProperties cartoonProperties; public CartoonCatAndMouse(CartoonProperties cartoonProperties){ this.cartoonProperties = cartoonProperties; cat = new Cat(); cat.setName(cartoonProperties.getCat()!=null && StringUtils.hasText(cartoonProperties.getCat().getName()) ? cartoonProperties.getCat().getName() : "tom"); cat.setAge(cartoonProperties.getCat()!=null && cartoonProperties.getCat().getAge()!=null ? cartoonProperties.getCat().getAge() : 3); mouse = new Mouse(); mouse.setName(cartoonProperties.getMouse()!=null && StringUtils.hasText(cartoonProperties.getMouse().getName()) ? cartoonProperties.getMouse().getName() : "jerry"); mouse.setAge(cartoonProperties.getMouse()!=null && cartoonProperties.getMouse().getAge()!=null ? cartoonProperties.getMouse().getAge() : 4); } public void play(){ System.out.println(cat.getAge()+"歲的"+cat.getName()+"和"+ mouse.getAge()+"歲的"+mouse.getName()+"打起來了"); } }
將業(yè)務(wù)功能bean運(yùn)行需要的資源抽取長(zhǎng)獨(dú)立的屬性類,設(shè)置讀取配置文件的信息
@Data //類與配置文件綁定 @ConfigurationProperties(prefix = "cartoon") public class CartoonProperties { private Cat cat; private Mouse mouse; }
測(cè)試:
@SpringBootApplication //當(dāng)運(yùn)行時(shí)將CartoonCatAndMouse類加載成bean,不允許不加載 @Import(CartoonCatAndMouse.class) public class Demo15SpringbootBeanpropertiesApplication { public static void main(String[] args) { ConfigurableApplicationContext ctx = SpringApplication.run(Demo15SpringbootBeanpropertiesApplication.class, args); CartoonCatAndMouse bean = ctx.getBean(CartoonCatAndMouse.class); bean.play(); } }
總結(jié):
- 業(yè)務(wù)bean的屬性可以為其設(shè)定默認(rèn)值
- 當(dāng)需要設(shè)置時(shí)通過配置文件傳遞屬性
- 業(yè)務(wù)bean應(yīng)盡量避免設(shè)置強(qiáng)制加載,而是根據(jù)需要導(dǎo)入后加載,降低spring容器管理bean的強(qiáng)度
到此這篇關(guān)于SpringBoot bean依賴屬性配置詳細(xì)介紹的文章就介紹到這了,更多相關(guān)SpringBoot bean依賴屬性配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java的synchronized關(guān)鍵字深入解析
這篇文章主要介紹了Java的synchronized關(guān)鍵字深入解析,在并發(fā)編程中,多線程同時(shí)并發(fā)訪問的資源叫做臨界資源,當(dāng)多個(gè)線程同時(shí)訪問對(duì)象并要求操作相同資源時(shí),分割了原子操作就有可能出現(xiàn)數(shù)據(jù)的不一致或數(shù)據(jù)不完整的情況,需要的朋友可以參考下2023-12-12有關(guān)ServletConfig與ServletContext的訪問
下面小編就為大家?guī)硪黄嘘P(guān)ServletConfig與ServletContext的訪問。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01spring cloud內(nèi)容匯總(各個(gè)功能模塊、啟動(dòng)、部署)的詳細(xì)過程
Spring Cloud 是一套基于 Spring Boot 的框架集合,用于構(gòu)建分布式微服務(wù)架構(gòu),文章介紹了Spring Cloud框架及其相關(guān)工具的使用,還詳細(xì)介紹了如何配置和使用這些功能,包括配置文件、依賴管理以及實(shí)際應(yīng)用示例,感興趣的朋友一起看看吧2024-11-11用Rational Rose逆向工程(java)生成類圖(教程和錯(cuò)誤解決)
Rational Rose有個(gè)很方便的功能,將項(xiàng)目中的JAVA代碼自動(dòng)轉(zhuǎn)換成UML類圖2013-02-02dubbo將異常轉(zhuǎn)換成RuntimeException的原因分析?ExceptionFilter
這篇文章主要介紹了dubbo將異常轉(zhuǎn)換成RuntimeException的原因分析?ExceptionFilter問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03SpringBoot中MapStruct實(shí)現(xiàn)優(yōu)雅的數(shù)據(jù)復(fù)制
本文主要介紹了SpringBoot中MapStruct實(shí)現(xiàn)優(yōu)雅的數(shù)據(jù)復(fù)制,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08