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

spring.turbo.core.SpringFactoriesUtils Maven / Gradle / Ivy

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *    ____             _            _____           _
 *   / ___| _ __  _ __(_)_ __   __ |_   _|   _ _ __| |__   ___
 *   \___ \| '_ \| '__| | '_ \ / _` || || | | | '__| '_ \ / _ \
 *    ___) | |_) | |  | | | | | (_| || || |_| | |  | |_) | (_) |
 *   |____/| .__/|_|  |_|_| |_|\__, ||_| \__,_|_|  |_.__/ \___/
 *         |_|                 |___/   https://github.com/yingzhuo/spring-turbo
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package spring.turbo.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.OrderComparator;
import org.springframework.core.io.support.SpringFactoriesLoader;
import spring.turbo.util.Asserts;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static spring.turbo.util.CollectionUtils.nullSafeAddAll;

/**
 * {@link SpringFactoriesLoader} 相关工具
 *
 * @author 应卓
 * @since 2.0.5
 */
public final class SpringFactoriesUtils {

    /**
     * 私有构造方法
     */
    private SpringFactoriesUtils() {
        super();
    }

    /**
     * 获取服务的实例
     *
     * @param factoryType 服务的类型
     * @param          服务的类型泛型
     * @return 结果
     */
    public static  List loadQuietly(Class factoryType) {
        Asserts.notNull(factoryType);

        var factoriesLoader = SpringFactoriesLoader.forDefaultResourceLocation();

        try {
            List list = new ArrayList<>();
            var services = factoriesLoader.load(factoryType, LoggingFailureHandler.INSTANCE);
            nullSafeAddAll(list, services);
            OrderComparator.sort(list);
            return Collections.unmodifiableList(list);
        } catch (Throwable e) {
            return Collections.emptyList();
        }
    }

    // -----------------------------------------------------------------------------------------------------------------

    private static class LoggingFailureHandler implements SpringFactoriesLoader.FailureHandler {
        private static final Logger log = LoggerFactory.getLogger(LoggingFailureHandler.class);
        private static final LoggingFailureHandler INSTANCE = new LoggingFailureHandler();

        /**
         * 私有构造方法
         */
        private LoggingFailureHandler() {
            super();
        }

        @Override
        public void handleFailure(Class factoryType, String factoryImplementationName, Throwable ex) {
            log.warn(ex.getMessage(), ex);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy