博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot动态调用实现类
阅读量:2241 次
发布时间:2019-05-09

本文共 3584 字,大约阅读时间需要 11 分钟。

因为项目需要,我们有一个功能的接口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 */@Componentpublic 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 */@Componentpublic 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 */@Componentpublic class BeanConfig implements InitializingBean, ApplicationContextAware {
private Map
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
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 */@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(); }}

5、启动运行与结果

访问:http://localhost:8080/get?id=UserReaderImpl1

在这里插入图片描述

访问:http://localhost:8080/get?id=UserReaderImpl2

在这里插入图片描述

转载地址:http://ynhbb.baihongyu.com/

你可能感兴趣的文章
集成测试(一)—— 使用PHP页面请求Spring项目的Java接口数据
查看>>
使用Maven构建的简单的单模块SSM项目
查看>>
Intellij IDEA使用(十四)—— 在IDEA中创建包(package)的问题
查看>>
FastDFS集群架构配置搭建(转载)
查看>>
HTM+CSS实现立方体图片旋转展示效果
查看>>
FFmpeg 命令操作音视频
查看>>
问题:Opencv(3.1.0/3.4)找不到 /opencv2/gpu/gpu.hpp 问题
查看>>
目的:使用CUDA环境变量CUDA_VISIBLE_DEVICES来限定CUDA程序所能使用的GPU设备
查看>>
问题:Mysql中字段类型为text的值, java使用selectByExample查询为null
查看>>
程序员--学习之路--技巧
查看>>
解决问题之 MySQL慢查询日志设置
查看>>
contOS6 部署 lnmp、FTP、composer、ThinkPHP5、docker详细步骤
查看>>
TP5.1模板布局中遇到的坑,配置完不生效解决办法
查看>>
PHPstudy中遇到的坑No input file specified,以及传到linux环境下遇到的坑,模板文件不存在
查看>>
TP5.1事务操作和TP5事务回滚操作多表
查看>>
composer install或composer update 或 composer require phpoffice/phpexcel 失败解决办法
查看>>
TP5.1项目从windows的Apache服务迁移到linux的Nginx服务需要注意几点。
查看>>
win10安装软件 打开时报错 找不到 msvcp120.dll
查看>>
PHPunit+Xdebug代码覆盖率以及遇到的问题汇总
查看>>
PHPUnit安装及使用
查看>>