ServiceRegistry 构造函数在具体类上失败并出现 ClassCastException
尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
然而,我以前从未使用过这个 SPI 类,来自 API 文档,
ServiceRegistry
的构造函数采用带有类别的Iterator
,而不是提供程序。您可能想使用registerServiceProviders()
来注册您的提供程序?I have never used this SPI class before, however, from the API documentation, the constructor of
ServiceRegistry
takes in anIterator
with categories, not providers. You may want to register your providers withregisterServiceProviders()
instead?yclian,谢谢你的“头疼”。
构造函数确实需要“类别”列表,它们是注册表可以搜索的接口。一旦我更正了该部分,并按照您所说填充了 ServiceRegistry,其余的工作就很好了。
新代码:
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: