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

SpringBoot根據(jù)參數(shù)動態(tài)調用接口實現(xiàn)類方法

 更新時間:2025年02月27日 08:35:35   作者:AAA_boy  
在?Spring?Boot?開發(fā)中,我們經常會遇到根據(jù)不同參數(shù)調用接口不同實現(xiàn)類方法的需求,本文將詳細介紹如何實現(xiàn)這一功能,有需要的小伙伴可以參考下

在 Spring Boot 開發(fā)中,我們經常會遇到根據(jù)不同參數(shù)調用接口不同實現(xiàn)類方法的需求。本文將詳細介紹如何實現(xiàn)這一功能,并處理當對應實現(xiàn)類不存在時調用默認方法的情況。

需求背景

假設有一個接口 I,它有三個實現(xiàn)類 AB、C,且這三個實現(xiàn)類都使用 @Service 注解注冊到 Spring 容器中,其對應的 Bean 名稱為 type + "Service"。我們需要根據(jù)傳入的參數(shù) type 動態(tài)調用不同實現(xiàn)類的 m 方法,若 type 對應的實現(xiàn)類不存在,則調用默認方法。

實現(xiàn)步驟

1. 定義接口

首先,我們定義接口 I,該接口包含一個 m 方法。

public interface I {
    void m();
}

2. 實現(xiàn)類 A、B、C

創(chuàng)建接口 I 的三個實現(xiàn)類 A、BC,并使用 @Service 注解將它們注冊為 Spring Bean。

import org.springframework.stereotype.Service;

@Service("AService")
public class A implements I {
    @Override
    public void m() {
        System.out.println("Executing method m in class A");
    }
}

@Service("BService")
public class B implements I {
    @Override
    public void m() {
        System.out.println("Executing method m in class B");
    }
}

@Service("CService")
public class C implements I {
    @Override
    public void m() {
        System.out.println("Executing method m in class C");
    }
}

3. 創(chuàng)建服務工廠類

創(chuàng)建一個服務工廠類 ServiceFactory,用于根據(jù) type 參數(shù)獲取對應的實現(xiàn)類 Bean。若找不到對應的 Bean,則返回一個默認實現(xiàn)。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class ServiceFactory {

    @Autowired
    private ApplicationContext applicationContext;

    public I getService(String type) {
        String beanName = type + "Service";
        try {
            return applicationContext.getBean(beanName, I.class);
        } catch (Exception e) {
            // 這里可以添加默認的處理邏輯
            return new I() {
                @Override
                public void m() {
                    System.out.println("Executing default implementation of method m");
                }
            };
        }
    }
}

4. 控制器類(可選)

如果需要通過 HTTP 請求觸發(fā)方法調用,可以創(chuàng)建一個控制器類 MyController。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @Autowired
    private ServiceFactory serviceFactory;

    @GetMapping("/execute")
    public String execute(@RequestParam String type) {
        I service = serviceFactory.getService(type);
        service.m();
        return "Method executed for type: " + type;
    }
}

5. 測試類

創(chuàng)建一個測試類 Application,在 run 方法中測試不同 type 的服務調用,包括一個不存在的 type 以驗證默認邏輯。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    private ServiceFactory serviceFactory;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) {
        I serviceA = serviceFactory.getService("A");
        serviceA.m();

        I serviceB = serviceFactory.getService("B");
        serviceB.m();

        I serviceC = serviceFactory.getService("C");
        serviceC.m();

        I defaultService = serviceFactory.getService("D");
        defaultService.m();
    }
}

代碼解釋

  • 接口 I:定義了一個方法 m,供實現(xiàn)類實現(xiàn)。
  • 實現(xiàn)類 A、BC:分別實現(xiàn)了接口 I 的 m 方法,并使用 @Service 注解注冊為 Bean。
  • 服務工廠類 ServiceFactory:通過 ApplicationContext 根據(jù) type 嘗試獲取對應的實現(xiàn)類 Bean,如果找不到則返回一個匿名內部類實現(xiàn)的默認邏輯。
  • 控制器類 MyController:提供一個 HTTP 接口 /execute,根據(jù)傳入的 type 調用對應的服務方法。
  • 測試類 Application:在 run 方法中測試不同 type 的服務調用,包括一個不存在的 type 以驗證默認邏輯。

注意事項

  • 不能直接將 @Service 注解加在接口上,因為接口本身不能被實例化,無法作為具體的 Bean 被 Spring 容器管理。
  • 在 ServiceFactory 類中,當找不到對應 type 的 Bean 時,返回的默認實現(xiàn)可以根據(jù)實際需求進行修改和擴展。

通過以上步驟,我們可以在 Spring Boot 項目中根據(jù)參數(shù) type 動態(tài)調用接口不同實現(xiàn)類的方法,并處理當對應實現(xiàn)類不存在時調用默認方法的情況。

到此這篇關于SpringBoot根據(jù)參數(shù)動態(tài)調用接口實現(xiàn)類方法的文章就介紹到這了,更多相關SpringBoot動態(tài)調用接口實現(xiàn)類方法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java實現(xiàn)動態(tài)規(guī)劃背包問題

    Java實現(xiàn)動態(tài)規(guī)劃背包問題

    本文主要介紹使用java實現(xiàn)動態(tài)規(guī)劃的背包問題,詳細使用圖文和多種案例進行解析,幫助理解該算法
    2021-06-06
  • spring-cloud-gateway啟動踩坑及解決

    spring-cloud-gateway啟動踩坑及解決

    這篇文章主要介紹了spring-cloud-gateway啟動踩坑及解決方案,具有很好的參考價值,希望對大家有所幫助。
    2021-08-08
  • Log4j不同模塊輸出到不同的文件中

    Log4j不同模塊輸出到不同的文件中

    這篇文章主要介紹了Log4j不同模塊輸出到不同的文件中 的相關資料,需要的朋友可以參考下
    2016-08-08
  • 使用springboot的jar包能夠以service方式啟動

    使用springboot的jar包能夠以service方式啟動

    這篇文章主要介紹了使用springboot的jar包能夠以service方式啟動,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java實現(xiàn)Jar文件的遍歷復制與文件追加

    Java實現(xiàn)Jar文件的遍歷復制與文件追加

    這篇文章主要為大家詳細介紹了如何利用Java實現(xiàn)Jar文件的遍歷復制與文件追加功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-11-11
  • Java+OpenCV實現(xiàn)人臉檢測并自動拍照

    Java+OpenCV實現(xiàn)人臉檢測并自動拍照

    這篇文章主要為大家詳細介紹了Java+OpenCV實現(xiàn)人臉檢測,并調用筆記本攝像頭實時抓拍,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • java中的Io(input與output)操作總結(四)

    java中的Io(input與output)操作總結(四)

    前面已經把java io的主要操作講完了,這一節(jié)我們來說說關于java io的其他內容:Serializable序列化/DataOutputStream和DataInputStream類/管道流等等,感興趣的朋友可以了解下
    2013-01-01
  • 深入理解Spring?Boot中的Flyway

    深入理解Spring?Boot中的Flyway

    Flyway將數(shù)據(jù)庫結構的變更定義為一系列遷移腳本,通常是SQL腳本文件,當應用程序啟動時,F(xiàn)lyway會自動檢測并執(zhí)行未應用的遷移腳本,將數(shù)據(jù)庫升級到最新版本,這篇文章主要介紹了深入理解Spring?Boot中的Flyway,需要的朋友可以參考下
    2024-01-01
  • 淺談MyBatis原生批量插入的坑與解決方案

    淺談MyBatis原生批量插入的坑與解決方案

    本文主要介紹了淺談MyBatis原生批量插入的坑與解決方案,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • Nacos配置中心設計原理分析

    Nacos配置中心設計原理分析

    今天分享一下Nacos配置變更的相關知識點,現(xiàn)在使用Java生態(tài)如果使用微服務,如果部署在K8s上,那么可能會使用ConfigMap來存儲配置文件,如果沒有使用K8s,那么基本上都使用Nacos來做配置中心,所以有必要了解一下Nacos的配置的知識點,本文只是對其中的部分實現(xiàn)原理進行分析
    2023-10-10

最新評論