简单的 Java“服务提供者框架”?
但是如何让各个服务提供者(例如一堆默认提供者 + 一些自定义提供者)进行自我注册?
interface FooAlgorithm
{
/* methods particular to this class of algorithms */
}
interface FooAlgorithmProvider
{
public FooAlgorithm getAlgorithm(Configuration c);
}
class FooAlgorithmRegistry
{
private FooAlgorithmRegistry() {}
static private final Map<String, FooAlgorithmProvider> directory =
new HashMap<String, FooAlgorithmProvider>();
static public FooAlgorithmProvider getProvider(String name)
{
return directory.get(serviceName);
}
static public boolean registerProvider(String name,
FooAlgorithmProvider provider)
{
if (directory.containsKey(name))
return false;
directory.put(name, provider);
return true;
}
}
例如,如果我编写自定义类 MyFooAlgorithm 和 MyFooAlgorithmProvider 来实现 FooAlgorithm,并将它们分发到 jar 中,是否有任何方法可以自动调用 registerProvider,或者使用该算法的客户端程序必须显式调用 FooAlgorithmRegistry.registerProvider( )对于他们想要使用的每个类?
I refer to "service provider framework" as discussed in Chapter 2 of Effective Java, which seems like exactly the right way to handle a problem I am having, where I need to instantiate one of several classes at runtime, based on a String
to select which service, and an Configuration
object (essentially an XML snippet):
But how do I get the individual service providers (e.g. a bunch of default providers + some custom providers) to register themselves?
interface FooAlgorithm
{
/* methods particular to this class of algorithms */
}
interface FooAlgorithmProvider
{
public FooAlgorithm getAlgorithm(Configuration c);
}
class FooAlgorithmRegistry
{
private FooAlgorithmRegistry() {}
static private final Map<String, FooAlgorithmProvider> directory =
new HashMap<String, FooAlgorithmProvider>();
static public FooAlgorithmProvider getProvider(String name)
{
return directory.get(serviceName);
}
static public boolean registerProvider(String name,
FooAlgorithmProvider provider)
{
if (directory.containsKey(name))
return false;
directory.put(name, provider);
return true;
}
}
e.g. if I write custom classes MyFooAlgorithm and MyFooAlgorithmProvider to implement FooAlgorithm, and I distribute them in a jar, is there any way to get registerProvider to be called automatically, or will my client programs that use the algorithm have to explicitly call FooAlgorithmRegistry.registerProvider() for each class they want to use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以让客户端 JAR 在某个类中的静态初始化程序块中注册提供程序,您知道该类将在 FooAlgorithmRegistry.getProvider() 之前调用,例如:
但是,可能很难找到一个保证这将在工厂的访问器方法之前运行(静态初始化器保证在类首次加载时运行一次且仅一次)。
You could have the client JAR register the providers in a static initializer block within some class that you know will be called before
FooAlgorithmRegistry.getProvider()
, something like:But, it might be pretty hard to find a way to guarantee that this will run (static initializers are guaranteed to be run once and only once, when the class is first loaded) before the accessor method of the factory.
我认为您需要创建一个 META-INF/services/filled.qualified.ClassName 并在那里列出内容,但我不记得规范(JAR 文件规范 或 这个)。
Java 架构师的实用 API 设计自白一书的第 8 章是关于 SPI 的。
ServiceLoader 可能会帮助您列出可用的实现。 例如使用 PersistenceProvider 接口:
I think you need to create a META-INF/services/fully.qualified.ClassName and list things there, but I don't remember the spec (JAR File Specification or this).
The Practical API design confessions of a Java architect book chapter 8 is about SPI.
The ServiceLoader might help you to list available implementations. For example with the PersistenceProvider interface: