本文共 3584 字,大约阅读时间需要 11 分钟。
因为项目需要,我们有一个功能的接口UserReader。其他的类都是实现这个接口。那么会有多个实现UserReader接口的实现类。现在需要在程序 中动态的去调用不通实现类中的方法getUser()。下面既是功能实现代码:
package com.example.mavenceshi.service;/** * @author by CLP * @Classname UserReader * @Description * @Date 2020/9/8 15:16 */public interface UserReader { String getUser();}
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 */@Componentpublic class UserReaderImpl1 implements UserReader { @Override public String getUser() { return "访问的UserReaderImpl1"; }}
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 */@Componentpublic class UserReaderImpl2 implements UserReader { @Override public String getUser() { return "访问的UserReaderImpl2"; }}
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 */@Componentpublic class BeanConfig implements InitializingBean, ApplicationContextAware { private MapqueryServiceImplMap = 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 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; }}
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 */@RestControllerpublic 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(); }}
访问:http://localhost:8080/get?id=UserReaderImpl1
访问:http://localhost:8080/get?id=UserReaderImpl2
转载地址:http://ynhbb.baihongyu.com/