亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring示例講解條件注入方法

 更新時間:2022年06月21日 10:58:45   作者:IT利刃出鞘  
Spring支持按照條件來注入某些特定的bean,這也是Spring Boot實現(xiàn)自動化配置的底層方法,文中的示例代碼講解詳細,需要的可以參考一下

簡介

說明

本文用實例介紹Spring的條件注入的用法。

@Component、@Configuration+@Bean都可以與條件注入的注解結合。

@Component+條件注解

Bean

package com.example.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
@Component
@ConditionalOnProperty(name = "custom.myComponent.enabled", havingValue = "true")
public class MyComponent {
    public MyComponent() {
        System.out.println("[MyComponent#MyComponent]");
    }
}

application.yml

custom:
  myComponent:
    enabled: true

運行結果:

[MyComponent#MyComponent]

若將application.yml的custom.myComponent.enabled去掉,或者設置為非true值,則不會輸出上邊的運行結果。

@Configuration+@Bean+條件注解

Bean

package com.example.config;
public class MyComponent {
    public MyComponent() {
        System.out.println("[MyComponent#MyComponent]");
    }
}

配置類

package com.example.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfig {
    @Bean
    @ConditionalOnProperty(name = "custom.myComponent.enabled", havingValue = "true")
    public MyComponent getMyComponent() {
        return new MyComponent();
    }
}

application.yml

custom:
  myComponent:
    enabled: true

運行結果:

[MyComponent#MyComponent]

若將application.yml的custom.myComponent.enabled去掉,或者設置為非true值,則不會輸出上邊的運行結果。

@Configuration+條件注解+@Bean

Bean

package com.example.config;
public class MyComponent {
    public MyComponent() {
        System.out.println("[MyComponent#MyComponent]");
    }
}

配置類

package com.example.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnProperty(name = "custom.myComponent.enabled", havingValue = "true")
public class MyConfig {
    @Bean
    public MyComponent getMyComponent() {
        return new MyComponent();
    }
}

application.yml

custom:
  myComponent:
    enabled: true

運行結果:

[MyComponent#MyComponent]

若將application.yml的custom.myComponent.enabled去掉,或者設置為非true值,則不會輸出上邊的運行結果。

自定義Condition

自定義的condition的matches方法返回值為true時,才會創(chuàng)建bean。

條件類

//判斷當前系統(tǒng)是否是Mac

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class MyCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, 
                           AnnotatedTypeMetadata annotatedTypeMetadata) {
        return conditionContext.getEnvironment().getProperty("os.name").contains("Mac");
    }
}
@Configuration
public class Config {
    @Conditional(MyCondition.class)
    @Bean
    public String condition() {
        System.err.println("This is mac");
        return "";
    }
}

到此這篇關于Spring示例講解條件注入方法的文章就介紹到這了,更多相關Spring條件注入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java線性結構中棧、隊列和串的基本概念和特點詳解

    Java線性結構中棧、隊列和串的基本概念和特點詳解

    前幾天小編給大家介紹了Java線性結構中的鏈表,除了鏈表這種結構之外,實際上還有棧、隊列、串等結構,那么這些結構又有哪些特點呢,本文就給大家詳細的介紹一下,感興趣的小伙伴跟著小編一起來看看吧
    2023-07-07
  • Java中一個for語句導致無窮大死循環(huán)的例子

    Java中一個for語句導致無窮大死循環(huán)的例子

    這篇文章主要介紹了Java中一個for語句導致無窮大死循環(huán)的例子,本文給出的是一個很特別的例子,這個例子會跟你所想的結果不一樣,需要的朋友可以參考下
    2015-06-06
  • Java文件拒絕訪問問題及解決

    Java文件拒絕訪問問題及解決

    這篇文章主要介紹了Java文件拒絕訪問問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • mybatisplus中返回Vo的案例講解

    mybatisplus中返回Vo的案例講解

    這篇文章主要介紹了mybatisplus中返回Vo的案例,mybatisplus內(nèi)置的幾個方法使用泛型限制了方法的返回類型,所以實現(xiàn)返回Vo還是得自定義方法,?這個方法名盡量不要和原有的名字類似,本文通過實例代碼給大家詳解講解,需要的朋友可以參考下
    2023-03-03
  • Java之HashMap.values()方法的誤用解讀

    Java之HashMap.values()方法的誤用解讀

    這篇文章主要介紹了Java之HashMap.values()方法的誤用解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • java連接MySQL數(shù)據(jù)庫的代碼

    java連接MySQL數(shù)據(jù)庫的代碼

    這篇文章主要為大家詳細介紹了java連接MySQL數(shù)據(jù)庫的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Java高級之HashMap中的entrySet()方法使用

    Java高級之HashMap中的entrySet()方法使用

    這篇文章主要介紹了Java高級之HashMap中的entrySet()方法使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • SpringBoot整合MongoDB的實現(xiàn)代碼

    SpringBoot整合MongoDB的實現(xiàn)代碼

    自己本科時候一直使用的是Mysql,目前的課題組使用的是MongoDB,因此就花了一部分時間整理了一下,實現(xiàn)springboot與MongoDB的整合,并且實現(xiàn)基本的增刪改查操作,從頭到尾給出一個完整的案例。
    2021-05-05
  • Java之網(wǎng)絡編程案例講解

    Java之網(wǎng)絡編程案例講解

    這篇文章主要介紹了Java之網(wǎng)絡編程案例講解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • Java通過正則表達式捕獲組中的文本

    Java通過正則表達式捕獲組中的文本

    這篇文章主要給大家介紹了關于利用Java如何通過正則表達式捕獲組中文本的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Java具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧下
    2019-09-09

最新評論