本文整理了Java中java.util.ServiceLoader.load()
方法的一些代码示例,展示了ServiceLoader.load()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ServiceLoader.load()
方法的具体详情如下:
包路径:java.util.ServiceLoader
类名称:ServiceLoader
方法名:load
[英]Constructs a service loader, using the current thread's context class loader.
[中]使用当前线程的上下文类加载器构造服务加载器。
代码示例来源:origin: ctripcorp/apollo
public static <S> Iterator<S> loadAll(Class<S> clazz) {
ServiceLoader<S> loader = ServiceLoader.load(clazz);
return loader.iterator();
}
}
代码示例来源:origin: gocd/gocd
private IVProvider getIvProviderInstance() {
if (ivProvider == null) {
synchronized (AESEncrypter.class) {
if (ivProvider == null) {
ivProvider = ServiceLoader.load(IVProvider.class).iterator().next();
}
}
}
return ivProvider;
}
代码示例来源:origin: jenkinsci/jenkins
/**
* Obtains the instance to be used.
*/
public static InitStrategy get(ClassLoader cl) throws IOException {
Iterator<InitStrategy> it = ServiceLoader.load(InitStrategy.class, cl).iterator();
if (!it.hasNext()) {
return new InitStrategy(); // default
}
InitStrategy s = it.next();
LOGGER.log(Level.FINE, "Using {0} as InitStrategy", s);
return s;
}
代码示例来源:origin: apache/hbase
private static List<MetricRegistries> getDefinedImplemantations() {
ServiceLoader<MetricRegistries> loader = ServiceLoader.load(MetricRegistries.class);
List<MetricRegistries> availableFactories = new ArrayList<>();
for (MetricRegistries impl : loader) {
availableFactories.add(impl);
}
return availableFactories;
}
}
代码示例来源:origin: wildfly/wildfly
private static List<Externalizer<Object>> loadExternalizers(ClassLoader loader) {
List<Externalizer<Object>> loadedExternalizers = new LinkedList<>();
for (Externalizer<Object> externalizer : ServiceLoader.load(Externalizer.class, loader)) {
loadedExternalizers.add(externalizer);
}
Set<DefaultExternalizer> defaultExternalizers = EnumSet.allOf(DefaultExternalizer.class);
List<Externalizer<Object>> result = new ArrayList<>(defaultExternalizers.size() + loadedExternalizers.size());
result.addAll(defaultExternalizers);
result.addAll(loadedExternalizers);
return result;
}
代码示例来源:origin: google/data-transfer-project
public static Monitor loadMonitor() {
List<Monitor> monitors = new ArrayList<>();
ServiceLoader.load(MonitorExtension.class)
.iterator()
.forEachRemaining(
extension -> {
extension.getMonitor();
extension.initialize();
monitors.add(extension.getMonitor());
});
return monitors.isEmpty()
? new ConsoleMonitor(DEBUG)
: new MultiplexMonitor((Monitor[]) monitors.toArray());
}
代码示例来源:origin: eclipse-vertx/vert.x
public static <T> Collection<T> loadFactories(Class<T> clazz, ClassLoader classLoader) {
List<T> list = new ArrayList<>();
ServiceLoader<T> factories;
if (classLoader != null) {
factories = ServiceLoader.load(clazz, classLoader);
} else {
// this is equivalent to:
// ServiceLoader.load(clazz, TCCL);
factories = ServiceLoader.load(clazz);
}
if (factories.iterator().hasNext()) {
factories.iterator().forEachRemaining(list::add);
return list;
} else {
// By default ServiceLoader.load uses the TCCL, this may not be enough in environment dealing with
// classloaders differently such as OSGi. So we should try to use the classloader having loaded this
// class. In OSGi it would be the bundle exposing vert.x and so have access to all its classes.
factories = ServiceLoader.load(clazz, ServiceHelper.class.getClassLoader());
if (factories.iterator().hasNext()) {
factories.iterator().forEachRemaining(list::add);
return list;
} else {
return Collections.emptyList();
}
}
}
}
代码示例来源:origin: ch.qos.logback/logback-classic
public static <T> T loadFromServiceLoader(Class<T> c) {
ServiceLoader<T> loader = ServiceLoader.load(c, getServiceLoaderClassLoader());
Iterator<T> it = loader.iterator();
if (it.hasNext())
return it.next();
return null;
}
代码示例来源:origin: apache/ignite
@Override public List<PluginProvider> run() {
List<PluginProvider> providers = new ArrayList<>();
ServiceLoader<PluginProvider> ldr = ServiceLoader.load(PluginProvider.class);
for (PluginProvider provider : ldr)
providers.add(provider);
return providers;
}
});
代码示例来源:origin: wildfly/wildfly
private static List<KeyFormat<?>> load(ClassLoader loader) {
List<KeyFormat<?>> keyFormats = new LinkedList<>();
for (KeyFormat<?> keyFormat : ServiceLoader.load(KeyFormat.class, loader)) {
keyFormats.add(keyFormat);
}
List<KeyFormat<?>> result = new ArrayList<>(keyFormats.size() + 6);
// Add key formats for common key types
result.add(new SimpleKeyFormat<>(String.class, Function.identity()));
result.add(new SimpleKeyFormat<>(Byte.class, Byte::valueOf));
result.add(new SimpleKeyFormat<>(Short.class, Short::valueOf));
result.add(new SimpleKeyFormat<>(Integer.class, Integer::valueOf));
result.add(new SimpleKeyFormat<>(Long.class, Long::valueOf));
result.add(new SimpleKeyFormat<>(UUID.class, UUID::fromString));
result.addAll(keyFormats);
return result;
}
}
代码示例来源:origin: javax.enterprise/cdi-api
private static void findAllProviders() {
ServiceLoader<CDIProvider> providerLoader;
Set<CDIProvider> providers = new TreeSet<>(Comparator.comparingInt(CDIProvider::getPriority).reversed());
providerLoader = ServiceLoader.load(CDIProvider.class, CDI.class.getClassLoader());
if(! providerLoader.iterator().hasNext()) {
throw new IllegalStateException("Unable to locate CDIProvider");
}
try {
providerLoader.forEach(providers::add);
} catch (ServiceConfigurationError e) {
throw new IllegalStateException(e);
}
CDI.discoveredProviders = Collections.unmodifiableSet(providers);
}
代码示例来源:origin: SonarSource/sonarqube
Set<CoreExtension> load(ClassLoader classLoader) {
ServiceLoader<CoreExtension> loader = ServiceLoader.load(CoreExtension.class, classLoader);
return ImmutableSet.copyOf(loader.iterator());
}
}
代码示例来源:origin: swagger-api/swagger-core
@Override
public boolean exists(String path) {
try {
ServiceLoader<OpenAPIConfigBuilder> loader = ServiceLoader.load(OpenAPIConfigBuilder.class);
if (loader.iterator().hasNext()) {
loader.iterator().next();
return true;
}
return false;
} catch (Exception e) {
return false;
}
}
}
代码示例来源:origin: apache/geode
private static List<ClientProtocolService> initializeProtocolServices() {
List<ClientProtocolService> resultList = new LinkedList<>();
for (ClientProtocolService clientProtocolService : ServiceLoader
.load(ClientProtocolService.class)) {
resultList.add(clientProtocolService);
}
return resultList;
}
代码示例来源:origin: oracle/helidon
private PolicyValidator(Builder builder) {
//first find all services
ServiceLoader<PolicyExecutorService> services = ServiceLoader.load(PolicyExecutorService.class);
for (PolicyExecutorService service : services) {
executors.add(service.instantiate(builder.config.get(service.configKey())));
}
//then add explicit
this.executors.addAll(builder.executors);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testServiceListFactoryBean() {
assumeTrue(ServiceLoader.load(DocumentBuilderFactory.class).iterator().hasNext());
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
RootBeanDefinition bd = new RootBeanDefinition(ServiceListFactoryBean.class);
bd.getPropertyValues().add("serviceType", DocumentBuilderFactory.class.getName());
bf.registerBeanDefinition("service", bd);
List<?> serviceList = (List<?>) bf.getBean("service");
assertTrue(serviceList.get(0) instanceof DocumentBuilderFactory);
}
代码示例来源:origin: neo4j/neo4j
/**
* @return instances of all classes with loadable configuration options
*/
static List<LoadableConfig> allConfigClasses()
{
return Iterators.stream( ServiceLoader.load( LoadableConfig.class ).iterator() ).collect( Collectors.toList() );
}
}
代码示例来源:origin: org.assertj/assertj-core
public static <SERVICE> SERVICE get(Class<SERVICE> serviceType, SERVICE defaultValue) {
Iterator<SERVICE> services = ServiceLoader.load(serviceType, Services.class.getClassLoader()).iterator();
SERVICE result = services.hasNext() ? services.next() : defaultValue;
if (services.hasNext()) {
result = defaultValue;
System.err.println(format("Found multiple implementations for the service provider %s. Using the default: %s",
serviceType, result.getClass()));
}
return result;
}
}
代码示例来源:origin: apache/geode
private List<DistributedSystemService> initializeServices() {
ServiceLoader<DistributedSystemService> loader =
ServiceLoader.load(DistributedSystemService.class);
List<DistributedSystemService> services = new ArrayList<>();
for (DistributedSystemService service : loader) {
services.add(service);
}
return services;
}
代码示例来源:origin: oracle/helidon
private AbacProvider(Builder builder) {
ServiceLoader<AbacValidatorService> services = ServiceLoader.load(AbacValidatorService.class);
for (AbacValidatorService service : services) {
validators.add(service.instantiate(builder.config.get(service.configKey())));
}
this.validators.addAll(builder.validators);
Set<Class<? extends Annotation>> annotations = new HashSet<>();
Set<String> configKeys = new HashSet<>();
Set<Class<? extends AbacValidatorConfig>> customObjects = new HashSet<>();
validators.forEach(v -> {
annotations.addAll(v.supportedAnnotations());
configKeys.add(v.configKey());
customObjects.add(v.configClass());
});
this.supportedAnnotations = Collections.unmodifiableSet(annotations);
this.supportedConfigKeys = Collections.unmodifiableSet(configKeys);
this.supportedCustomObjects = Collections.unmodifiableSet(customObjects);
this.failOnUnvalidated = builder.failOnUnvalidated;
this.failIfNoneValidated = builder.failIfNoneValidated;
}
内容来源于网络,如有侵权,请联系作者删除!