注解
package com.example.demo.remote.handler;
import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Component;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Component
@Documented
public @interface LogicHandler {
@AliasFor(annotation = Component.class)
String value();
Class type();
}
通用通知类
@Data
public class SimpleResponse<R> {
private String responseCode;
private String responseMsg;
private String transCode;
private R data;
}
@Data
public class RemoteHandlerRequest {
private RequestHead head;
private Object body;
}
@Data
public class RequestHead {
private String syscode;
private String bussiness;
private String usercode;
private String password;
public String sign;
}
抽象类
@Slf4j
public abstract class AbstractLogicHandler<Request,Response> {
private static final String USER_NAME ="";
private static final String USER_PASSWORD ="";
private Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
public final SimpleResponse<Response> execute(RemoteHandlerRequest request, Class<Request> clazz) throws Exception {
RequestHead head=request.getHead();
if (head!=null){
String syscode=head.getSyscode();
String userCode=head.getUsercode();
String userPassword=head.getPassword();
String user_code="";
String user_password="";
if ("online".equalsIgnoreCase(syscode)) {
user_code=USER_NAME;
user_password=USER_PASSWORD;
}
if (StringUtils.isBlank(userCode) || StringUtils.isBlank(userPassword)||!userCode.equals(user_code)||!user_password.equals(userPassword)) {
SimpleResponse response=new SimpleResponse();
response.setResponseCode("999");
response.setResponseMsg("账号密码验证失败");
return response;
}}
return logic(gson.fromJson(gson.toJson(request.getBody()),clazz),request.getHead());
}
public abstract SimpleResponse<Response> logic(Request request, RequestHead requestHead);
}
注解提取和扫描方法
@Slf4j
@Component
public class LogicHandlerScanner {
@Autowired
ApplicationContext applicationContext;
private volatile Map<String,HandlerCache> handlers;
@Data
@AllArgsConstructor
static class HandlerCache{
LogicHandler handler;
AbstractLogicHandler logic;
}
public HandlerCache getByBusiness(String businessCode){return handlers.get(businessCode);}
@PostConstruct
void load(){
Map<String,Object> data=applicationContext.getBeansWithAnnotation(LogicHandler.class);
handlers=new ConcurrentHashMap<>();
data.forEach((k,v)->{
Class x= AopUtils.getTargetClass(v);
LogicHandler handler=(LogicHandler) x.getAnnotation(LogicHandler.class);
if (!ObjectUtils.isEmpty(handler)&&AbstractLogicHandler.class.isAssignableFrom(x)){
log.info("Loading handler"+x.getName());
handlers.put(handler.value(),new HandlerCache(handler,(AbstractLogicHandler)v));
}
else {log.error("Loading handler"+x.getName());}
});
}
}
@Component
public class CompositeHandler {
@Autowired
LogicHandlerScanner scanner;
public final SimpleResponse excute(RemoteHandlerRequest request){
LogicHandlerScanner.HandlerCache ca=scanner.getByBusiness(request.getHead().getBussiness());
try {
return ca.getLogic().execute(request,ca.getHandler().type());
} catch (Exception e) {
e.printStackTrace();
return null;
}}
}
实现方法范例
@Slf4j
@LogicHandler(value="Q001",type = User.class)
public class xxxnotifyHandler extends AbstractLogicHandler<User, Object> {
@Override
public SimpleResponse<Object> logic(User user, RequestHead head){
return null;
}
}
Controller层调用
@RestController
@Slf4j
@RequestMapping("/controller")
public class RemoteController {
@Autowired
CompositeHandler handler;
@ResponseBody
@RequestMapping(value = "/toxxx",consumes = "application/json", produces = "application/json",method = RequestMethod.POST)
public SimpleResponse notify(@RequestBody RemoteHandlerRequest request){
SimpleResponse response =handler.excute(request);
return response;
}
}