Spring Boot 获取某接口所有实现类

https://blog.csdn.net/qq_39237801/article/details/112299013

使用Spring的getBeansOfType实现接口多实现类的动态调用

https://blog.csdn.net/u012501054/article/details/103927674/?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-0&spm=1001.2101.3001.4242

记一次applicationContext.getBeansOfType方法的应用场景

https://blog.csdn.net/weixin_43568232/article/details/109616116?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-4.control&dist_request_id=1331647.22027.16184783653183445&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-4.control

--https://blog.csdn.net/asdfayw/article/details/75507917

https://www.zhihu.com/question/38597960

绪论
在springboot项目中,为了方便,我们可能需要获取某一个接口下面的所有实现类,根据名称进行匹配使用。

正文
1、ServiceLocator.java

package com.yang.config;

import com.yang.workOrder.service.IRootService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
* explain:获取应用上下文并获取相应的接口实现类
*
* @author yang
* @date 2021/1/5
*/
@Component
public class ServiceLocator implements ApplicationContextAware {

/**
* 用于保存接口实现类名及对应的类
*/
private Map<String, IRootService> map;

/**
* 获取应用上下文并获取相应的接口实现类
* @param applicationContext
* @throws BeansException
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//根据接口类型返回相应的所有bean
map = applicationContext.getBeansOfType(IRootService.class);
}

/**
* 获取所有实现集合
* @return
*/
public Map<String, IRootService> getMap() {
return map;
}

/**
* 获取对应服务
* @param key
* @return
*/
public IRootService getService(String key) {
return map.get(key);
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
2、IRootService.java

package com.yang.workOrder.service;

import com.alibaba.fastjson.JSONObject;
import com.yang.workOrder.entity.WorkOrder;

/**
* explain:基础流程操作服务接口
*
* @author yang
* @date 2021/1/5
*/
public interface IRootService {

/**
* 开始流程
* @param workOrder
* @return
*/
boolean startProcess(WorkOrder workOrder);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
3、RootA001ServiceImpl.java

package com.yang.workOrder.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yang.workOrder.entity.WorkOrder;
import com.yang.workOrder.service.IRootService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
* explain:A_001流程审批实现类
*
* @author yang
* @date 2021/1/5
*/
@Service("A_001")
public class RootA001ServiceImpl implements IRootService {

private static final Logger LOGGER = LoggerFactory.getLogger(RootA001ServiceImpl.class);

@Override
public boolean startProcess(WorkOrder workOrder) {
return false;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
4、RootA002ServiceImpl.java

package com.yang.workOrder.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yang.workOrder.entity.WorkOrder;
import com.yang.workOrder.service.IRootService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
* explain:A_002流程审批实现类
*
* @author yang
* @date 2021/1/5
*/
@Service("A_002")
public class RootA002ServiceImpl implements IRootService {

private static final Logger LOGGER = LoggerFactory.getLogger(RootA002ServiceImpl.class);

@Override
public boolean startProcess(WorkOrder workOrder) {
return false;
}
}

————————————————
版权声明:本文为CSDN博主「3y先生」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_39237801/article/details/112299013

原文地址:https://www.cnblogs.com/zhoading/p/14663454.html