Spring Boot中如何遍历指定接口下的所有实现类?

2026-05-25 20:571阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计830个文字,预计阅读时间需要4分钟。

Spring Boot中如何遍历指定接口下的所有实现类?

目录 + SpringBoot获取接口下所有实现类 + SpringBoot动态调用实现类 + 1、添加接口 + 2、创建实现类 + 3、获取实现类的相关接口 + SpringBoot获取接口下所有实现类 + 首先定义一个接口 + public interface Login

目录
  • springboot获取接口下所有实现类
  • springboot动态调用实现类
    • 1、添加接口
    • 2、创建实现类
    • 3、获取实现类的相关接口

springboot获取接口下所有实现类

首先定义一个接口

public interface LoginUserService { /** * 判断手机号是否允许登录 * * @param phone 手机号 * @return 是否允许 */ boolean hasUser(String phone) throws ServiceException; /** * 通过Phone获取用户信息 * * @param phone 手机号 * @return 用户信息 */ UserDTO getUserInfoByPhone(String phone) throws LoginException; }

编写实现类,三个

Spring Boot中如何遍历指定接口下的所有实现类?

在这点我的登陆接口上继承了LoginUserService

在运行时需要通过查找bean,但是又不知道具体需要使用哪个bean,在这里自定义一个注解来标记使用哪个实现类

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

在实现类上标记具体类型

通过service使用bean进行查询想要的实现类

@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接口的实现类 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动态调用实现类

因为项目需要,我们有一个功能的接口UserReader。其他的类都是实现这个接口。那么会有多个实现UserReader接口的实现类。现在需要在程序 中动态的去调用不通实现类中的方法getUser()。

下面既是功能实现代码:

1、添加接口

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

2、创建实现类

1)实现类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)实现类 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、获取实现类的相关接口

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); //遍历该接口的所有实现,将其放入map中 for (UserReader serviceImpl : beanMap.values()) { queryServiceImplMap.put(serviceImpl.getClass().getSimpleName(), serviceImpl); } } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。

本文共计830个文字,预计阅读时间需要4分钟。

Spring Boot中如何遍历指定接口下的所有实现类?

目录 + SpringBoot获取接口下所有实现类 + SpringBoot动态调用实现类 + 1、添加接口 + 2、创建实现类 + 3、获取实现类的相关接口 + SpringBoot获取接口下所有实现类 + 首先定义一个接口 + public interface Login

目录
  • springboot获取接口下所有实现类
  • springboot动态调用实现类
    • 1、添加接口
    • 2、创建实现类
    • 3、获取实现类的相关接口

springboot获取接口下所有实现类

首先定义一个接口

public interface LoginUserService { /** * 判断手机号是否允许登录 * * @param phone 手机号 * @return 是否允许 */ boolean hasUser(String phone) throws ServiceException; /** * 通过Phone获取用户信息 * * @param phone 手机号 * @return 用户信息 */ UserDTO getUserInfoByPhone(String phone) throws LoginException; }

编写实现类,三个

Spring Boot中如何遍历指定接口下的所有实现类?

在这点我的登陆接口上继承了LoginUserService

在运行时需要通过查找bean,但是又不知道具体需要使用哪个bean,在这里自定义一个注解来标记使用哪个实现类

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

在实现类上标记具体类型

通过service使用bean进行查询想要的实现类

@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接口的实现类 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动态调用实现类

因为项目需要,我们有一个功能的接口UserReader。其他的类都是实现这个接口。那么会有多个实现UserReader接口的实现类。现在需要在程序 中动态的去调用不通实现类中的方法getUser()。

下面既是功能实现代码:

1、添加接口

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

2、创建实现类

1)实现类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)实现类 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、获取实现类的相关接口

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); //遍历该接口的所有实现,将其放入map中 for (UserReader serviceImpl : beanMap.values()) { queryServiceImplMap.put(serviceImpl.getClass().getSimpleName(), serviceImpl); } } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。