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

org.zodiac.autoconfigure.feign.annotation.FeignConsumerConfigurerRegistrar Maven / Gradle / Ivy

package org.zodiac.autoconfigure.feign.annotation;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.ClassUtils;
import org.zodiac.autoconfigure.feign.FeignClientConfiguration;
import org.zodiac.autoconfigure.feign.FeignContextAutoConfiguration;
import org.zodiac.autoconfigure.feign.context.DefaultFeignContextAutoConfiguration;
import org.zodiac.commons.logging.SmartSlf4jLoggerFactory;
import org.zodiac.commons.util.Colls;
import org.zodiac.commons.util.spring.Springs;
import org.zodiac.feign.core.annotation.FrameworkType;
import org.zodiac.feign.core.constants.FeignSystemPropertiesConstants;
import org.zodiac.feign.core.util.FeignSpringUtil;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;

public class FeignConsumerConfigurerRegistrar implements ImportBeanDefinitionRegistrar, ApplicationContextAware, EnvironmentAware {

    private static List DEFAULT_REACTIVE_PLUGINS = Collections.unmodifiableList(
        Arrays.asList(
            "org.zodiac.feign.core.extension.reactive.ReactiveInsertBeanBindingCoprocessor",
            "org.zodiac.feign.core.extension.reactive.ReactivePageBindingCoprocessor",
            "org.zodiac.feign.core.extension.reactive.ReactiveRpcSimpleLogTraceCoprocessor"
        )
    );

    private static List DEFAULT_SERVLET_PLUGINS = Collections.unmodifiableList(
        Arrays.asList(
            "org.zodiac.feign.core.extension.servlet.ServletInsertBeanBindingCoprocessor",
            "org.zodiac.feign.core.extension.servlet.ServletPageBindingCoprocessor",
            "org.zodiac.feign.core.extension.servlet.ServletRpcSimpleLogTraceCoprocessor"
        )
    );

    protected final Logger log = SmartSlf4jLoggerFactory.getLogger(getClass());

    
    private ApplicationContext applicationContext;
    private Environment environment;

    public FeignConsumerConfigurerRegistrar() {
        super();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    /**
     * Spring cloud auto configuration should not be affected.
     * 
     * 
*
    *
  • org.springframework.cloud.openfeign.ribbon.DefaultFeignLoadBalancedConfiguration
  • *
  • org.springframework.cloud.openfeign.ribbon.HttpClientFeignLoadBalancedConfiguration
  • *
  • org.springframework.cloud.openfeign.ribbon.OkHttpFeignLoadBalancedConfiguration
  • *
*
*/ @Override public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { /*Check enabled configuration*/ if (!FeignSpringUtil.isEnableConfiguration(environment)) { log.warn("No enabled Feign+SpringBoot / Feign+SpringBoot+Istio / Feign+SpringCloud / Feign+SpringBoot+Dubbo auto configurer !"); return; } /*Register core requires configuration.*/ registerBeanDefinition0(registry, FeignClientConfiguration.class); /*Register SpringCloud-Feign environment configuration.*/ if (FrameworkType.FEIGN_SPRINGCLOUD.isActive()) { /*Nothing to-do, some codes.*/ } /*Register Apache-Dubbo-Feign environment configuration.*/ else if (FrameworkType.FEIGN_SPRINGBOOT_APACHE_DUBBO.isActive()) { /*Nothing to-do, some codes.*/ } /*Register (Default)SpringBoot-Feign or SpringBoot-Istio-Feign environment configuration.*/ else { if (FrameworkType.FEIGN_SPRINGBOOT.isActive() || FrameworkType.FEIGN_SPRINGBOOT_ISTIO.isActive()) { registerBeanDefinition0(registry, FeignContextAutoConfiguration.class); } if (FrameworkType.FEIGN_SPRINGBOOT.isActive()) { registerBeanDefinition0(registry, DefaultFeignContextAutoConfiguration.class); } } /*Register build-in feign-context configuration.*/ //registerBeanDefinition0(registry, FeignContextAutoConfiguration.class); /*Register plug-in's configuration.*/ // registerBeanDefinition(registry,FeignPluginsAutoConfiguration.class); registerPlugins(registry); // /*Not SpringCloud + Feign*/ // if (!FeignSpringUtil.hasSpringCloudFeignClass()) { // //BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(FeignConsumerAutoConfiguration.class); // BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(obtainFeignConsumerConfigurationType()); // AbstractBeanDefinition definition = builder.getBeanDefinition(); // String beanName = AnnotationBeanNameGenerator.INSTANCE.generateBeanName(definition, registry); // registry.registerBeanDefinition(beanName, definition); // } } // protected Class obtainFeignConsumerConfigurationType() { // return FeignContextAutoConfiguration.class; // } /*Register the feign context coprocessing plug-in's.*/ private void registerPlugins(BeanDefinitionRegistry registry) { if (Springs.isNonWeb(applicationContext)) { return; } List plugins = Springs.isServletWeb(applicationContext) ? DEFAULT_SERVLET_PLUGINS : DEFAULT_REACTIVE_PLUGINS; /*Gets from configuration.*/ String pluginValue = environment.getProperty(FeignSystemPropertiesConstants.FEIGN_PLUGIN_CLASSES); if (StrUtil.isNotBlank(pluginValue)) { List plugins0 = StrUtil.split(pluginValue, ',').stream().map(String::trim).filter(s -> s.length() > 0).collect(Collectors.toList()); if (Colls.notEmptyColl(plugins0)) { plugins = plugins0; } } /*Register plug-in's bean.*/ for (String cls : plugins) { try { registerBeanDefinition0(registry, ClassUtils.forName(cls, Thread.currentThread().getContextClassLoader())); } catch (ClassNotFoundException | LinkageError e) { throw new IllegalStateException(String.format("Failed to register coprocessing for '%s'", cls), e); } } } private void registerBeanDefinition0(BeanDefinitionRegistry registry, Class configurationClass) { AbstractBeanDefinition definition = BeanDefinitionBuilder.genericBeanDefinition(configurationClass).getBeanDefinition(); registry.registerBeanDefinition(AnnotationBeanNameGenerator.INSTANCE.generateBeanName(definition, registry), definition); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy