欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot bean依賴屬性配置詳細(xì)介紹

 更新時(shí)間:2022年09月26日 15:42:44   作者:搗蛋孩學(xué)編程  
Spring容器是Spring的核心,一切SpringBean都存儲(chǔ)在Spring容器內(nèi)??梢哉fbean是spring核心中的核心。Bean配置信息定義了Bean的實(shí)現(xiàn)及依賴關(guān)系,這篇文章主要介紹了SpringBoot bean依賴屬性配置

創(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é):

  1. 業(yè)務(wù)bean的屬性可以為其設(shè)定默認(rèn)值
  2. 當(dāng)需要設(shè)置時(shí)通過配置文件傳遞屬性
  3. 業(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)鍵字深入解析

    這篇文章主要介紹了Java的synchronized關(guān)鍵字深入解析,在并發(fā)編程中,多線程同時(shí)并發(fā)訪問的資源叫做臨界資源,當(dāng)多個(gè)線程同時(shí)訪問對(duì)象并要求操作相同資源時(shí),分割了原子操作就有可能出現(xiàn)數(shù)據(jù)的不一致或數(shù)據(jù)不完整的情況,需要的朋友可以參考下
    2023-12-12
  • 如何徹底刪除SVN中的文件和文件夾(附恢復(fù)方法)

    如何徹底刪除SVN中的文件和文件夾(附恢復(fù)方法)

    在SVN中如果刪除某個(gè)文件或文件夾也可以在歷史記錄中進(jìn)行找回,有的時(shí)候需要徹底刪除某些文件,即不希望通過歷史記錄進(jìn)行恢復(fù),需要在服務(wù)器上對(duì)SVN的數(shù)據(jù)進(jìn)行重新整理
    2014-08-08
  • 有關(guān)ServletConfig與ServletContext的訪問

    有關(guān)ServletConfig與ServletContext的訪問

    下面小編就為大家?guī)硪黄嘘P(guān)ServletConfig與ServletContext的訪問。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01
  • springboot日期格式化及時(shí)差問題分析

    springboot日期格式化及時(shí)差問題分析

    這篇文章主要介紹了springboot日期格式化,時(shí)差問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-12-12
  • spring cloud內(nèi)容匯總(各個(gè)功能模塊、啟動(dòng)、部署)的詳細(xì)過程

    spring 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
  • 淺談Spring自定義注解從入門到精通

    淺談Spring自定義注解從入門到精通

    這篇文章主要介紹了淺談Spring自定義注解從入門到精通,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 用Rational Rose逆向工程(java)生成類圖(教程和錯(cuò)誤解決)

    用Rational Rose逆向工程(java)生成類圖(教程和錯(cuò)誤解決)

    Rational Rose有個(gè)很方便的功能,將項(xiàng)目中的JAVA代碼自動(dòng)轉(zhuǎn)換成UML類圖
    2013-02-02
  • dubbo將異常轉(zhuǎn)換成RuntimeException的原因分析?ExceptionFilter

    dubbo將異常轉(zhuǎn)換成RuntimeException的原因分析?ExceptionFilter

    這篇文章主要介紹了dubbo將異常轉(zhuǎn)換成RuntimeException的原因分析?ExceptionFilter問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • zookeeper watch機(jī)制的理解

    zookeeper watch機(jī)制的理解

    這篇文章主要介紹了zookeeper watch機(jī)制的相關(guān)內(nèi)容,內(nèi)容比較詳細(xì),需要的朋友可以參考下。
    2017-09-09
  • SpringBoot中MapStruct實(shí)現(xiàn)優(yōu)雅的數(shù)據(jù)復(fù)制

    SpringBoot中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

最新評(píng)論