[英]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.