springboot如何獲取接口下所有實(shí)現(xiàn)類(lèi)
springboot獲取接口下所有實(shí)現(xiàn)類(lèi)
首先定義一個(gè)接口
public interface LoginUserService { /** * 判斷手機(jī)號(hào)是否允許登錄 * * @param phone 手機(jī)號(hào) * @return 是否允許 */ boolean hasUser(String phone) throws ServiceException; /** * 通過(guò)Phone獲取用戶信息 * * @param phone 手機(jī)號(hào) * @return 用戶信息 */ UserDTO getUserInfoByPhone(String phone) throws LoginException; }
編寫(xiě)實(shí)現(xiàn)類(lèi),三個(gè)
在這點(diǎn)我的登陸接口上繼承了LoginUserService
在運(yùn)行時(shí)需要通過(guò)查找bean,但是又不知道具體需要使用哪個(gè)bean,在這里自定義一個(gè)注解來(lái)標(biāo)記使用哪個(gè)實(shí)現(xiàn)類(lèi)
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface OrgainzationType { LoginTypeEnum value(); }
在實(shí)現(xiàn)類(lèi)上標(biāo)記具體類(lèi)型
通過(guò)service使用bean進(jìn)行查詢想要的實(shí)現(xiàn)類(lèi)
@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)類(lèi) Map<String, LoginUserService> beanMap = applicationContext.getBeansOfType(LoginUserService.class); for (LoginUserService impl : beanMap.values()) { // 獲取注解上的類(lèi)型 OrgainzationType annotation = AnnotationUtils.findAnnotation(impl.getClass(),OrgainzationType.class); // 如果沒(méi)有添加注解則不需要使用 if (Objects.isNull(annotation)) { continue; } serviceMap.put(annotation.value(), impl); } } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }
運(yùn)行時(shí)的類(lèi)
springboot動(dòng)態(tài)調(diào)用實(shí)現(xiàn)類(lèi)
因?yàn)轫?xiàng)目需要,我們有一個(gè)功能的接口UserReader。其他的類(lèi)都是實(shí)現(xiàn)這個(gè)接口。那么會(huì)有多個(gè)實(shí)現(xiàn)UserReader接口的實(shí)現(xiàn)類(lèi)。現(xiàn)在需要在程序 中動(dòng)態(tài)的去調(diào)用不通實(shí)現(xiàn)類(lèi)中的方法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)類(lèi)
1)實(shí)現(xiàn)類(lèi)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 "訪問(wèn)的UserReaderImpl1"; ? ? } }
2)實(shí)現(xiàn)類(lèi) 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 "訪問(wèn)的UserReaderImpl2"; ? ? } }
3、獲取實(shí)現(xiàn)類(lèi)的相關(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)文章
解決spring boot啟動(dòng)掃描不到自定義注解的問(wèn)題
這篇文章主要介紹了解決spring boot啟動(dòng)掃描不到自定義注解的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09springMVC如何對(duì)輸入數(shù)據(jù)校驗(yàn)實(shí)現(xiàn)代碼
數(shù)據(jù)的校驗(yàn)是交互式網(wǎng)站一個(gè)不可或缺的功能,數(shù)據(jù)驗(yàn)證分為客戶端驗(yàn)證和服務(wù)器端驗(yàn)證,這篇文章主要介紹了springMVC如何對(duì)輸入數(shù)據(jù)校驗(yàn),需要的朋友可以參考下2020-10-10一文教會(huì)你如何從0到1搭建一個(gè)SpringBoot項(xiàng)目
今天剛好學(xué)習(xí)到SpringBoot,就順便記錄一下吧,下面這篇文章主要給大家介紹了關(guān)于如何從0到1搭建一個(gè)SpringBoot項(xiàng)目的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01Springmvc異常映射2種實(shí)現(xiàn)方法
這篇文章主要介紹了Springmvc異常映射2種實(shí)現(xiàn)方法以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。,需要的朋友可以參考下2020-05-05Scala之文件讀取、寫(xiě)入、控制臺(tái)操作的方法示例
這篇文章主要介紹了Scala之文件讀取、寫(xiě)入、控制臺(tái)操作的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Java中使用instanceof判斷對(duì)象類(lèi)型的示例
在List<Object>中遍歷Object時(shí),先判斷類(lèi)型,再定向轉(zhuǎn)換,本文給大家介紹Java中使用instanceof判斷對(duì)象類(lèi)型,感興趣的朋友跟隨小編一起看看吧2023-08-08