因为项目需要,我们有一个功能的接口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;
}
}
4、写一个controller接口,实现动态调用
package com.example.mavenceshi.controller;
import com.example.mavenceshi.config.BeanConfig;
import com.example.mavenceshi.service.UserReader;
import com.example.mavenceshi.service.impl.UserReaderImpl1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/** * @author by CLP * @Classname controller * @Description * @Date 2020/9/8 15:18 */
@RestController
public class controller {
@Autowired
private UserReaderImpl1 userReader;
@Autowired
private BeanConfig beanConfig;
@GetMapping("/get")
public String get(String id) {
UserReader queryService = beanConfig.createQueryService(id);
System.out.println(queryService.toString());
return queryService.getUser();
}
}
5、启动运行与结果
访问:http://localhost:8080/get?id=UserReaderImpl1
访问:http://localhost:8080/get?id=UserReaderImpl2
原文链接:https://blog.csdn.net/aiming66/article/details/108472109