**
一、通过枚举类配置实现调用
**
1、枚举类
public enum TestEnums {
C00001("接口A","com.xxx.impl.CodeProcessorImplA"),
C00002("接口B","com.xxx.impl.CodeProcessorImplB");
private final String desc;
private final String className;
TestEnums(String desc, String className) {
this.desc = desc;
this.className = className;
}
public String getDesc() {
return desc;
}
public String getClassName() {
return className;
}
}
2、编码接收层Service
public interface CodeReceiverService {
String process(String code);
}
3、编码接收层Impl
import org.springframework.beans.factory.annotation.Autowired;
@Service
public class CodeReceiverServiceImpl implements CodeReceiverService {
@Autowired
private ServiceFactory serviceFactory;
@Override
public String process(String code) {
TestEnums testEnums = TestEnums.valueOf(code);
CodeProcessorService service = serviceFactory.getService(testEnums);
return service.execute(code);
}
}
4、工厂类方法:获取对应已经初始化的Bean
@Component
public class ServiceFactory {
@Autowired
private ApplicationContext context;
public CodeProcessorService getService(TestEnums code) {
try {
Class<?> aClass = Class.forName(code.getClassName());
//注意:return (CodeProcessorService ) clazz.newInstance();
//使用Class.newInstance()方法生成实例后,其内部的@Autowired注解不会生效,所以注入的值会为null,导致获取不到属性信息,
//@Autowired注解依赖于Spring的IoC容器来解析并注入对应的Bean,而使用Class.newInstance()是直接调用类的无参构造函数来实例化对象,生成实例则绕过了Spring的依赖注入环节,从而不会进行任何初始化操作。
return (CodeProcessorService) context.getBean(aClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
6、逻辑处理层Service
public interface CodeProcessorService {
String execute(String code);
}
6、C00001编码对应实现类A
@Service
public class CodeProcessorImplA implements CodeProcessorService {
@Override
public String execute(String code) {
return "==========CodeProcessorImplA==========";
}
}
7、C00002编码对应实现类B
@Service
public class CodeProcessorImplB implements CodeProcessorService {
@Override
public String execute(String code) {
return "==========CodeProcessorImplB==========";
}
}
测试代码
String C00001= codeReceiverService.process("COO0O1");
System.out.println(C00001);
二、通过构造函数、Spring容器实现
public class TestController {
/**
目前不涉及线程安全问题,如果后续其他地方有修改操作,可使用:
private final Map<String, TestService> interfaceMap = new ConcurrentHashMap<>();
*/
private final Map<String, TestService> interfaceMap = new HashMap<>();
//===========================构造函数=====================================
@Autowired
public TestController(TestService[] services) {
for (TestService service : services) {
if (service.supportType() != null) {
interfaceMap.put(service.supportType(), service);
}
}
if (interfaceMap.isEmpty()) {
log.error("处理器初始化失败");
}
}
//===========================容器=====================================
@Resource
private ApplicationContext applicationContext;
@PostConstruct
public void init() {
Map<String, TestService> map = applicationContext.getBeansOfType(TestService.class);
map.values().forEach(service -> {
if (!Objects.isNull(service.supportType())){
interfaceMap.put(service.supportType(), service);
}
});
if (interfaceMap.isEmpty()){
log.error("处理器初始化失败");
}
}
//===========================interfaceMap根据实际使用位置引用===================================
@PostMapping(value = "/execute", produces = {"application/json;charset=UTF-8"})
public void executeService(@RequestBody TestDto test) {
TestService testService = interfaceMap.get(test.getType());
if (testService != null) {
testService.execute();
} else {
log.error("未找到相应的处理器");
// 异常处理
}
}
}
Service
public interface TestService {
void execute();
String supportType();
}
Impl1
@Service
public class TestServiceImpl1 implements TestService {
@Override
public void execute() {
System.out.println("TestServiceImpl1");
}
@Override
public String supportType() {
return "TestServiceImpl1";
}
}
Imp2
@Service
public class TestServiceImpl2 implements TestService {
@Override
public void execute() {
System.out.println("TestServiceImpl2");
}
@Override
public String supportType() {
return "TestServiceImpl2";
}
}