All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.ishow.starter.rpc.processor.DecoderBeanPostProcessor Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
package cn.ishow.starter.rpc.processor;

import cn.ishow.starter.rpc.basic.CustomDecoder;
import cn.ishow.starter.rpc.context.RpcContext;
import feign.codec.Decoder;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author bucheng
 * @create 2022/5/15 20:26
 * @description 在spring对象注入时候动态修改Decoder对象,注入自定义拦截器
 */
public class DecoderBeanPostProcessor implements BeanPostProcessor {
    /**
     * 是否完成代理
     */
    private final AtomicBoolean complete = new AtomicBoolean(false);
    @Autowired
    private RpcContext rpcContext;

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof Decoder
                && !(bean instanceof CustomDecoder)
                && complete.compareAndSet(false, true)) {
            return new CustomDecoder((Decoder) bean, rpcContext);
        }
        return bean;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy