注册器
/**
*
* @author 木此
*/
public abstract class BaseTransferHandle <R extends BaseTransferHandleRequest,T extends BaseTransferHandleResult> implements
InitializingBean {
@Autowired
TransferHandlerRepository transferHandlerRepository;
public abstract T execute(R request);
@Override
public void afterPropertiesSet() throws Exception {
TransferType api = this.getClass().getAnnotation(TransferType.class);
if(api == null){
throw new RuntimeException(
"No registration class found"
);
}
transferHandlerRepository.register(api.value(),this);
}
}
request
/**
*
* @author 木此
*/
public abstract class BaseTransferHandleRequest {
}
result
/**
*
* @author 木此
*/
public abstract class BaseTransferHandleResult {
}
存储器
/**
*
* @author 木此
*/
@Component
public class TransferHandlerRepository {
private final Map<String,BaseTransferHandle> map = new HashMap<>(16);
public void register(String code, BaseTransferHandle handler) {
map.put(code, handler);
}
public BaseTransferHandle get(String code) {
return map.get(code);
}
}
自定义注解
/**
*
* @author 木此
*/
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface TransferType {
String value();
}
调用转接类
/**
*
* @author 木此
*/
@Component
public class TransferInletHandle{
@Autowired
private TransferHandlerRepository transferHandlerRepository;
public TransferInletHandleResult execute(TransferInletHandleRequest request) {
TransferInletHandleResult result = new TransferInletHandleResult();
if (StringUtils.isEmpty(request.getFunctionCode())) {
return result;
}
Map<String, Object> requestData = request.getRequestData();
BaseTransferHandle handler = transferHandlerRepository.get(request.getFunctionCode());
ParameterizedType superClass = (ParameterizedType) handler.getClass()
.getGenericSuperclass();
Type requestType = superClass.getActualTypeArguments()[0];
BaseTransferHandleRequest handlerRequest = JSONObject
.parseObject(JSONObject.toJSONString(requestData), requestType);
result.setResultData(handler.execute(handlerRequest));
return result;
}
}
request
/**
*
* @author 木此
*/
public class TransferInletHandleRequest {
/**
* 决定走哪个方法
*/
private String functionCode;
/**
* 入参合集
*/
private Map<String, Object> requestData;
/**
*
* @return the value of functionCode
*/
public String getFunctionCode() {
return functionCode;
}
/**
*
* @param functionCode functionCode
*/
public void setFunctionCode(String functionCode) {
this.functionCode = functionCode;
}
/**
*
* @return the value of requestData
*/
public Map<String, Object> getRequestData() {
return requestData;
}
/**
* @param requestData requestData
*/
public void setRequestData(Map<String, Object> requestData) {
this.requestData = requestData;
}
}
result
/**
*
* @author 木此
*/
public class TransferInletHandleResult {
/**
* 返回值集合
*/
private Object resultData;
/**
*
* @return the value of resultData
*/
public Object getResultData() {
return resultData;
}
/**
*
* @param resultData resultData
*/
public void setResultData(Object resultData) {
this.resultData = resultData;
}
}
注册测试类
/**
*
* @author 木此
*/
@TransferType("test.chai")
public class TestTransferHandle extends BaseTransferHandle<TestTransferHandleRequest,TestTransferHandleResult>{
@Override
public TestTransferHandleResult execute(TestTransferHandleRequest request) {
TestTransferHandleResult result = new TestTransferHandleResult();
System.out.println("-----------------------");
System.out.println("----------chai----------");
System.out.println("-----------ke---------");
System.out.println("----------qiang---------");
System.out.println("-----------------------");
return result;
}
}
request
/**
*
* @author 木此
*/
public class TransferInletHandleRequest extends BaseTransferHandleRequest{
/**
* 决定走哪个方法
*/
private String functionCode;
/**
* 入参合集
*/
private Map<String, Object> requestData;
/**
*
* @return the value of functionCode
*/
public String getFunctionCode() {
return functionCode;
}
/**
*
* @param functionCode functionCode
*/
public void setFunctionCode(String functionCode) {
this.functionCode = functionCode;
}
/**
*
* @return the value of requestData
*/
public Map<String, Object> getRequestData() {
return requestData;
}
/**
* @param requestData requestData
*/
public void setRequestData(Map<String, Object> requestData) {
this.requestData = requestData;
}
}
result
/**
*
* @author 木此
*/
public class TransferInletHandleResult extends BaseTransferHandleResult{
/**
* 返回值集合
*/
private Object resultData;
/**
*
* @return the value of resultData
*/
public Object getResultData() {
return resultData;
}
/**
*
* @param resultData resultData
*/
public void setResultData(Object resultData) {
this.resultData = resultData;
}
}