@Override public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { List<TemplateAvailabilityProvider> availabilityProviders = SpringFactoriesLoader .loadFactories(TemplateAvailabilityProvider.class, context.getClassLoader()); for (TemplateAvailabilityProvider availabilityProvider : availabilityProviders) { if (availabilityProvider.isTemplateAvailable("error", context.getEnvironment(), context.getClassLoader(), context.getResourceLoader())) { return ConditionOutcome.noMatch("Template from " + availabilityProvider + " found for error view"); } } return ConditionOutcome.match("No error template view detected"); }
protected T buildInitializer(Supplier<T> defaultSupplier, Object ... args) { List<String> initClassNames = SpringFactoriesLoader.loadFactoryNames(initClass, Thread.currentThread().getContextClassLoader()); if (initClassNames.isEmpty()) { return defaultSupplier.get(); } List<Class<?>> initClasses = ClassUtils.convertClassNamesToClasses(initClassNames); initClasses.sort(AnnotationAwareOrderComparator.INSTANCE); Class<?> primaryInitClass = initClasses.get(0); try { return this.initClass.cast(ConstructorUtils.invokeConstructor(primaryInitClass, args)); } catch (IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException e) { throw new ApplicationContextException(String.format("Unable to instantiate application initializer (class=%s).", primaryInitClass.getName()), e); } }
@Override public void init() { SdcctBeanDefinitionParser beanDefParser; for (Class<?> beanDefClass : ClassUtils .convertClassNamesToClasses(SpringFactoriesLoader.loadFactoryNames(SdcctBeanDefinitionParser.class, this.getClass().getClassLoader()))) { try { beanDefParser = ((SdcctBeanDefinitionParser) ConstructorUtils.invokeConstructor(beanDefClass, this)); } catch (IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException e) { throw new ApplicationContextException(String.format("Unable to instantiate bean definition parser (class=%s).", beanDefClass.getName()), e); } for (String beanDefParserElemLocalName : beanDefParser.getElementBeanClasses().keySet()) { this.beanDefParsers.put(beanDefParserElemLocalName, beanDefParser); this.registerBeanDefinitionParser(beanDefParserElemLocalName, beanDefParser); } } }
protected static <T> T buildComponent(Class<T> componentClass, Supplier<T> defaultSupplier, Object ... args) { List<String> componentClassNames = SpringFactoriesLoader.loadFactoryNames(componentClass, AbstractCrigttApplicationRunListener.class.getClassLoader()); if (componentClassNames.isEmpty()) { return defaultSupplier.get(); } List<Class<?>> componentClasses = ClassUtils.convertClassNamesToClasses(componentClassNames); componentClasses.sort(AnnotationAwareOrderComparator.INSTANCE); Class<?> primaryComponentClass = componentClasses.get(0); try { return componentClass.cast(ConstructorUtils.invokeConstructor(primaryComponentClass, args)); } catch (IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException e) { throw new ApplicationContextException(String.format("Unable to instantiate component (class=%s).", primaryComponentClass.getName()), e); } }
CredentialProviderRegistry(final DataManager dataManager) { this.dataManager = dataManager; credentialProviderFactories = SpringFactoriesLoader .loadFactories(CredentialProviderFactory.class, ClassUtils.getDefaultClassLoader()).stream() .collect(Collectors.toMap(CredentialProviderFactory::id, Function.identity())); }
@Test public void test01() { try { Enumeration<URL> files = this.getClass().getClassLoader().getResources(SpringFactoriesLoader.FACTORIES_RESOURCE_LOCATION); while(files.hasMoreElements()) { System.out.println("=== " + files.nextElement()); } } catch (IOException e) { e.printStackTrace(); } }
@Test public void test02() { List<String> classes = SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class, this.getClass().getClassLoader()); classes.forEach(clazz -> { System.out.println("==== " + clazz); }); }
/** * Create a new {@link PropertySourceLoader} instance backed by the specified * {@link MutablePropertySources}. * @param propertySources the destination property sources */ public PropertySourcesLoader(MutablePropertySources propertySources) { Assert.notNull(propertySources, "PropertySources must not be null"); this.propertySources = propertySources; this.loaders = SpringFactoriesLoader.loadFactories(PropertySourceLoader.class, getClass().getClassLoader()); }
private <T> Collection<? extends T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) { 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 = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names); AnnotationAwareOrderComparator.sort(instances); return instances; }
public static boolean analyzeAndReport(Throwable failure, ClassLoader classLoader, ConfigurableApplicationContext context) { List<FailureAnalyzer> analyzers = SpringFactoriesLoader .loadFactories(FailureAnalyzer.class, classLoader); List<FailureAnalysisReporter> reporters = SpringFactoriesLoader .loadFactories(FailureAnalysisReporter.class, classLoader); FailureAnalysis analysis = analyze(failure, analyzers, context); return report(analysis, reporters); }
@Override public String[] selectImports(AnnotationMetadata metadata) { // Find all possible auto configuration classes, filtering duplicates List<String> factories = new ArrayList<String>( new LinkedHashSet<String>(SpringFactoriesLoader.loadFactoryNames( ManagementContextConfiguration.class, this.classLoader))); AnnotationAwareOrderComparator.sort(factories); return factories.toArray(new String[0]); }
private Collection<String> getConfigurationsForAnnotation(Class<?> source, Annotation annotation) { String[] value = (String[]) AnnotationUtils .getAnnotationAttributes(annotation, true).get("value"); if (value.length > 0) { return Arrays.asList(value); } return SpringFactoriesLoader.loadFactoryNames(source, getClass().getClassLoader()); }
/** * 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 * @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; }
@Test public void importsAreSelected() { configureExclusions(new String[0], new String[0], new String[0]); String[] imports = this.importSelector.selectImports(this.annotationMetadata); assertThat(imports).hasSameSizeAs(SpringFactoriesLoader.loadFactoryNames( EnableAutoConfiguration.class, getClass().getClassLoader())); assertThat(ConditionEvaluationReport.get(this.beanFactory).getExclusions()) .isEmpty(); }
/** * Return the HTTP client class names that should be considered. By default * this method will load candidates using {@link SpringFactoriesLoader} with * {@link #getSpringFactoriesLoaderFactoryClass()}. * attributes} * @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; }
@Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { List<EnvironmentPostProcessor> postProcessors = SpringFactoriesLoader .loadFactories(EnvironmentPostProcessor.class, getClass().getClassLoader()); for (EnvironmentPostProcessor postProcessor : postProcessors) { postProcessor.postProcessEnvironment(event.getEnvironment(), event.getSpringApplication()); } }
@SuppressWarnings("unchecked") private <T> Collection<? extends T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) { 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; }
@Test public void importsAreSelected() { configureExclusions(new String[0], new String[0], new String[0]); String[] imports = this.importSelector.selectImports(this.annotationMetadata); assertThat(imports.length, is(equalTo(SpringFactoriesLoader .loadFactoryNames(EnableAutoConfiguration.class, getClass().getClassLoader()) .size()))); assertThat(ConditionEvaluationReport.get(this.beanFactory).getExclusions(), hasSize(0)); }
private <T> Collection<? extends T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) { 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; }
@Override public String[] selectImports(AnnotationMetadata metadata) { if (!isEnabled()) { return new String[0]; } 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()]); }
private List<String> getConfigurations() { return SpringFactoriesLoader.loadFactoryNames(Configuration.class, Thread.currentThread().getContextClassLoader()); }
List<EnvironmentPostProcessor> loadPostProcessors() { return SpringFactoriesLoader.loadFactories(EnvironmentPostProcessor.class, getClass().getClassLoader()); }
/** * Create a new {@link TemplateAvailabilityProviders} instance. * @param classLoader the source class loader */ public TemplateAvailabilityProviders(ClassLoader classLoader) { Assert.notNull(classLoader, "ClassLoader must not be null"); this.providers = SpringFactoriesLoader .loadFactories(TemplateAvailabilityProvider.class, classLoader); }
private List<String> getAutoConfigurationClassNames() { return SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class, getClass().getClassLoader()); }
private List<PropertySourceLoader> initPropertyLoaders() { return SpringFactoriesLoader.loadFactories(PropertySourceLoader.class, getClass().getClassLoader()); }
@Override protected List<String> doSelect(AnnotationMetadata metadata, AnnotationAttributes attributes) { List<String> classNames = new ArrayList<String>(); if(attributes.getBoolean("enableCommonService")){ classNames.add(BootCommonServiceConfig.class.getName()); } AppcationType appcationType = (AppcationType)attributes.get("appcationType"); if(appcationType==AppcationType.WEB_SERVICE){ classNames.add(BootMSContextAutoConfig.class.getName()); }else if(appcationType==AppcationType.WEB_UI){ classNames.add(BootWebUIContextAutoConfig.class.getName()); } classNames.add(ErrorHandleConfiguration.class.getName()); classNames.add(EnhanceRequestMappingConfiguration.class.getName()); classNames.add(JwtContextConfig.class.getName()); classNames.add(OAuth2SsoClientAutoContextConfig.class.getName()); classNames.add(RedisConfiguration.class.getName()); classNames.add(AsyncMvcConfiguration.class.getName()); classNames.add(AsyncTaskConfiguration.class.getName()); classNames.add(AccessLogConfiguration.class.getName()); classNames.add(GraceKillConfiguration.class.getName()); //cache classNames.add(SpringCacheConfiguration.class.getName()); classNames.add(RedissonConfiguration.class.getName()); Collection<String> exts = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(this.annotationClass, this.beanClassLoader)); for(String extClassName : exts){ Class<?> extClass; try { extClass = ClassUtils.forName(extClassName, beanClassLoader); } catch (ClassNotFoundException | LinkageError e) { throw new BaseException("load "+this.annotationClass.getSimpleName()+" error. extension class: " + extClassName, e); } if(extClass.getAnnotation(JFishWebPlugin.class)==null){ throw new BaseException("extension class["+extClassName+"] must be annotated by "+JFishWebPlugin.class.getName()); } classNames.add(extClassName); } return classNames; }