最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot如何獲取接口下所有實現(xiàn)類

 更新時間:2022年09月29日 10:38:08   作者:喝酸奶要舔蓋兒  
這篇文章主要介紹了springboot如何獲取接口下所有實現(xiàn)類問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

springboot獲取接口下所有實現(xiàn)類

首先定義一個接口

public interface LoginUserService {
    /**
     * 判斷手機號是否允許登錄
     *
     * @param phone 手機號
     * @return 是否允許
     */
    boolean hasUser(String phone) throws ServiceException;
    /**
     * 通過Phone獲取用戶信息
     *
     * @param phone 手機號
     * @return 用戶信息
     */
    UserDTO getUserInfoByPhone(String phone) throws LoginException;
}

編寫實現(xiàn)類,三個

在這點我的登陸接口上繼承了LoginUserService

在運行時需要通過查找bean,但是又不知道具體需要使用哪個bean,在這里自定義一個注解來標記使用哪個實現(xiàn)類

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface OrgainzationType {
    LoginTypeEnum value();
}

在實現(xiàn)類上標記具體類型

通過service使用bean進行查詢想要的實現(xiàn)類

@Slf4j
@Service
public class LoginServiceImpl implements LoginService, InitializingBean, ApplicationContextAware {
    private ApplicationContext applicationContext;
    protected Map<LoginTypeEnum, LoginUserService> serviceMap = new HashMap<>();
    @Override
    public void afterPropertiesSet() throws Exception {
        // 查找所有LoginUserService接口的實現(xiàn)類
        Map<String, LoginUserService> beanMap = applicationContext.getBeansOfType(LoginUserService.class);
        for (LoginUserService impl : beanMap.values()) {
        	// 獲取注解上的類型
            OrgainzationType annotation = AnnotationUtils.findAnnotation(impl.getClass(),OrgainzationType.class);
            // 如果沒有添加注解則不需要使用
            if (Objects.isNull(annotation)) {
                continue;
            }
            serviceMap.put(annotation.value(), impl);
        }
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
 }

運行時的類

springboot動態(tài)調(diào)用實現(xiàn)類

因為項目需要,我們有一個功能的接口UserReader。其他的類都是實現(xiàn)這個接口。那么會有多個實現(xiàn)UserReader接口的實現(xiàn)類?,F(xiàn)在需要在程序 中動態(tài)的去調(diào)用不通實現(xiàn)類中的方法getUser()。

下面既是功能實現(xiàn)代碼:

1、添加接口

package com.example.mavenceshi.service;
/**
?* @author by CLP
?* @Classname UserReader
?* @Description
?* @Date 2020/9/8 15:16
?*/
public interface UserReader {
? ? String getUser();
}

2、創(chuàng)建實現(xiàn)類

1)實現(xiàn)類UserReaderImpl1

package com.example.mavenceshi.service.impl;
import com.example.mavenceshi.service.UserReader;
import org.springframework.stereotype.Component;
/**
?* @author by CLP
?* @Classname UserReader1
?* @Description
?* @Date 2020/9/8 15:17
?*/
@Component
public class UserReaderImpl1 implements UserReader {
? ? @Override
? ? public String getUser() {
? ? ? ? ? ?return "訪問的UserReaderImpl1";
? ? }
}

2)實現(xiàn)類 UserReaderImpl2

package com.example.mavenceshi.service.impl;
import com.example.mavenceshi.service.UserReader;
import org.springframework.stereotype.Component;
/**
?* @author by CLP
?* @Classname UserReaderImpl2
?* @Description
?* @Date 2020/9/8 15:18
?*/
@Component
public class UserReaderImpl2 implements UserReader {
? ? @Override
? ? public String getUser() {
? ? ? ? ? return "訪問的UserReaderImpl2";
? ? }
}

3、獲取實現(xiàn)類的相關接口 

package com.example.mavenceshi.config;
import com.example.mavenceshi.service.UserReader;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
/**
 * @author by CLP
 * @Classname BeanConfig
 * @Description
 * @Date 2020/9/8 15:28
 */
@Component
public class BeanConfig implements InitializingBean, ApplicationContextAware {
    private Map<String, UserReader> queryServiceImplMap = new HashMap<>();
    private ApplicationContext applicationContext;
    public UserReader createQueryService(String type) {
        UserReader userReader = queryServiceImplMap.get(type);
        if (userReader == null) {
            return queryServiceImplMap.get("UserReader1Impl");
        }
        return userReader;
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        Map<String, UserReader> beanMap = applicationContext.getBeansOfType(UserReader.class);
        //遍歷該接口的所有實現(xiàn),將其放入map中
        for (UserReader serviceImpl : beanMap.values()) {
            queryServiceImplMap.put(serviceImpl.getClass().getSimpleName(), serviceImpl);
        }
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 教你如何用Java簡單爬取WebMagic

    教你如何用Java簡單爬取WebMagic

    今天給大家?guī)淼氖顷P于Java爬蟲的相關知識,文章圍繞著Java如何爬取WebMagic展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Spring中的底層架構核心概念類型轉(zhuǎn)換器詳解

    Spring中的底層架構核心概念類型轉(zhuǎn)換器詳解

    這篇文章主要介紹了Spring中的底層架構核心概念類型轉(zhuǎn)換器詳解,本文結合示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-12-12
  • java?for循環(huán)內(nèi)執(zhí)行多線程問題

    java?for循環(huán)內(nèi)執(zhí)行多線程問題

    這篇文章主要介紹了java?for循環(huán)內(nèi)執(zhí)行多線程問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 詳解Java ScheduledThreadPoolExecutor的踩坑與解決方法

    詳解Java ScheduledThreadPoolExecutor的踩坑與解決方法

    最近項目上反饋某個重要的定時任務突然不執(zhí)行了,很頭疼,開發(fā)環(huán)境和測試環(huán)境都沒有出現(xiàn)過這個問題。定時任務采用的是ScheduledThreadPoolExecutor,后來一看代碼發(fā)現(xiàn)踩了一個大坑。本文就來和大家聊聊這次的踩坑記錄與解決方法,需要的可以參考一下
    2022-10-10
  • Java實現(xiàn)整數(shù)的逆序輸出的三種方法

    Java實現(xiàn)整數(shù)的逆序輸出的三種方法

    這篇文章主要介紹了Java實現(xiàn)整數(shù)的逆序輸出的三種方法,第一種是無限制整數(shù)的逆序輸出,第二種是非負整數(shù)的逆序輸出,第三種是非特殊情況的逆序輸出,每種方法給大家講解的非常詳細需要的朋友可以參考下
    2022-11-11
  • 基于Java實現(xiàn)無向環(huán)和有向環(huán)的檢測

    基于Java實現(xiàn)無向環(huán)和有向環(huán)的檢測

    這篇文章主要介紹了如何在?Java?中實現(xiàn)無向環(huán)和有向環(huán)的檢測,文中的示例代碼講解詳細,對我們學習Java有一定的幫助,需要的可以參考一下
    2022-04-04
  • Java中關鍵字final finally finalize的區(qū)別介紹

    Java中關鍵字final finally finalize的區(qū)別介紹

    這篇文章主要給大家分享的是 Java中final,finally,finalize 到底有什么區(qū)別,文章圍繞final,finally,finalize的相關資料展開詳細內(nèi)容,具有一定的參考的價值,需要的朋友可以參考一下
    2022-04-04
  • Java關于jar包的知識詳解

    Java關于jar包的知識詳解

    這篇文章主要介紹了Java關于jar包的知識,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03
  • 超全面的SpringBoot面試題含答案

    超全面的SpringBoot面試題含答案

    這篇文章主要收錄了44道面試中經(jīng)常被問的SpringBoot問題,不管你是正在求職的新手還是已經(jīng)工作很久的高手,這篇關于SpringBoot的面試題總結一定會讓你有新的理解,讓我們一起來看看吧
    2023-03-03
  • Mybatis中的游標查詢Cursor(滾動查詢)

    Mybatis中的游標查詢Cursor(滾動查詢)

    這篇文章主要介紹了Mybatis中的游標查詢Cursor(滾動查詢),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01

最新評論

银川市| 长垣县| 得荣县| 积石山| 襄樊市| 黄大仙区| 淳安县| 宜宾县| 南乐县| 瓦房店市| 吕梁市| 青州市| 镇安县| 开封市| 洛浦县| 绵阳市| 尼勒克县| 北川| 上犹县| 三江| 阿拉尔市| 高唐县| 如东县| 嫩江县| 木里| 西昌市| 正阳县| 潍坊市| 义马市| 长宁区| 缙云县| 宁南县| 博客| 文山县| 栾城县| 英吉沙县| 锦州市| 桓仁| 青浦区| 清镇市| 潞城市|