简单的Java“服务提供商框架"? [英] Simple Java "Service Provider frameworks"?

查看:83
本文介绍了简单的Java“服务提供商框架"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是有效的第2章中讨论的服务提供商框架" Java ,这似乎是解决我遇到的问题的正确方法,其中我需要在运行时实例化几个类之一,基于String选择哪个服务和一个Configuration对象(本质上是XML代码段):

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;
     }
 }

例如如果我编写自定义类MyFooAlgorithm和MyFooAlgorithmProvider来实现FooAlgorithm,并且将它们分布在jar中,则有什么方法可以自动调用registerProvider,或者使用该算法的客户端程序必须显式调用FooAlgorithmRegistry.registerProvider()他们想使用的每个班级?

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?

推荐答案

我认为您需要创建一个META-INF/services/fully.qualified.ClassName并在其中列出内容,但我不记得该规范( JAR文件规范

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).

Java架构师的实用API设计告白第8章是关于SPI的.

The Practical API design confessions of a Java architect book chapter 8 is about SPI.

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

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);
}

这篇关于简单的Java“服务提供商框架"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆