日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot中Bean按条件装配

發布時間:2023/12/4 javascript 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot中Bean按条件装配 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

@Conditional條件裝配

  • @Conditional是Spring Framework提供的一個核心功能注解,這個注解的作用是提供自動裝配的條件限制,一般我們在用@Configuration,@Bean的時候使用它。
  • 也就是我們在自定義Bean的注入的時候,我們可以通過@Condition來對bean的注入增加邏輯判斷,符合我們要求的我們才讓他自動裝配
@Conditional 的使用
  • 如下注解源碼是@Condition
@FunctionalInterface public interface Condition {/*** Determine if the condition matches.* @param context the condition context* @param metadata metadata of the {@link org.springframework.core.type.AnnotationMetadata class}* or {@link org.springframework.core.type.MethodMetadata method} being checked* @return {@code true} if the condition matches and the component can be registered,* or {@code false} to veto the annotated component's registration*/boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);}
  • condition是一個函數式接口,提供了一個matches方法,主要提供一個條件匹配的規則,返回表示是否可以注入Bean。

  • @Conditional 的注解來聲明如下,他可以接受接收一個Condition的數組

@Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Conditional {/*** All {@link Condition Conditions} that must {@linkplain Condition#matches match}* in order for the component to be registered.*/Class<? extends Condition>[] value();}
Conditional使用Demo
  • 我們通過對以上Conditional的了解來自己實現一個Bean的按自定義條件的裝配:
  • 定義一個Condition,判斷系統來返回是否裝配:
/*** Created by jiamin5 on 2022/3/10.*/ public class GpCondition implements Condition{private static final Logger logger = LoggerFactory.getLogger(GpCondition.class);@Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {String os = context.getEnvironment().getProperty("os.name");logger.info("GpCondition matthes osName:{}", os);if(os.contains("Windows")){return true;}return false;} }
  • 定義配置類,裝載一個BeanTestClass
/*** Created by jiamin5 on 2022/3/10.*/ @Configuration public class ConditionConfig {@Bean@Conditional(GpCondition.class)public BeanTestClass beanTestClass(){return new BeanTestClass();} }
  • 如上,我們在BeanTestClass上增加了一個@Conditional(GpCondition.class),其中的具體條件就是我們自定義的注入限制條件類。
  • 意思就是,當條件滿足我們定義的GpCondition的時候,我們就注入,否則不注入。
  • 依然在Application啟動類中添加對新定義Bean的獲取,如下
/*** @author liaojiamin* @Date:Created in 11:08 2022/3/7*/ @SpringBootApplication @EnableAutoImport public class Application {public static void main(String[] args) {ConfigurableApplicationContext ca = SpringApplication.run(Application.class, args);System.out.println(ca.getBean(FilterFirstObj.class));BeanTestClass beanTestClass = ca.getBean(BeanTestClass.class);System.out.println(beanTestClass);} }
  • 當我們在Mac os上運行,有如下結果

Spring Boot 中的@Conditional

  • 在SpringBoot中,有針對@Conditional的擴展,提供了更簡單的使用方式,擴張了各種類型的注解,如下:

    • ConditionalOnBean/ConditionalOnMissBean 容器中存在或者不存在某個類的時候進行Bean加載
    • ConditionalOnClass/ConditionalOnMissClass:classpath下存在或者不存在指定類的時候進行Bean加載
    • ConditionalOnCloudPlatform:只允許在指定的云平臺上才加載指定Bean
    • ConditionalOnExpression:基于SpEl表達式的條件判斷
    • ConditionalOnJava:只允許在指定版本Java才加載Bean
    • ConditionalOnJndi:只有指定資源通過JNDI加載后才加載Bean
    • ConditionalOnWebApplication/ConsitionalOnNotWebApplication:如果是活著不是web應用才加載指定的Bean
    • ConditionalOnProperty:系統中指定對呀的屬性是否有對應值
    • ConditionOnResource:要加載的Bean依賴指定資源是否存在于classpath
    • ConditionOnSingleCandidate:只有在確定了給定Bean的單個候選項時候才會加載Bean
  • 以上這些都在spring-boot-autoconfiguration.jar 中

其他注入方式spring-autoconfigure-metadata

  • 除了@Conditional注解。Spring Boot中提供了spring-autoconfigure-metadata.properties文件來實現批量自動裝配條件配置

  • 與@Conditional意義,只是將條件放在的文件,我們可以在spring-boot-autoconfigure.jar中找到這種配置

  • 同樣遵循“約定由于配置”,通過這種配置實現條件過濾需保證兩個前提條件:

    • 如上圖中,文件路徑名稱必須是/META-INF/spring-autoconfigure-metadata.properties
    • 配置文件的key’的配置格式:自動配置類的類全路徑名.條件 = 值
  • 這種配置優點在于可以有效降低Spring Boot啟動時間,通過這種過濾方式減少配置陪的加載數量,因為這個過濾發生在配置類的裝載之前,所以他可以降低Spring Boot啟動時裝載Bean的耗時

上一篇:SpringBoot自動裝配源碼解析

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的SpringBoot中Bean按条件装配的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。