cn.ishow.starter.rpc.processor.FeignClientAnnotationProcessor Maven / Gradle / Ivy
package cn.ishow.starter.rpc.processor;
import cn.ishow.starter.common.util.java.AnnotationUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.cloud.openfeign.EnableFeignClients;
import java.lang.annotation.Annotation;
/**
* EnableFeignClients 注解动态添加器
*
* @author bucheng
* @create 2022/5/29 16:45
*/
public class FeignClientAnnotationProcessor implements SpringApplicationRunListener {
private SpringApplication application;
private final String[] args;
public FeignClientAnnotationProcessor(SpringApplication application, String[] args) {
this.application = application;
this.args = args;
}
@Override
public void starting() {
Class> mainApplicationClass = application.getMainApplicationClass();
if (mainApplicationClass == null) {
return;
}
EnableFeignClients annotation = mainApplicationClass.getAnnotation(EnableFeignClients.class);
if (annotation != null) {
return;
}
annotation = new EnableFeignClients() {
@Override
public Class extends Annotation> annotationType() {
return EnableFeignClients.class;
}
@Override
public String[] value() {
return new String[]{mainApplicationClass.getPackage().getName()};
}
@Override
public String[] basePackages() {
return new String[]{mainApplicationClass.getPackage().getName()};
}
@Override
public Class>[] basePackageClasses() {
return new Class[0];
}
@Override
public Class>[] defaultConfiguration() {
return new Class[0];
}
@Override
public Class>[] clients() {
return new Class[0];
}
};
AnnotationUtils.modifyAnnotation(mainApplicationClass, EnableFeignClients.class, annotation);
}
}