ServiceRegistry 构造函数在具体类上失败并出现 ClassCastException

发布于 2024-09-11 12:30:56 字数 1314 浏览 6 评论 0原文

尝试使用 javax.imageio.spi.ServiceRegistry 动态注册 HttpServlet 的请求处理器:

    private static final Class PROVIDER_CLASS = IRequestProcessor.class;

 private void loadProviders() throws ClassNotFoundException {
  _serviceRegistry = new ServiceRegistry(ServiceRegistry.lookupProviders(PROVIDER_CLASS));
 }

我得到的错误是:

 java.lang.ClassCastException: org.confused.servlet.GetStandardCodesProcessor
javax.imageio.spi.ServiceRegistry.<init>(ServiceRegistry.java:103
org.confused.servlet.MyServlet.loadProviders(.java:100)
 org.confused.servlet.MyServlet.checkProviders(.java:106)
 org.confused.servlet.MyServlet.service(.java:44)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

GetStandardCodesProcessor 实现IRequestProcessor。 调用 ServiceRegistry.lookupProviders() 从文件 META-INF/services/org.confused.servlet.IRequestProcessor 获取类列表。 看来我错过了如何从 ServiceRegistry.lookupProviders() 传递迭代器的细微差别。

此页面几乎显示了我正在做的事情,尽管分配了从lookupProviders() 返回到一个无类型迭代器,该迭代器又被传递到ServiceRegistry 构造函数。该技术给我带来了同样的错误。

最后,如果重要的话,我将在 Eclipse Gallileo(内部版本 20100218-1602)中运行它。

问候, 德鲁

Trying to use javax.imageio.spi.ServiceRegistry to dynamically register request processors for an HttpServlet:

    private static final Class PROVIDER_CLASS = IRequestProcessor.class;

 private void loadProviders() throws ClassNotFoundException {
  _serviceRegistry = new ServiceRegistry(ServiceRegistry.lookupProviders(PROVIDER_CLASS));
 }

The error I get is:

 java.lang.ClassCastException: org.confused.servlet.GetStandardCodesProcessor
javax.imageio.spi.ServiceRegistry.<init>(ServiceRegistry.java:103
org.confused.servlet.MyServlet.loadProviders(.java:100)
 org.confused.servlet.MyServlet.checkProviders(.java:106)
 org.confused.servlet.MyServlet.service(.java:44)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

The class GetStandardCodesProcessor implements IRequestProcessor.
The call ServiceRegistry.lookupProviders() is getting a list of classes from the file META-INF/services/org.confused.servlet.IRequestProcessor.
It seems I am missing a nuance about how to pass in the iterator from ServiceRegistry.lookupProviders().

This page shows pretty much what I'm doing, albeit assigning the the return from lookupProviders() to an untyped Iterator, which in turn gets passed to the ServiceRegistry constructor. That technique gives the same error for me.

Lastly, I am running this in Eclipse Gallileo (build 20100218-1602), if that matters.

Regards,
Drew

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

只是一片海 2024-09-18 12:30:56

然而,我以前从未使用过这个 SPI 类,来自 API 文档ServiceRegistry 的构造函数采用带有类别的 Iterator,而不是提供程序。您可能想使用 registerServiceProviders() 来注册您的提供程序?

I have never used this SPI class before, however, from the API documentation, the constructor of ServiceRegistry takes in an Iterator with categories, not providers. You may want to register your providers with registerServiceProviders() instead?

北音执念 2024-09-18 12:30:56

yclian,谢谢你的“头疼”。

构造函数确实需要“类别”列表,它们是注册表可以搜索的接口。一旦我更正了该部分,并按照您所说填充了 ServiceRegistry,其余的工作就很好了。

新代码:

private void loadProviders() throws ClassNotFoundException {
    Iterator iterCategories = (Collections.singletonList(PROVIDER_CLASS)).iterator();
    _serviceRegistry = new ServiceRegistry(iterCategories);
    _serviceRegistry.registerServiceProviders(ServiceRegistry.lookupProviders(PROVIDER_CLASS));
}

yclian, thanks for the "bonk on the head".

The constructor is indeed expecting the list of 'categories', which are the interfaces that the registry can search for. Once I corrected that part, and populated the ServiceRegistry as you said, the rest works great.

The new code:

private void loadProviders() throws ClassNotFoundException {
    Iterator iterCategories = (Collections.singletonList(PROVIDER_CLASS)).iterator();
    _serviceRegistry = new ServiceRegistry(iterCategories);
    _serviceRegistry.registerServiceProviders(ServiceRegistry.lookupProviders(PROVIDER_CLASS));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文