目录
- 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;
}
编写实现类,三个

在这点我的登陆接口上继承了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;
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

评论(0)