利用注解开发多业务场景的通用型接口

注解

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;
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值