乾貨分享:小技巧大用處之Bean管理類工廠多種實現方式

来源:https://www.cnblogs.com/zuowj/archive/2022/07/30/16534102.html
-Advertisement-
Play Games

前言:最近幾個月很忙,都沒有時間寫文章了,今天周末剛好忙完下班相對早點(20:00下班)就在家把之前想總結的知識點寫出來,於是就有了這篇文章。雖無很高深的技術,但小技巧有大用處。 有時我們經常需要將實現了某個基類或某個介面的所有Bean進行分類管理,在需要用到的時候按需獲取實現了某個基類或某個介面的 ...


前言:最近幾個月很忙,都沒有時間寫文章了,今天周末剛好忙完下班相對早點(20:00下班)就在家把之前想總結的知識點寫出來,於是就有了這篇文章。雖無很高深的技術,但小技巧有大用處。

有時我們經常需要將實現了某個基類或某個介面的所有Bean進行分類管理,在需要用到的時候按需獲取實現了某個基類或某個介面的Bean實例對象,那麼我們就需要Bean管理類工廠(即:工廠模式),實現Bean管理類工廠我總結了目前已知且常用的實現方式,敬請各位看官欣賞,如是不足或更好建議歡迎評論區留言指正,謝謝!

為了便於演示,我先自定義如下介面:

/**
 * @author zuowenjun
 *  <pre>www.zuowenjun.cn</pre>
 */
public interface IDemo {
    String getValue();
    int doFor();
}

然後定義3個實現了上述介面的Service Bean類:(註意到Bean類上方還有@DemoFactoryNeedBean這個先不用管,後面的方式中會有用到)

@Service
public class DemoService1 implements IDemo {

    @Override
    public String getValue() {
        return "DemoService1.getValue by 夢在旅途 zuowj.cnblogs.com";
    }

    @Override
    public int doFor() {
        return 1;
    }
}

@DemoFactoryNeedBean
@Service
public class DemoService2 implements IDemo {

    @Override
    public String getValue() {
        return "DemoService2.getValue by 夢在旅途 zuowj.cnblogs.com";
    }

    @Override
    public int doFor() {
        return 2;
    }
}

@DemoFactoryNeedBean
@Service
public class DemoService3 implements IDemo {

    @Override
    public String getValue() {
        return "DemoService3.getValue by 夢在旅途 zuowj.cnblogs.com";
    }

    @Override
    public int doFor() {
        return 3;
    }
}

下麵直接無廢話列舉各種實現方式

  1. 實現方式一:直接使用集合的依賴註入方式(利用spring註入時會判斷是否為集合,若為集合則獲取所有實現了該類的BEAN集合併進行註入)

    /**
     * @author zuowenjun
     *  <pre>www.zuowenjun.cn</pre>
     */
    @Service
    public class DemoFactory1 {
    
        @Autowired
        private List<IDemo> demos;
    
        public IDemo getOne(int index){
            return demos.stream().filter(d->d.doFor()==index).findFirst().orElseThrow(()->new IllegalArgumentException("not found demo bean"));
        }
    }
    

    單元測試【DemoFactory1】BEAN管理工廠用法及結果:

        @Autowired
        private DemoFactory1 demoFactory1;
        
            @Test
        public void testDemoFactory1(){
            for (int i=1;i<=3;i++){
                    IDemo demo = demoFactory1.getOne(i);
                    System.out.printf("testDemoFactory1--bean class: %s , getValue:%s,  doFor:%d %n", demo.getClass().getSimpleName(), demo.getValue(), demo.doFor());
            }
        }
    

    運行結果:

    testDemoFactory1--bean class: DemoService1 , getValue:DemoService1.getValue by 夢在旅途 zuowj.cnblogs.com, doFor:1
    testDemoFactory1--bean class: DemoService2 , getValue:DemoService2.getValue by 夢在旅途 zuowj.cnblogs.com, doFor:2
    testDemoFactory1--bean class: DemoService3 , getValue:DemoService3.getValue by 夢在旅途 zuowj.cnblogs.com, doFor:3

  2. 實現方式二:通過實現BeanPostProcessor介面,利用每個BEAN實例化後均會調用postProcessAfterInitialization方法的特點,直接在postProcessAfterInitialization方法中收集所需的BEAN實例並添加到集合中

    /**
     * @author zuowenjun
     *  <pre>www.zuowenjun.cn</pre>
     */
    @Service
    public class DemoFactory2 implements BeanPostProcessor {
    
        private List<IDemo> demos=new ArrayList<>();
    
        public IDemo getOne(int index){
            return demos.stream().filter(d->d.doFor()==index).findFirst().orElseThrow(()->new IllegalArgumentException("not found demo bean"));
        }
    
        @Override
        @Nullable
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof IDemo) {
                System.out.printf("postProcessAfterInitialization->bean class:%s",bean.getClass().getSimpleName());
                demos.add((IDemo) bean);
            }
            return bean;
        }
    }
    

    單元測試【DemoFactory2】BEAN管理工廠用法及結果:

        @Autowired
        private DemoFactory2 demoFactory2;
        
            @Test
        public void testDemoFactory2(){
            for (int i=1;i<=3;i++){
                IDemo demo= demoFactory2.getOne(i);
                System.out.printf("testDemoFactory2--bean class: %s , getValue:%s,  doFor:%d %n",demo.getClass().getSimpleName(),demo.getValue(),demo.doFor());
            }
        }
    

    運行結果:

    testDemoFactory2--bean class: DemoService1 , getValue:DemoService1.getValue by 夢在旅途 zuowj.cnblogs.com, doFor:1
    testDemoFactory2--bean class: DemoService2 , getValue:DemoService2.getValue by 夢在旅途 zuowj.cnblogs.com, doFor:2
    testDemoFactory2--bean class: DemoService3 , getValue:DemoService3.getValue by 夢在旅途 zuowj.cnblogs.com, doFor:3

  3. 實現方式三:通過實現ApplicationRunner、ApplicationContextAware介面,以便在setApplicationContext能獲取到上下文實例對象並保存,然後在spring初始化完成執行run方法中使用上下文實例對象獲取指定類型的BEAN實例集合。當然也可以不用實現ApplicationRunner介面,而是在工廠方法獲取BEAN對象第一次時才用上下文實例對象獲取指定類型的BEAN實例集合(即:初始化一次)如代碼中的getOneForLazy方法所示。

    /**
     * @author zuowenjun
     *  <pre>www.zuowenjun.cn</pre>
     */
    @Service
    public class DemoFactory3 implements ApplicationRunner, ApplicationContextAware {
    
        private ApplicationContext context;
    
        @Autowired
        private List<IDemo> demos;
    
        public IDemo getOne(int index) {
            return demos.stream().filter(d -> d.doFor() == index).findFirst().orElseThrow(() -> new IllegalArgumentException("not found demo bean"));
        }
    
        public IDemo getOneForLazy(int index) {
            if (CollectionUtils.isEmpty(demos)){
                demos = new ArrayList<>(context.getBeansOfType(IDemo.class).values());
            }
            return demos.stream().filter(d -> d.doFor() == index).findFirst().orElseThrow(() -> new IllegalArgumentException("not found demo bean"));
        }
    
    
        @Override
        public void run(ApplicationArguments args) throws Exception {
            demos = new ArrayList<>(context.getBeansOfType(IDemo.class).values());
        }
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.context = applicationContext;
        }
    }
    

    單元測試【DemoFactory3】BEAN管理工廠用法及結果:

        @Autowired
        private DemoFactory3 demoFactory3;
    
        @Test
        public void testDemoFactory3(){
            for (int i=1;i<=3;i++){
                IDemo demo= demoFactory3.getOne(i);
                System.out.printf("testDemoFactory3--bean class: %s , getValue:%s,  doFor:%d %n",demo.getClass().getSimpleName(),demo.getValue(),demo.doFor());
            }
        }
    

    運行結果:

    testDemoFactory3--bean class: DemoService1 , getValue:DemoService1.getValue by 夢在旅途 zuowj.cnblogs.com, doFor:1
    testDemoFactory3--bean class: DemoService2 , getValue:DemoService2.getValue by 夢在旅途 zuowj.cnblogs.com, doFor:2
    testDemoFactory3--bean class: DemoService3 , getValue:DemoService3.getValue by 夢在旅途 zuowj.cnblogs.com, doFor:3

  4. 實現方式四:此為組合模式,先定義註入ApplicationContext上下文對象,然後定義一個枚舉類,在枚舉類中為每個枚舉項都指明BEAN的實現類型,最後需要獲取BEAN實例時,直接根據上下文對象獲取指定類型的BEAN實例即可。

    
    /**
     * @author zuowenjun
     * <pre>www.zuowenjun.cn</pre>
     */
    @Service
    public class DemoFactory4 {
    
        private final ApplicationContext context;
    
        public DemoFactory4(ApplicationContext context) {
            this.context = context;
        }
    
        public IDemo getOne(DemoFactory4Enum factory4Enum) {
            return context.getBean(factory4Enum.getBeanClass());
        }
    
    
        public enum DemoFactory4Enum {
            Demo1(1, DemoService1.class),
            Demo2(2, DemoService2.class),
            Demo3(3, DemoService3.class),
            ;
    
            private final Class<? extends IDemo> beanClass;
            private final int index;
    
            DemoFactory4Enum(int i, Class<? extends IDemo> beanClass) {
                this.index = i;
                this.beanClass = beanClass;
            }
    
            public Class<? extends IDemo> getBeanClass() {
                return beanClass;
            }
    
            public int getIndex() {
                return index;
            }
    
            public static DemoFactory4Enum parse(int i){
                return Arrays.stream(values()).filter(d->d.getIndex()==i).findFirst().orElseThrow(()->new IllegalArgumentException("not found enum item!"));
            }
    
        }
    }
    

    單元測試【DemoFactory4】BEAN管理工廠用法及結果:(演示了2種方式,當然本質都是先確定枚舉項,再獲取BEAN對象)

        @Autowired
        private DemoFactory4 demoFactory4;
        
            @Test
        public void testDemoFactory4(){
    //        for (DemoFactory4.DemoFactory4Enum enumItem:DemoFactory4.DemoFactory4Enum.values()){
    //            IDemo demo= demoFactory4.getOne(enumItem);
    //            System.out.printf("testDemoFactory4--bean class: %s , getValue:%s,  doFor:%d %n",demo.getClass().getSimpleName(),demo.getValue(),demo.doFor());
    //        }
    
            for (int i=1;i<=3;i++){
                IDemo demo= demoFactory4.getOne(DemoFactory4.DemoFactory4Enum.parse(i));
                System.out.printf("testDemoFactory4--bean class: %s , getValue:%s,  doFor:%d %n",demo.getClass().getSimpleName(),demo.getValue(),demo.doFor());
            }
        }
    

    運行結果:

    testDemoFactory4--bean class: DemoService1 , getValue:DemoService1.getValue by 夢在旅途 zuowj.cnblogs.com, doFor:1
    testDemoFactory4--bean class: DemoService2 , getValue:DemoService2.getValue by 夢在旅途 zuowj.cnblogs.com, doFor:2
    testDemoFactory4--bean class: DemoService3 , getValue:DemoService3.getValue by 夢在旅途 zuowj.cnblogs.com, doFor:3

  5. 實現方式五:此為組合模式,與實現方式四有點類似,但又有不同,仍然是先定義註入ApplicationContext上下文對象,然後定義一個抽象枚舉類(有一個抽象方法,如:getBean),在枚舉類中為每個枚舉項都實現這個抽象方法,在抽象方法中通過靜態上下文對象欄位來獲取指定類型的BEAN實例,最後需要獲取BEAN實例就比較簡單了,只要得到枚舉項,就可以直接獲取到對應的BEAN實例。

    
    /**
     * @author zuowenjun
     *  <pre>www.zuowenjun.cn</pre>
     */
    @Service
    public class DemoFactory5 {
    
        private static ApplicationContext context;
    
        public DemoFactory5(ApplicationContext context) {
            DemoFactory5.context = context;
        }
    
        public enum DemosEnum {
            Demo1(1) {
                @Override
                public IDemo getBean() {
                    return context.getBean(DemoService1.class);
                }
            },
            Demo2(2) {
                @Override
                public IDemo getBean() {
                    return context.getBean(DemoService2.class);
                }
            },
            Demo3(3) {
                @Override
                public IDemo getBean() {
                    return context.getBean(DemoService3.class);
                }
            },
            ;
    
            private final int index;
    
            DemosEnum(int index) {
                this.index = index;
            }
    
            public int getIndex() {
                return index;
            }
    
            public abstract IDemo getBean();
    
    
            public static DemosEnum parse(int i){
                return Arrays.stream(values()).filter(d->d.getIndex()==i).findFirst().orElseThrow(()->new IllegalArgumentException("not found enum item!"));
            }
    
        }
    }
    

    單元測試【DemoFactory5】BEAN管理工廠用法及結果:(演示了2種方式,當然本質都是先確定枚舉項,再獲取BEAN對象)

        @Test
        public void testDemoFactory5(){
    //        for (DemoFactory5.DemosEnum demosEnum:DemoFactory5.DemosEnum.values()){
    //            IDemo demo= demosEnum.getBean();
    //            System.out.printf("testDemoFactory5--bean class: %s , getValue:%s,  doFor:%d %n",demo.getClass().getSimpleName(),demo.getValue(),demo.doFor());
    //        }
    
            for (int i=1;i<=3;i++){
                IDemo demo= DemoFactory5.DemosEnum.parse(i).getBean();
                System.out.printf("testDemoFactory5--bean class: %s , getValue:%s,  doFor:%d %n",demo.getClass().getSimpleName(),demo.getValue(),demo.doFor());
            }
        }
    

    運行結果:

    testDemoFactory5--bean class: DemoService1 , getValue:DemoService1.getValue by 夢在旅途 zuowj.cnblogs.com, doFor:1
    testDemoFactory5--bean class: DemoService2 , getValue:DemoService2.getValue by 夢在旅途 zuowj.cnblogs.com, doFor:2
    testDemoFactory5--bean class: DemoService3 , getValue:DemoService3.getValue by 夢在旅途 zuowj.cnblogs.com, doFor:3

  6. 實現方式六:其實本質還是實現方式一的靈活應用,通過自定義標註了@Qualifier註解的過濾註解類(如:@DemoFactoryNeedBean),然後在對應的BEAN類上加上該自定義的過濾註解,最後在工廠類的內部集合依賴註入欄位上同樣增加自定義的過濾註解,這樣就可以在原有的基礎上(BEAN的基類或介面)增加過濾必需包含指明瞭自定義過濾註解的BEAN實例集合。

    @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Qualifier
    public @interface DemoFactoryNeedBean {
    }
    
    /**
     * @author zuowenjun
     *  <pre>www.zuowenjun.cn</pre>
     */
    @Service
    public class DemoFactory1 {
    
        @DemoFactoryNeedBean
        @Autowired
        private List<IDemo> demos;
    
        public IDemo getOne(int index){
            return demos.stream().filter(d->d.doFor()==index).findFirst().orElseThrow(()->new IllegalArgumentException("not found demo bean"));
        }
    
        public boolean hasBean(int index){
            return demos.stream().anyMatch(d->d.doFor()==index);
        }
    }
    

    然後再看文章開頭定義的3個BEAN類,其中:DemoService2、DemoService3是有加@DemoFactoryNeedBean註解的,最後再次單元測試【DemoFactory1】BEAN管理工廠用法及結果:

        @Test
        public void testDemoFactory1(){
            for (int i=1;i<=3;i++){
                if (demoFactory1.hasBean(i)) {
                    IDemo demo = demoFactory1.getOne(i);
                    System.out.printf("testDemoFactory1--bean class: %s , getValue:%s,  doFor:%d %n", demo.getClass().getSimpleName(), demo.getValue(), demo.doFor());
                }
            }
        }
    

    運行結果:(少了DemoService1 的BEAN)

    testDemoFactory1--bean class: DemoService2 , getValue:DemoService2.getValue by 夢在旅途 zuowj.cnblogs.com, doFor:2
    testDemoFactory1--bean class: DemoService3 , getValue:DemoService3.getValue by 夢在旅途 zuowj.cnblogs.com, doFor:3

    好了, 以上就是全部的實現方式了,至於哪種更好,我認為在不同的場景下選擇合適的實現方式即可,沒有所謂的最好,存在即有意義,最後期待我下次再寫新的博文吧!~


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 為什麼要參加軟考: 軟考全稱是電腦技術與軟體專業技術資格考試,學生可以為畢業後面試錦上添花,已參加工作且不是本專業的拿個證在一定程度上彌補不是本專業的劣勢。如果你要往架構師、項目經理等晉升,有些企業面試會有證優先。另外評職稱、積分落戶等都一定用處。本人屬於已參加工作。 —————————————— ...
  • 1、引例 【例1】 分析該程式,有哪些問題 int main() { swap(int p, int q); int a = 10, b = 20; printf("(1)a = %d, b = %d\n", a, b); swap(&a, &b); printf("(2)a = %d, b = % ...
  • 精華筆記: 1. 運算符: - 算術:+、-、*、/、%、++、-- - 關係:>、<、>=、<=、==、!= - 邏輯:&&、||、! - 賦值:=、+=、-=、*=、/=、%= - 字元串連接:+ - 條件/三目:boolean?數1:數2 2. 分支結構:基於條件執行的語句 - if結構:1條 ...
  • 精華筆記: 1. 變數:存數的 - 聲明: 在銀行開了個帳戶 - 初始化: 給帳戶存錢 - 使用: 使用的是帳戶裡面的錢 - 對變數的使用就是對它所存的那個數的使用 - 變數在使用之前必須聲明並初始化 - 命名: - 只能包含字母、數字、_和$符,並且不能以數字開頭 - 嚴格區分大小寫 - 不能使用 ...
  • 一、python簡介:是什麼,特點是什麼,有什麼用 1、python是什麼? python是一門結合解釋型,編譯性,互動性和麵向對象的腳本語言,具有很強的可讀性,相比其他例如Java語言,C語言更加容易入門。 2、python有哪些特點? 易於學習:python有相對較少的關鍵字,結構簡單,和一個明 ...
  • JetBrAIns GoLand 是Mac os系統上由JetBrAIns推出的一個GO語言集成開發工具環境,基於IntelliJ平臺,支持JetBrAIns插件體系,擁有針對GO語言的代碼助手、代碼編輯器、代碼調試等工具,支持前端和後端開發,並且支持IntelliJ插件,可以大大提高Go語言開發者 ...
  • 配置不同生產環境 本文適用於開發環境下需要打包項目至生產環境,避免開發環境的配置文件泄露。 設置maven 作用:1. 手動調節運行時的不同環境 2. 打包時可以不會有其它環境的文件 註:每次換環境前(打包前)記得手動clean清楚,因為idea不會在換環境後自動清除另一個環境的文件 在pom文件中 ...
  • 介紹 本篇文章主要針對於電腦二級考試的崽崽,當然想瞭解Python和學習Python的崽崽也是可以看本篇文章的;畢竟,手機和電腦都可以運行Python;本篇我文章雖然是筆記,但是也純靠手打,希望關註和點贊一下,期待我的其他隨筆和文章;文章作者由博客園狐小妖用戶撰寫,非來自於博客園且不帶轉載註明,均 ...
一周排行
    -Advertisement-
    Play Games
  • 人臉識別技術在現代社會中扮演著越來越重要的角色,比如人臉識別門禁、人臉識別支付、甚至人臉識別網站登錄等。 最近有群友問.NET有沒有人臉識別的組件,小編查閱相關資料介紹下麵幾種.NET人臉識別組件供大家參考。 **1、Microsoft Azure Face API** 簡介:Microsoft A ...
  • # 1. 與 .NET Core 緩存的關係和差異 ABP 框架中的緩存系統核心包是 [Volo.Abp.Caching](https://www.nuget.org/packages/Volo.Abp.Caching) ,而對於分散式緩存的支持,abp 官方提供了基於 Redis 的方案,需要安裝 ...
  • 最近ET做熱更重載dll的時候,返回登陸會重新檢測新的dll,首次登錄之前已經Assembly.Load()過一次dll,第二次返回登陸再次load dll到記憶體中,Invoke執行方法的時候,異常了,有些方法執行了,有些未執行,於是查資料,看到些老資料說Assembly.Load重覆載入同名dll ...
  • 1. 擴展方法 擴展方法使你能夠向現有類型“添加”方法,而無需創建新的派生類型、重新編譯或以其他方式修改原始類型。 擴展方法是一種靜態方法,但可以像擴展類型上的實例方法一樣進行調用。 對於用 C#、F# 和 Visual Basic 編寫的客戶端代碼,調用擴展方法與調用在類型中定義的方法沒有明顯區別 ...
  • 以前在隨筆《Winform開發框架之客戶關係管理系統(CRM)的開發總結系列1-界面功能展示 》的幾篇隨筆中介紹過基於WInform開發框架開發的CRM系統,系統的功能主要也是圍繞著客戶相關信息來進行管理的。本篇隨筆介紹在最新的《SqlSugar開發框架》中整合CRM系統模塊的功能。 ...
  • 隨著技術的發展,ASP.NET Core MVC也推出了好長時間,經過不斷的版本更新迭代,已經越來越完善,本系列文章主要講解ASP.NET Core MVC開發B/S系統過程中所涉及到的相關內容,適用於初學者,在校畢業生,或其他想從事ASP.NET Core MVC 系統開發的人員。 經過前幾篇文章... ...
  • [toc] 這篇文章是我之前總結的一篇文章,因為整理博客的原因,原有博客已經註銷,但這篇文章對一些讀者很有用,所以現在新瓶裝舊酒重新整理回來分享給大家。 最近一段時間生產環境頻繁出問題,每次都會生成一個hs_err_pid*.log文件,因為工作內容的原因,在此之前並沒有瞭解過相關內容,趁此機會學習 ...
  • # 前言 在上一篇文章中,給大家講解了泛型的概念、作用、使用場景,以及泛型集合、泛型介面和泛型類的用法,但受限於篇幅,並沒有把泛型的內容講解完畢。所以今天我們會繼續學習泛型方法、泛型擦除,以及通配符等的內容,希望大家繼續做好學習的準備哦。 *** 全文大約【**4600】** 字,不說廢話,只講可以 ...
  • 昨天遇到參數key大小寫不一致導致校驗簽名失敗的問題,查了很長時間才找到原因。看了一下FastJson源碼,發現JSON.toObject中轉換成對象的時候會忽略大小寫。 所以,當使用了JSON.toObject將json轉成Java對象後,再用JSON.toObject轉成json,key值就變了 ...
  • 基於java的線上商城設計與實現,線上購物平臺,校園購物商城,商品銷售平臺,基於Java的電商平臺;電商平臺,買家和賣家可以在此平臺上進行銷售和交易,節約了大量的線下時間成本,購物車的功能,校園交易平臺等等; ...