简单的 Java“服务提供者框架”?

发布于 2024-07-28 07:42:20 字数 1288 浏览 8 评论 0原文

我指的是 Effective Java 第 2 章,这似乎是处理我遇到的问题的正确方法,我需要在运行时实例化几个类之一,基于 String 来选择哪个服务,以及一个 Configuration 对象(本质上是一个 XML 片段):

但是如何让各个服务提供者(例如一堆默认提供者 + 一些自定义提供者)进行自我注册?

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白衬杉格子梦 2024-08-04 07:43:06

您可以让客户端 JAR 在某个类中的静态初始化程序块中注册提供程序,您知道该类将在 FooAlgorithmRegistry.getProvider() 之前调用,例如:

static {
    FooAlgorithmRegistry.registerProvider("test", new MyFooAlgorithmProvider());
}

但是,可能很难找到一个保证这将在工厂的访问器方法之前运行(静态初始化器保证在类首次加载时运行一次且仅一次)。

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:

static {
    FooAlgorithmRegistry.registerProvider("test", new MyFooAlgorithmProvider());
}

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.

轮廓§ 2024-08-04 07:42:55

我认为您需要创建一个 META-INF/services/filled.qualified.ClassName 并在那里列出内容,但我不记得规范(JAR 文件规范这个)。

Java 架构师的实用 API 设计自白一书的第 8 章是关于 SPI 的。

ServiceLoader 可能会帮助您列出可用的实现。 例如使用 PersistenceProvider 接口:

ServiceLoader<PersistenceProvider> loader = 
        ServiceLoader.load(PersistenceProvider.class);
Iterator<PersistenceProvider> implementations = loader.iterator();
while(implementations.hasNext()) {
    PersistenceProvider implementation = implementations.next();
    logger.info("PersistenceProvider implementation: " + implementation);
}

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:

ServiceLoader<PersistenceProvider> loader = 
        ServiceLoader.load(PersistenceProvider.class);
Iterator<PersistenceProvider> implementations = loader.iterator();
while(implementations.hasNext()) {
    PersistenceProvider implementation = implementations.next();
    logger.info("PersistenceProvider implementation: " + implementation);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文