cn.ishow.starter.rpc.aop.RpcClientAdvisor Maven / Gradle / Ivy
package cn.ishow.starter.rpc.aop;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.AbstractBeanFactoryPointcutAdvisor;
import org.springframework.aop.support.StaticMethodMatcher;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import java.lang.reflect.Method;
/**
* 自定义Advisor,让其能够动态链接Feign接口上面方法
*
* @author bucheng
* @create 2022/5/15 11:29
*/
public class RpcClientAdvisor extends AbstractBeanFactoryPointcutAdvisor {
/**
* 对FeignClient注解类进行AOP拦截
*/
private static final Pointcut FEIGN_POINTCUT = new RpcClientPointcut();
@Override
public Pointcut getPointcut() {
return FEIGN_POINTCUT;
}
private static class RpcClientPointcut implements Pointcut {
@Override
public ClassFilter getClassFilter() {
return clazz -> AnnotationUtils.findAnnotation(clazz, FeignClient.class) != null;
}
@Override
public MethodMatcher getMethodMatcher() {
return new StaticMethodMatcher() {
@Override
public boolean matches(Method method, Class> targetClass) {
//这里包括GetMapping和PostMapping等;spring该方法会根据注解派生性进行获取
return AnnotationUtils.findAnnotation(method, RequestMapping.class) != null;
}
};
}
}
}