Example usage for org.springframework.core.io.support SpringFactoriesLoader loadFactoryNames

List of usage examples for org.springframework.core.io.support SpringFactoriesLoader loadFactoryNames

Introduction

In this page you can find the example usage for org.springframework.core.io.support SpringFactoriesLoader loadFactoryNames.

Prototype

public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) 

Source Link

Document

Load the fully qualified class names of factory implementations of the given type from #FACTORIES_RESOURCE_LOCATION , using the given class loader.

Usage

From source file:io.gravitee.gateway.handlers.api.http.client.spring.HttpClientConfigurationImportSelector.java

/**
 * Return the HTTP client class names that should be considered. By default
 * this method will load candidates using {@link SpringFactoriesLoader} with
 * {@link #getSpringFactoriesLoaderFactoryClass()}.
 * attributes}//from ww  w  .  j  a  v a 2  s  .c  o  m
 * @return a list of candidate configurations
 */
protected List<String> getCandidateConfigurations() {
    List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
            this.getClass().getClassLoader());
    Assert.notEmpty(configurations, "No HTTP client classes found in META-INF/spring.factories. If you"
            + "are using a custom packaging, make sure that file is correct.");
    return configurations;
}

From source file:org.wso2.msf4j.spring.MSF4JSpringApplication.java

private <T> Collection<? extends T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes,
        Object... args) {/*  w  w  w.  ja va 2s  .co  m*/
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    Set<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
    List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
    AnnotationAwareOrderComparator.sort(instances);
    return instances;
}

From source file:org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.java

/**
 * Return the auto-configuration class names that should be considered. By default
 * this method will load candidates using {@link SpringFactoriesLoader} with
 * {@link #getSpringFactoriesLoaderFactoryClass()}.
 * @param metadata the source metadata/*w  ww.ja  va2  s. c o m*/
 * @param attributes the {@link #getAttributes(AnnotationMetadata) annotation
 * attributes}
 * @return a list of candidate configurations
 */
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata,
        AnnotationAttributes attributes) {
    List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
            getBeanClassLoader());
    Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
            + "are using a custom packaging, make sure that file is correct.");
    return configurations;
}

From source file:org.springframework.boot.diagnostics.FailureAnalyzers.java

private List<FailureAnalyzer> loadFailureAnalyzers(ClassLoader classLoader) {
    List<String> analyzerNames = SpringFactoriesLoader.loadFactoryNames(FailureAnalyzer.class, classLoader);
    List<FailureAnalyzer> analyzers = new ArrayList<FailureAnalyzer>();
    for (String analyzerName : analyzerNames) {
        try {//from  www  .  ja  v  a2s . com
            Constructor<?> constructor = ClassUtils.forName(analyzerName, classLoader).getDeclaredConstructor();
            ReflectionUtils.makeAccessible(constructor);
            analyzers.add((FailureAnalyzer) constructor.newInstance());
        } catch (Throwable ex) {
            log.trace("Failed to load " + analyzerName, ex);
        }
    }
    AnnotationAwareOrderComparator.sort(analyzers);
    return analyzers;
}

From source file:org.springframework.boot.SpringApplication.java

@SuppressWarnings("unchecked")
private <T> Collection<? extends T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes,
        Object... args) {/*from  w w  w. j  av a  2  s .c  o  m*/
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

    // Use names and ensure unique to protect against duplicates
    Set<String> names = new LinkedHashSet<String>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
    List<T> instances = new ArrayList<T>(names.size());

    // Create instances from the names
    for (String name : names) {
        try {
            Class<?> instanceClass = ClassUtils.forName(name, classLoader);
            Assert.isAssignable(type, instanceClass);
            Constructor<?> constructor = instanceClass.getConstructor(parameterTypes);
            T instance = (T) constructor.newInstance(args);
            instances.add(instance);
        } catch (Throwable ex) {
            throw new IllegalArgumentException("Cannot instantiate " + type + " : " + name, ex);
        }
    }

    AnnotationAwareOrderComparator.sort(instances);
    return instances;
}

From source file:org.springframework.cloud.commons.util.SpringFactoryImportSelector.java

@Override
public String[] selectImports(AnnotationMetadata metadata) {
    if (!isEnabled()) {
        return new String[0];
    }//from w  ww  .jav a  2  s. com
    AnnotationAttributes attributes = AnnotationAttributes
            .fromMap(metadata.getAnnotationAttributes(this.annotationClass.getName(), true));

    Assert.notNull(attributes, "No " + getSimpleName() + " attributes found. Is " + metadata.getClassName()
            + " annotated with @" + getSimpleName() + "?");

    // Find all possible auto configuration classes, filtering duplicates
    List<String> factories = new ArrayList<>(new LinkedHashSet<>(
            SpringFactoriesLoader.loadFactoryNames(this.annotationClass, this.beanClassLoader)));

    if (factories.isEmpty() && !hasDefaultFactory()) {
        throw new IllegalStateException("Annotation @" + getSimpleName()
                + " found, but there are no implementations. Did you forget to include a starter?");
    }

    if (factories.size() > 1) {
        // there should only ever be one DiscoveryClient, but there might be more than
        // one factory
        log.warn("More than one implementation " + "of @" + getSimpleName()
                + " (now relying on @Conditionals to pick one): " + factories);
    }

    return factories.toArray(new String[factories.size()]);
}

From source file:org.springframework.test.context.support.AbstractTestContextBootstrapper.java

/**
 * Get the names of the default {@link TestExecutionListener} classes for
 * this bootstrapper.//from w w w  .  j  av  a  2s.  com
 * <p>The default implementation looks up all
 * {@code org.springframework.test.context.TestExecutionListener} entries
 * configured in all {@code META-INF/spring.factories} files on the classpath.
 * <p>This method is invoked by {@link #getDefaultTestExecutionListenerClasses()}.
 * @return an <em>unmodifiable</em> list of names of default {@code TestExecutionListener}
 * classes
 * @see SpringFactoriesLoader#loadFactoryNames
 */
protected List<String> getDefaultTestExecutionListenerClassNames() {
    List<String> classNames = SpringFactoriesLoader.loadFactoryNames(TestExecutionListener.class,
            getClass().getClassLoader());
    if (logger.isInfoEnabled()) {
        logger.info(String.format("Loaded default TestExecutionListener class names from location [%s]: %s",
                SpringFactoriesLoader.FACTORIES_RESOURCE_LOCATION, classNames));
    }
    return Collections.unmodifiableList(classNames);
}