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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
springMVC如何對輸入數(shù)據(jù)校驗實現(xiàn)代碼
數(shù)據(jù)的校驗是交互式網(wǎng)站一個不可或缺的功能,數(shù)據(jù)驗證分為客戶端驗證和服務器端驗證,這篇文章主要介紹了springMVC如何對輸入數(shù)據(jù)校驗,需要的朋友可以參考下2020-10-10