Overview
Files in the META-INF/services directory are service provider configuration files. A service is a well-known set of interfaces and (usually abstract) classes. A service provider is a specific implementation of a service. The classes in a provider typically implement the interfaces and subclass the classes defined in the service itself. Service providers may be installed in an implementation of the Java platform in the form of extensions, that is, jar files placed into any of the usual extension directories. Providers may also be made available by adding them to the applet or application class path or by some other platform-specific means.
A service is represented by an abstract class. A provider of a given service contains one or more concrete classes that extend this service class with data and code specific to the provider. This provider class will typically not be the entire provider itself but rather a proxy that contains enough information to decide whether the provider is able to satisfy a particular request together with code that can create the actual provider on demand. The details of provider classes tend to be highly service-specific; no single class or interface could possibly unify them, so no such class has been defined. The only requirement enforced here is that provider classes must have a zero-argument constructor so that they may be instantiated during lookup.
Provider-Configuration File
A service provider identifies itself by placing a provider-configuration file in the resource directory META-INF/services. The file's name should consist of the fully-qualified name of the abstract service class. The file should contain a newline-separated list of unique concrete provider-class names. Space and tab characters, as well as blank lines, are ignored. The comment character is '#' (0x23); on each line all characters following the first comment character are ignored. The file must be encoded in UTF-8.
ExampleSuppose we have a service class named java.io.spi.CharCodec. It has two abstract methods:
public abstract CharEncoder getEncoder(String encodingName);
public abstract CharDecoder getDecoder(String encodingName);
Each method returns an appropriate object or null if it cannot translate the given encoding. Typical CharCodec providers will support more than one encoding.
If sun.io.StandardCodec is a provider of the CharCodec service then its jar file would contain the file META-INF/services/java.io.spi.CharCodec. This file would contain the single line:
sun.io.StandardCodec # Standard codecs for the platform
To locate an encoder for a given encoding name, the internal I/O code would do something like this:
CharEncoder getEncoder(String encodingName) {
Iterator ps = Service.providers(CharCodec.class);
while (ps.hasNext()) {
CharCodec cc = (CharCodec)ps.next();
CharEncoder ce = cc.getEncoder(encodingName);
if (ce != null)
return ce;
}
return null;
}
The provider-lookup mechanism always executes in the security context of the caller. Trusted system code should typically invoke the methods in this class from within a privileged security context.
实例
例如新指定一个DNS service进行测试,需要创建一个实现了NameServiceDescriptor 的类,将相关类打成jar包。并且在jar 中创建META-INF/services 目录,目录中新建文件:sun.net.spi.nameservice.NameServiceDescriptor
文件内容:runtime.BogusNameServiceDescriptor
在测试调用的时候,System.setProperty("sun.net.spi.nameservice.provider.1", "throwing,Bogus");
运行OK
分享到:
相关推荐
Net SAML2 Service Provider Framework
File System Service Provider,文件系统服务提供者,JAVA SE中使用JNDI时常用
File System Service Provider for JNDI。This is the Early Access 4 release of the JNDI/File system service provider. It contains two jars,they are fscontext.jar and providerutil.jar. The doc is ...
CLC-CCIE Service Provider v5.0-Practice Lab v1.0
Spring Cloud服务提供方,向Eureka注册,结合Jersy
浪潮ERP-Financial Service Provider财务公司解决方案是浪潮结合多年来在财务公司领域的丰富经验,根据目前财务公司所面临的主要问题设计并提出的财务公司整体信息化解决方案。它针对财务公司全面业务应用及管理体系...
Android SM2、SM3、SM4 算法支持 Service Provider 及证书制作软件包 国密算法 JCAJCE Service Provider,适应版本 Android 4.2.2~7.0 支持 SM2 的 KeyFactory、KeyPairGenerator、Cipher、Signature、X.509 ...
微软文档:什么是 Service Provider Foundation?-36页
随着国产密码算法不断涌现,许多公司都提供了CSP相关国密算法的支持,但大部分CSP实现的内容仅是一个空...本文介绍的是既采用CSP架构,且不用硬件的国密算法——由西电捷通研发的HUFU Cipher Service Provider技术。
Networkers2009:BRKIPM-3300 - Advanced Service Provider IPv6 Deployment
NULL 博文链接:https://garyjiao.iteye.com/blog/2244981
Laravel开发-service-provider Laravel 5类固醇服务提供商。很多额外的。
博文链接:https://jiangzhen1984.iteye.com/blog/225126
CSP是Cryptographic Service Provider的简写,是微软专为Windows系列操作系统制定的底层加密接口,用于管理硬件或软件形式的加密设备,实现数据加密、解密,数字签名、验证和数据摘要(即HASH)等。
里面包含了Winsock SPI的函数枚举协议和分层服务提供者
Allpay-4-laravel是一个将欧付宝官方提供的SDK包装好的Service Provider。 让开发人员省去一再初始化SDK、输入设定值的麻烦。 欧付宝官方SDK请见 注意:AllPay.Payment.Integration.php(整个Allpay SDK的核心档案...
主要介绍了laravel5创建service provider和facade的方法,实例分析了laravel创建service、provider和facade类的具体步骤与实现技巧,需要的朋友可以参考下
主要给大家介绍了关于Laravel Service Provider开发设置延迟加载时遇到的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起看看吧。
服务提供者Service Provider 是用于远程客户端测试的各种服务(REST、WS、JMS、EJB 等)的 Java EE 应用程序。可用的 REST 服务 (JAX-RS) 网址: AcronymsDictionary - /acronym 回声 - /回声HttpStatus - /status ...