简体   繁体   English

java.util.ServiceLoader只加载一个服务实例

[英]does java.util.ServiceLoader load only a single instance of a service

I want to ask whether the ServiceLoader.load() can help me create multiple instances of the loaded service? 我想询问ServiceLoader.load()是否可以帮助我创建加载服务的多个实例?

I have been doing some testing and its seems it can only load one instance of the service at a time. 我一直在做一些测试,它似乎一次只能加载一个服务实例。 If it is possible to have multiple instances, can someone explain how I can achieve this? 如果有可能有多个实例,有人可以解释我如何实现这一目标吗?

You don't want to use the ServiceLoader itself to create multiple copies. 您不希望使用ServiceLoader本身来创建多个副本。 You could , using the reload method of the loader, but that is not the intended usage of it, probably doesn't perform very well, and also has the side effect that other implementations are reloaded as well. 可以使用reload器的reload方法,但这不是它的预期用途,可能执行得不好,并且还具有其他实现也重新加载的副作用。

More likely, what you want to do is instead of create a factory class (or some similar concept), which itself creates the instances of the class you actually want to use, in the end, and use the service loader to load such factory instances instead. 更有可能的是,你想要做的不是创建一个工厂类(或一些类似的概念),它最终会创建你真正想要使用的类的实例,并使用服务加载器来加载这样的工厂实例代替。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过java.util.ServiceLoader加载通用服务实现 - Loading generic service implementations via java.util.ServiceLoader java.util.ServiceLoader如何工作? - how java.util.ServiceLoader works? 使用 java.util.serviceLoader 动态加载多个 jar 文件 - loading multiple jar files dynamically using java.util.serviceLoader PowerMock:java.lang.IllegalAccessError:模拟java.util.ServiceLoader时的java.lang.Class - PowerMock: java.lang.IllegalAccessError: java.lang.Class when mocking java.util.ServiceLoader 如何阻止maven-shade-plugin阻止opensaml-impl类型的java.util.ServiceLoader初始化 - How to stop maven-shade-plugin from blocking java.util.ServiceLoader initialization of opensaml-impl types java.util.ServiceLoader.load() 函数没用,只返回空结果 - java.util.ServiceLoader.load() function is useless and only returns empty result 如何限制 Java ServiceLoader 只加载测试包中定义的服务提供者 - How to limit Java ServiceLoader to only load service providers defined in test packages 对 java 9 ServiceLoader::load 方法和如何提供服务实现的方式感到困惑 - Confused about java 9 ServiceLoader::load method and the way how to provide a service impl 嵌入式glassfish:java.lang.NoClassDefFoundError:java / util / ServiceLoader - embedded glassfish: java.lang.NoClassDefFoundError: java/util/ServiceLoader ServiceLoader是否真的动态加载提供程序? - Does ServiceLoader really load providers dynamically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM