Spring整合插件XXXRegistry类设计

  1. registry类设计实现
    @Getter
    public class GlobalServerInterceptorRegistry implements ApplicationContextAware {
        private final List<ServerInterceptor> serverInterceptors = Lists.newArrayList();
        private ApplicationContext applicationContext;
        @PostConstruct
        public void init() {
            // 获取所有的GlobalServerInterceptorConfigurerAdapter实现类
            Map<String, GlobalServerInterceptorConfigurerAdapter> map = applicationContext.getBeansOfType(GlobalServerInterceptorConfigurerAdapter.class);
            for (GlobalServerInterceptorConfigurerAdapter globalServerInterceptorConfigurerAdapter : map.values()) {
                // 调用adapter的addServerInterceptors, 将registry作为参数传入adapter,业务adapter调用registry的addServerInterceptors方法添加interceptor
                globalServerInterceptorConfigurerAdapter.addServerInterceptors(this);
            }
        }
        public GlobalServerInterceptorRegistry addServerInterceptors(ServerInterceptor interceptor) {
            serverInterceptors.add(interceptor);
            return this;
        }
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.applicationContext = applicationContext;
        }
    }
    
  2. Adapter类设计
    public abstract class GlobalServerInterceptorConfigurerAdapter {
        public void addServerInterceptors(GlobalServerInterceptorRegistry registry) {
    
        }
    }
    
  3. 项目中业务adapter代码编写
    @Configuration
    public class GlobalClientInterceptorConfiguration {
        @Bean
        public GlobalServerInterceptorConfigurerAdapter globalInterceptorConfigurerAdapter() {
            return new GlobalServerInterceptorConfigurerAdapter() {
                @Override
                public void addServerInterceptors(GlobalServerInterceptorRegistry registry) {
                    registry.addServerInterceptors(new LogGrpcInterceptor());
                }
            } ;
        }
    }
    public class LogGrpcInterceptor implements ServerInterceptor {
        @Override
        public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
            log.info("=======> method : {}", call.getMethodDescriptor().getFullMethodName());
            return next.startCall(call, headers);
        }
    }
    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值