欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

需求背景

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

實(shí)現(xiàn)步驟

1. 定義接口

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

public interface I {
    void m();
}

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

創(chuàng)建接口 I 的三個(gè)實(shí)現(xiàn)類 A、BC,并使用 @Service 注解將它們注冊(cè)為 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)建服務(wù)工廠類

創(chuàng)建一個(gè)服務(wù)工廠類 ServiceFactory,用于根據(jù) type 參數(shù)獲取對(duì)應(yīng)的實(shí)現(xiàn)類 Bean。若找不到對(duì)應(yīng)的 Bean,則返回一個(gè)默認(rèn)實(shí)現(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) {
            // 這里可以添加默認(rèn)的處理邏輯
            return new I() {
                @Override
                public void m() {
                    System.out.println("Executing default implementation of method m");
                }
            };
        }
    }
}

4. 控制器類(可選)

如果需要通過 HTTP 請(qǐng)求觸發(fā)方法調(diào)用,可以創(chuàng)建一個(gè)控制器類 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. 測(cè)試類

創(chuàng)建一個(gè)測(cè)試類 Application,在 run 方法中測(cè)試不同 type 的服務(wù)調(diào)用,包括一個(gè)不存在的 type 以驗(yàn)證默認(rèn)邏輯。

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

注意事項(xiàng)

  • 不能直接將 @Service 注解加在接口上,因?yàn)榻涌诒旧聿荒鼙粚?shí)例化,無法作為具體的 Bean 被 Spring 容器管理。
  • 在 ServiceFactory 類中,當(dāng)找不到對(duì)應(yīng) type 的 Bean 時(shí),返回的默認(rèn)實(shí)現(xiàn)可以根據(jù)實(shí)際需求進(jìn)行修改和擴(kuò)展。

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

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

相關(guān)文章

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

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

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

    spring-cloud-gateway啟動(dòng)踩坑及解決

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

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

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

    使用springboot的jar包能夠以service方式啟動(dòng)

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

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

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

    Java+OpenCV實(shí)現(xiàn)人臉檢測(cè)并自動(dòng)拍照

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

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

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

    深入理解Spring?Boot中的Flyway

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

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

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

    Nacos配置中心設(shè)計(jì)原理分析

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

最新評(píng)論