国产精品电影_久久视频免费_欧美日韩国产激情_成年人视频免费在线播放_日本久久亚洲电影_久久都是精品_66av99_九色精品美女在线_蜜臀a∨国产成人精品_冲田杏梨av在线_欧美精品在线一区二区三区_麻豆mv在线看

Quarkus依賴注入:用注解選擇注入Bean

開發 前端
本篇學習一個與創建Bean有關的重要知識點:一個接口如果有多個實現類時,Bean實例應該如何選擇其中的一個呢?可以用注解來設定Bean的選擇邏輯。

本篇概覽

  • 本文是《quarkus依賴注入》系列的第三篇,前文咱們掌握了創建bean的幾種方式,本篇趁熱打鐵,學習一個與創建bean有關的重要知識點:一個接口如果有多個實現類時,bean實例應該如何選擇其中的一個呢?可以用注解來設定bean的選擇邏輯。
  • 如果您熟悉spring,此刻應該會想到ConditionalXXX注解,下面的代碼來自spring官方,注解ConditionalOnProperty的作用是根據配置信息來控制bean是否實例化,本篇咱們要掌握的是quarkus框架下的類似控制邏輯。
@Service
@ConditionalOnProperty(
  value="logging.enabled", 
  havingValue = "true", 
  matchIfMissing = true)
class LoggingService {
    // ...
}
  • 本篇主要是通過實例學習以下五個注解的用法。
  1. LookupIfProperty,配置項的值符合要求才能使用bean。
  2. LookupUnlessProperty,配置項的值不符合要求才能使用bean。
  3. IfBuildProfile,如果是指定的profile才能使用bean。
  4. UnlessBuildProfile,如果不是指定的profile才能使用bean。
  5. IfBuildProperty,如果構建屬性匹配才能使用bean。

源碼下載

  • 本篇實戰的完整源碼可在GitHub下載到,地址和鏈接信息如下表所示(https://github.com/zq2599/blog_demos)。

  • 這個git項目中有多個文件夾,本次實戰的源碼在quarkus-tutorials文件夾下,如下圖紅框。

  • quarkus-tutorials是個父工程,里面有多個module,本篇實戰的module是basic-di,如下圖紅框。

LookupIfProperty,配置項的值符合要求才能使用bean

  • 注解LookupIfProperty的作用是檢查指定配置項,如果存在且符合要求,才能通過代碼獲取到此bean。
  • 有個關鍵點請注意:下圖是官方定義,可見LookupIfProperty并沒有決定是否實例化beam,它決定的是能否通過代碼取到bean,這個代碼就是Instance<T>來注入,并且用Instance.get方法來獲取。

  • 定義一個接口TryLookupIfProperty.java。
public interface TryLookupIfProperty {
    String hello();
}
  • 以及兩個實現類,第一個是TryLookupIfPropertyAlpha.java。
public class TryLookupIfPropertyAlpha implements TryLookupIfProperty {
    @Override
    public String hello() {
        return "from " + this.getClass().getSimpleName();
    }
}
  • 第二個TryLookupIfPropertyBeta.java。
public class TryLookupIfPropertyBeta implements TryLookupIfProperty {
    @Override
    public String hello() {
        return "from " + this.getClass().getSimpleName();
    }
}
  • 然后就是注解LookupIfProperty的用法了,如下所示,SelectBeanConfiguration是個配置類,里面有兩個方法用來生產bean,都用注解LookupIfProperty修飾,如果配置項service.alpha.enabled的值等于true,就會執行tryLookupIfPropertyAlpah方法,如果配置項service.beta.enabled的值等于true,就會執行tryLookupIfPropertyBeta方法。
package com.bolingcavalry.config;

import com.bolingcavalry.service.TryLookupIfProperty;
import com.bolingcavalry.service.impl.TryLookupIfPropertyAlpha;
import com.bolingcavalry.service.impl.TryLookupIfPropertyBeta;
import io.quarkus.arc.lookup.LookupIfProperty;
import javax.enterprise.context.ApplicationScoped;

public class SelectBeanConfiguration {

    @LookupIfProperty(name = "service.alpha.enabled", stringValue = "true")
    @ApplicationScoped
    public TryLookupIfProperty tryLookupIfPropertyAlpha() {
        return new TryLookupIfPropertyAlpha();
    }

    @LookupIfProperty(name = "service.beta.enabled", stringValue = "true")
    @ApplicationScoped
    public TryLookupIfProperty tryLookupIfPropertyBeta() {
        return new TryLookupIfPropertyBeta();
    }
}
  • 然后來驗證注解LookupIfProperty是否生效,下面是單元測試代碼,有兩處需要注意的地方,稍后會提到。
package com.bolingcavalry;

import com.bolingcavalry.service.TryLookupIfProperty;
import com.bolingcavalry.service.impl.TryLookupIfPropertyAlpha;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;

@QuarkusTest
public class BeanInstanceSwitchTest {

    @BeforeAll
    public static void setUp() {
        System.setProperty("service.alpha.enabled", "true");
    }

    // 注意,前面的LookupIfProperty不能決定注入bean是否實力話,只能決定Instance.get是否能取到,
    //所以此處要注入的是Instance,而不是TryLookupIfProperty本身
    @Inject
    Instance<TryLookupIfProperty> service;

    @Test
    public void testTryLookupIfProperty() {
        Assertions.assertEquals("from " + tryLookupIfPropertyAlpha.class.getSimpleName(),
                                service.get().hello());
    }
}
  • 上述代碼有以下兩點要注意。
  1. 注意TryLookupIfProperty的注入方式,對這種運行時才能確定具體實現類的bean,要用Instance的方式注入,使用時要用Instance.get方法取得bean。
  2. 單元測試的BeforeAll注解用于指定測試前要做的事情,這里用System.setProperty設置配置項service.alpha.enabled,所以,理論上SelectBeanConfiguration.tryLookupIfPropertyAlpha方法應該會執行,也就是說注入的TryLookupIfProperty應該是TryLookupIfPropertyAlpha實例,所以testTryLookupIfProperty中用assertEquals斷言預測:TryLookupIfProperty.hello的值來自TryLookupIfPropertyAlpha。
  • 執行單元測試,如下圖,符合預期。

  • 修改BeanInstanceSwitchTest.setUp,將service.alpha.enabled改成service.alpha.enabled,如此理論上SelectBeanConfiguration.tryLookupIfPropertyBeta方法應該會執行,實例化的應該就是TryLookupIfPropertyBeta,那么本次單元測試就不能通過了。
  • 如下圖,果然,注入的實例變成了TryLookupIfPropertyBeta,但是預期的還是之前的TryLookupIfPropertyAlpha,于是測試失敗。

LookupUnlessProperty,配置項的值不符合要求才能使用bean

  • LookupIfProperty的意思是配置項的值符合要求才會創建bean,而LookupUnlessProperty恰好相反,意思是配置項的值不符合要求才能使用bean。
  • 為了驗證LookupUnlessProperty的效果,修改SelectBeanConfiguration.java,只修改tryLookupIfPropertyBeta方法的注解,由從之前的LookupIfProperty改為LookupUnlessProperty,屬性也改為service.alpha.enabled,現在的邏輯是:如果屬性service.alpha.enabled的值是true,就執行tryLookupIfPropertyAlpha,如果屬性service.alpha.enabled的值不是true,就執行tryLookupIfPropertyBeta。
public class SelectBeanConfiguration {

    @LookupIfProperty(name = "service.alpha.enabled", stringValue = "true")
    @ApplicationScoped
    public TryLookupIfProperty tryLookupIfPropertyAlpha() {
        return new TryLookupIfPropertyAlpha();
    }

    @LookupUnlessProperty(name = "service.alpha.enabled", stringValue = "true")
    @ApplicationScoped
    public TryLookupIfProperty tryLookupIfPropertyBeta() {
        return new TryLookupIfPropertyBeta();
    }
}
  • 打開剛才的BeanInstanceSwitchTest.java,setUp方法中將service.alpha.enabled的值設為true。
@BeforeAll
public static void setUp() {
	System.setProperty("service.alpha.enabled", "true");
}
  • 運行單元測試,如下圖,符合預期。

  • 現在把service.alpha.enabled的值設為false,單元測試不通過,提示返回值是TryLookupIfPropertyBeta,這也是符合預期的,證明LookupUnlessProperty已經生效了。

  • 此刻您可能會好奇,如果配置項service.alpha.enabled不存在會如何,咱們將setUp方法中的System.setProperty這段代碼刪除,這樣配置項service.alpha.enabled就不存在了,再次執行單元測試,發現SelectBeanConfiguration類的tryLookupIfPropertyAlpha和tryLookupIfPropertyBeta兩個方法都沒有執行,導致沒有TryLookupIfProperty類型的bean。

  • 這時候您應該發現了一個問題:如果配置項service.alpha.enabled不存在的時候如何返回一個默認bean,以避免找不到bean呢?
  • LookupIfProperty和LookupUnlessProperty都有名為lookupIfMissing的屬性,意思都一樣:指定配置項不存在的時候,就執行注解所修飾的方法,修改SelectBeanConfiguration.java,如下圖黃框所示,增加lookupIfMissing屬性,指定值為true(沒有指定的時候,默認值是false)。

  • 再次運行單元測試,如下圖,盡管service.alpha.enabled不存在,但lookupIfMissing屬性起了作用,SelectBeanConfiguration.tryLookupIfPropertyAlpha方法還是執行了,于是測試通過。

IfBuildProfile,如果是指定的profile才能使用bean

  • 應用在運行時,其profile是固定的,IfBuildProfile檢查當前profile是否是指定值,如果是,其修飾的bean就能被業務代碼使用。
  • 對比官方對LookupIfProperty和IfBuildProfile描述的差別,LookupIfProperty決定了是否能被選擇,IfBuildProfile決定了是否在容器中。
# LookupIfProperty,說的是be obtained by programmatic
Indicates that a bean should only be obtained by programmatic lookup if the property matches the provided value.
# IfBuildProfile,說的是be enabled
the bean will only be enabled if the Quarkus build time profile matches the specified annotation value.
  • 接下來寫代碼驗證,先寫個接口。
public interface TryIfBuildProfile {
    String hello();
}
  • 再寫兩個實現類,第一個是TryIfBuildProfileProd.java。
public class TryIfBuildProfileProd implements TryIfBuildProfile {
    @Override
    public String hello() {
        return "from " + this.getClass().getSimpleName();
    }
}
  • 第二個TryIfBuildProfileDefault.java。
public class TryIfBuildProfileDefault implements TryIfBuildProfile {
    @Override
    public String hello() {
        return "from " + this.getClass().getSimpleName();
    }
}
  • 再來看IfBuildProfile的用法,在剛才的SelectBeanConfiguration.java中新增兩個方法,如下所示,應用運行時,如果profile是test,那么tryIfBuildProfileProd方法會被執行,還要注意的是注解DefaultBean的用法,如果profile不是test,那么quarkus的bean容器中就沒有TryIfBuildProfile類型的bean了,此時DefaultBean修飾的tryIfBuildProfileDefault方法就會被執行,導致TryIfBuildProfileDefault的實例注冊在quarkus容器中。
@Produces
@IfBuildProfile("test")
public TryIfBuildProfile tryIfBuildProfileProd() {
	return new TryIfBuildProfileProd();
}

@Produces
@DefaultBean
public TryIfBuildProfile tryIfBuildProfileDefault() {
	return new TryIfBuildProfileDefault();
}
  • 單元測試代碼寫在剛才的BeanInstanceSwitchTest.java中,運行單元測試是profile被設置為test,所以tryIfBuildProfile的預期是TryIfBuildProfileProd實例,注意,這里和前面LookupIfProperty不一樣的是:這里的TryIfBuildProfile直接注入就好,不需要Instance<T>來注入。
@Inject
TryIfBuildProfile tryIfBuildProfile;

@Test
public void testTryLookupIfProperty() {
	Assertions.assertEquals("from " + TryLookupIfPropertyAlpha.class.getSimpleName(),
                            service.get().hello());
}

@Test
public void tryIfBuildProfile() {
	Assertions.assertEquals("from " + TryIfBuildProfileProd.class.getSimpleName(),
                tryIfBuildProfile.hello());
}
  • 執行單元測試,如下圖,測試通過,紅框顯示當前profile確實是test。

  • 再來試試DefaultBean的是否正常,修改SelectBeanConfiguration.java的代碼,如下圖紅框,將IfBuildProfile注解的值從剛才的test改為prod,如此一來,再執行單元測試時tryIfBuildProfileProd方法就不會被執行了,此時看tryIfBuildProfileDefault方法能否執行。

  • 執行單元測試,結果如下圖,黃框中的內容證明是tryIfBuildProfileDefault方法被執行,也就是說DefaultBean正常工作。

UnlessBuildProfile,如果不是指定的profile才能使用bean

  • UnlessBuildProfile的邏輯與IfBuildProfile相反:如果不是指定的profile才能使用bean。
  • 回顧剛才測試失敗的代碼,如下圖紅框,單元測試的profile是test,下面要求profile必須等于prod,因此測試失敗,現在咱們將紅框中的IfBuildProfile改為UnlessBuildProfile,意思是profile不等于prod的時候bean可以使用。

  • 執行單元測試,如下圖,這一次順利通過,證明UnlessBuildProfile的作用符合預期。

IfBuildProperty,如果構建屬性匹配才能使用bean

  • 最后要提到注解是IfBuildProperty是,此注解與LookupIfProperty類似,下面是兩個注解的官方描述對比,可見IfBuildProperty作用的熟悉主要是構建屬性(前面的文章中提到過構建屬性,它們的特點是運行期間只讀,值固定不變)。
# LookupIfProperty的描述,如果屬性匹配,則此bean可以被獲取使用
Indicates that a bean should only be obtained by programmatic lookup if the property matches the provided value.
# IfBuildProperty的描述,如果構建屬性匹配,則此bean是enabled
the bean will only be enabled if the Quarkus build time property matches the provided value
  • 限于篇幅,就不寫代碼驗證了,來看看官方demo,用法上與LookupIfProperty類似,可以用DefaultBean來兜底,適配匹配失敗的場景。
@Dependent
public class TracerConfiguration {

    @Produces
    @IfBuildProperty(name = "some.tracer.enabled", stringValue = "true")
    public Tracer realTracer(Reporter reporter, Configuration configuration) {
        return new RealTracer(reporter, configuration);
    }

    @Produces
    @DefaultBean
    public Tracer noopTracer() {
        return new NoopTracer();
    }
}
  • 至此,基于多種注解來選擇bean實現的學習已經完成,依靠配置項和profile,已經可以覆蓋多數場景下bean的確認,如果這些不能滿足您的業務需求,接下來的文章咱們繼續了解更多靈活的選擇bean的方式。
責任編輯:姜華 來源: 今日頭條
相關推薦

2023-06-27 08:58:13

quarkusBean

2023-06-29 08:32:41

Bean作用域

2016-03-21 17:08:54

Java Spring注解區別

2023-10-07 08:35:07

依賴注入Spring

2011-05-31 10:00:21

Android Spring 依賴注入

2017-08-16 16:00:05

PHPcontainer依賴注入

2022-12-29 08:54:53

依賴注入JavaScript

2011-04-15 09:44:45

Spring

2009-06-15 17:48:32

Spring注解注入屬性

2021-06-03 07:55:12

技術

2016-10-20 19:36:01

androiddagger2依賴注入

2024-12-30 12:00:00

.NET Core依賴注入屬性注入

2015-09-02 11:22:36

JavaScript實現思路

2022-04-30 08:50:11

控制反轉Spring依賴注入

2019-09-18 18:12:57

前端javascriptvue.js

2022-04-11 09:02:18

Swift依賴注

2014-07-08 14:05:48

DaggerAndroid依賴

2021-02-28 20:41:18

Vue注入Angular

2024-04-01 00:02:56

Go語言代碼

2024-05-27 00:13:27

Go語言框架
點贊
收藏

51CTO技術棧公眾號

亚洲高清在线免费观看| 9.1麻豆精品| 99久久精品免费看国产| 国产精品乱码视频| 成人羞羞网站| 91精品国产电影| 91精品国产一区二区在线观看| 亚洲高清一二三区| 日本无删减在线| 日韩一级黄色片| 老司机精品视频在线观看6| 午夜在线成人av| 精品一二三四| 亚洲电影中文字幕在线观看| 69日小视频在线观看| 国产女主播在线一区二区| 天天夜碰日日摸日日澡性色av| 国产精品亚洲专一区二区三区| 自拍视频一区二区三区| 久久精品国产一区二区三区免费看| 日韩不卡av| 久久99精品国产麻豆不卡| 椎名由奈jux491在线播放| 美女一区二区三区| 中文字幕在线观看一区二区三区| 毛片一区二区三区| 伊人网在线免费| 成人激情综合网站| www一区二区www免费| 久久久精品国产免费观看同学| 最近中文字幕一区二区| 久久久精品国产免费观看同学| 国产成人黄色片| 国产清纯美女被跳蛋高潮一区二区久久w| 中文字幕乱码人妻综合二区三区| 国产精品视频你懂的| 国产在线制服美女| 色婷婷久久综合| 性欧美videos高清hd4k| 亚洲欧美中文日韩在线v日本| 精品久久99| 欧美做爰性生交视频| 仙踪林久久久久久久999| 精品国产乱码久久久久久88av| 美女在线视频一区| 亚洲乱码日产精品bd在线观看| 国产日韩高清在线| 中文字幕国产在线| 日韩女优毛片在线| 不卡亚洲精品| 国产成人亚洲综合青青| 欧美激情日韩| 日本黄网站色大片免费观看| 国产亲近乱来精品视频| 日韩av成人| 日韩电影中文字幕在线| 美女久久精品| 97视频资源在线观看| 久久成人精品无人区| 日本免费观看网站| 在线观看国产日韩| 国产麻豆一区| 亚洲综合精品一区二区| 激情欧美一区二区| 捆绑紧缚一区二区三区在线观看| 欧美日韩色一区| 日韩有码欧美| 99在线高清视频在线播放| 国产精品白丝av| 特黄特黄的视频| 日韩女同互慰一区二区| 精品中国亚洲| 日韩在线第一区| 国产精品久久久久久久久久久免费看 | 欧美13~18sex性hd| 日韩一区二区三区在线| caoporn成人免费视频在线| 国产精品久久精品国产 | 色老汉一区二区三区| 成人免费毛片嘿嘿连载视频…| 成人高h视频在线| 成人免费毛片高清视频| a视频网址在线观看| 久久久久久久成人| 美国一区二区三区在线播放| 在线观看av网| 色综合久久悠悠| 久久高清免费观看| 欧美著名女优| 日韩中文有码在线视频| 日韩精品一级二级 | 亚洲最大成人| 666精品在线| 国产精品理论片在线观看| caoporn-草棚在线视频最| 国产噜噜噜噜噜久久久久久久久 | 一区二区不卡在线视频 午夜欧美不卡' | 国产精品国产亚洲精品看不卡15| 国产亚洲精品7777| 美女高潮在线观看| 97人人香蕉| 亚洲你懂的在线视频| 欧美综合影院| 日韩中文一区二区三区| 在线视频国内一区二区| jiujiure精品视频播放| 中文字幕在线导航| 伊人久久综合97精品| 秋霞av亚洲一区二区三| 一区二区三区视频在线观看视频| 国产精品久久久久久久7电影| 国产日韩v精品一区二区| 成人免费视频观看| 国产精品一区在线免费观看| 欧美三级韩国三级日本三斤| 欧美好骚综合网| 好男人看片在线观看免费观看国语| 精品国产美女在线| 国产一本一道久久香蕉| 韩国成人免费视频| 麻豆精品传媒视频| 欧美日韩高清一区二区不卡| 国产一在线精品一区在线观看| 亚洲高清国产精品| 国产深夜精品福利| 午夜精品久久久久| 国产二区精品| 久久电影视频| 国产一区二区三区高清视频| 欧美中文字幕久久| 黄色精品网站| 看黄网站在线观看| 欧美日本国产精品| 精品99一区二区三区| 麻豆久久精品| h片视频在线观看| av电影一区二区三区| 伊人激情综合网| 久久蜜桃一区二区| 丝袜av一区| 视频一区二区在线播放| 成人欧美一区二区三区黑人免费| 欧美视频日韩视频在线观看| 亚洲精品美女| 92久久精品| 国产精品沙发午睡系列| 久久久久这里只有精品| 亚洲欧美影音先锋| 成人激情在线| 久久99精品久久久久久野外| 欧美性视频在线播放| www日韩中文字幕在线看| 亚洲国产成人一区二区三区| 国产欧美日韩免费观看| 91在线视频免费看| 亚洲欧美日产图| 这里只有视频精品| 国产日韩精品久久久| 欧美伦理在线视频| 日本在线免费看| 台湾无码一区二区| 777午夜精品福利在线观看| 无码av免费一区二区三区试看 | 青草在线视频| 国产一区 在线播放| 午夜精品久久久久久久99黑人| 午夜精品久久久久影视| 裸体一区二区| 免费观看亚洲天堂| 色资源在线观看| 亚洲免费视频一区| 国内精久久久久久久久久人| 在线这里只有精品| 成人小视频免费在线观看| 国产一区二区电影在线观看| 国产三区在线观看| 男女无套免费视频网站动漫| 亚洲一区二区中文字幕| 亚洲欧洲一区二区三区在线观看| 亚洲欧美精品午睡沙发| 日韩电影免费在线看| 国产精品视屏| 麻豆视频在线免费观看| www国产黄色| 99热99热| 欧美老妇交乱视频| 欧美精品在线观看一区二区| 久久久久成人黄色影片| 樱桃成人精品视频在线播放| 激情久久免费视频| 精品美女在线观看视频在线观看| 日韩欧美在线免费观看视频| 久久久久久久久久码影片| 久久久久久综合网天天| 欧美一卡在线观看| 中文字幕中文乱码欧美一区二区| 日韩中文字幕1| 成人情趣视频网站| 国产日韩欧美中文在线| 在线中文字幕-区二区三区四区|