Java中的提供者getService()方法

使用getService()类java.security.Provider中的方法可以获得可以描述关于任何算法的Provider实现的服务。此方法需要两个参数,即所需的服务类型和所需服务的算法名称。

演示此的程序如下所示-

示例

import java.security.*;
import java.util.*;
public class Demo {
   public static void main(String[] argv) throws Exception {
      try {
         Signature sign = Signature.getInstance("SHA256withDSA");
         Provider p = sign.getProvider();
         Provider.Service s = p.getService("Signature", sign.getAlgorithm());
         System.out.println("The Service for the Provider is: " + s);
      } catch (NoSuchAlgorithmException e) {
         System.out.println("Error!!! NoSuchAlgorithmException");
      }
   }
}

输出结果

The Service for the Provider is: SUN: Signature.SHA256withDSA -> sun.security.provider.DSA$SHA256withDSA
aliases: [OID.2.16.840.1.101.3.4.3.2, 2.16.840.1.101.3.4.3.2]
attributes: {KeySize=2048, SupportedKeyClasses=java.security.interfaces.DSAPublicKey|java.security.interfaces.DSAPrivateKey}

现在让我们了解上面的程序。

getService()方法用于获取可以描述有关任何算法的Provider实现的服务。然后显示。如果算法名称错误,则会引发异常NoSuchAlgorithmException。演示的代码片段如下-

try {
   Signature sign = Signature.getInstance("SHA256withDSA");
   Provider p = sign.getProvider();
   Provider.Service s = p.getService("Signature", sign.getAlgorithm());
   System.out.println("The Service for the Provider is: " + s);
} catch (NoSuchAlgorithmException e) {
   System.out.println("Error!!! NoSuchAlgorithmException");
}