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

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

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

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

首先定義一個(gè)接口

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

編寫實(shí)現(xiàn)類,三個(gè)

在這點(diǎn)我的登陸接口上繼承了LoginUserService

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

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

在實(shí)現(xiàn)類上標(biāo)記具體類型

通過service使用bean進(jìn)行查詢想要的實(shí)現(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接口的實(shí)現(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;
    }
 }

運(yùn)行時(shí)的類

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

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

下面既是功能實(shí)現(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)建實(shí)現(xiàn)類

1)實(shí)現(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)實(shí)現(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、獲取實(shí)現(xiàn)類的相關(guā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);
        //遍歷該接口的所有實(shí)現(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;
    }
}

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

相關(guān)文章

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

    教你如何用Java簡單爬取WebMagic

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

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

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

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

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

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

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

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

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

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

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

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

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

    Java關(guān)于jar包的知識詳解

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

    超全面的SpringBoot面試題含答案

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

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

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

最新評論

论坛| 湖南省| 浪卡子县| 日照市| 保定市| 海原县| 泽州县| 吐鲁番市| 左权县| 和田县| 九江市| 普兰店市| 札达县| 抚顺县| 万盛区| 邳州市| 拉孜县| 体育| 淮北市| 时尚| 灌云县| 波密县| 西贡区| 镇江市| 新安县| 青海省| 怀宁县| 锡林浩特市| 江源县| 扶余县| 伊通| 横峰县| 雅安市| 平舆县| 临朐县| 象山县| 灌云县| 贡山| 金塔县| 皋兰县| 留坝县|