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

group.flyfish.rest.registry.RestApiRegistry Maven / Gradle / Ivy

package group.flyfish.rest.registry;

import group.flyfish.rest.annotation.EnableRestApiProxy;
import group.flyfish.rest.annotation.RestService;
import group.flyfish.rest.core.factory.HttpClientProvider;
import group.flyfish.rest.registry.proxy.RestProxyInvoker;
import group.flyfish.rest.registry.proxy.support.RestArgumentResolverComposite;
import group.flyfish.rest.utils.DataUtils;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.reflections.Reflections;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.*;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * rest接口注册机
 *
 * @author wangyu
 */
@RequiredArgsConstructor
@Slf4j
public class RestApiRegistry implements BeanDefinitionRegistryPostProcessor, BeanFactoryAware {

    @Getter
    private final RestArgumentResolverComposite composite;

    @Getter
    private final HttpClientProvider provider;

    // bean工厂
    private ConfigurableListableBeanFactory beanFactory;

    /**
     * 动态注册bean
     *
     * @param registry 注册机
     * @throws BeansException 异常
     */
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        // 包名
        List packageNames = new ArrayList<>();
        // 找基本包,找不到立马报错
        beanFactory.getBeansWithAnnotation(EnableRestApiProxy.class)
                .forEach((key, value) -> {
                    EnableRestApiProxy proxy = value.getClass().getAnnotation(EnableRestApiProxy.class);
                    for (String basePackage : proxy.basePackages()) {
                        if (DataUtils.isNotBlank(basePackage)) {
                            packageNames.add(basePackage);
                        }
                    }
                });
        //  不为空时查找
        if (DataUtils.isNotEmpty(packageNames)) {
            // 初始化反射
            try {
                Reflections reflections = new Reflections(packageNames.toArray());
                // 得到Resource注解的类
                Set> classSet = reflections.getTypesAnnotatedWith(RestService.class);
                // 不存在,不要浪费性能
                if (CollectionUtils.isEmpty(classSet)) return;
                // 代理并生成子类,并注册到ioc容器
                classSet.forEach(clazz -> registry.registerBeanDefinition(clazz.getName(), generate(clazz)));
            } catch (IllegalStateException e) {
                log.error("初始化Rest映射时出错", e);
            }
            return;
        }
        throw new BeanDefinitionValidationException("【RestApi】EnableRestApiProxy注解必须指定有效的basePackage!");
    }

    /**
     * 生成bean定义
     *
     * @param clazz 目标类型
     * @return bean定义
     */
    private BeanDefinition generate(Class clazz) {
        return BeanDefinitionBuilder.genericBeanDefinition(clazz, () -> RestProxyInvoker.produce(clazz, this))
                .setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE).getRawBeanDefinition();
    }

    @SneakyThrows
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        // do nothing
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        Assert.isTrue(beanFactory instanceof ConfigurableListableBeanFactory, "当前bean factory不被支持!");
        this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy