如何在java中覆盖服务提供者 [英] how to override a service provider in java

查看:218
本文介绍了如何在java中覆盖服务提供者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个更为常见的问题:
我正在使用xstream和woodstox,woodstox附带了一个服务提供程序,用于注册com.ctc.wstx.stax的woodstox jar中的javax.xml.stream.XMLOutputFactory。 WstxOutputFactory。
我想提供自己的javax.xml.stream.XMLOutputFactory,并且在类路径中仍然有woodstox jar。我知道我可以提供我自己的系统属性javax.xml.stream.XMLOutputFactory,但我正试图从我们的开发团队中解脱麻烦,并在我的jar中使用服务文件或者在我的战争中使用META -INF / services文件夹。查看javax.xml.stream.FactoryFinder的代码如何确保我的
META-INF / services / javax.xml.stream.XMLOutputFactory文件将是FactoryFinder使用的文件?

This is more a general question by example: I'm using xstream and woodstox, woodstox comes with a service provider for javax.xml.stream.XMLOutputFactory in woodstox jar registering com.ctc.wstx.stax.WstxOutputFactory. I want to provide my own javax.xml.stream.XMLOutputFactory and still have woodstox jar in the classpath. I know I can provide my own with the system property javax.xml.stream.XMLOutputFactory , but I'm trying to take off the hassle from our dev ops team and do it with a service file in my jar or maybe in my war's META-INF/services folder. looking the code of javax.xml.stream.FactoryFinder how can I make sure that my META-INF/services/javax.xml.stream.XMLOutputFactory file will be the one used by FactoryFinder?

我们将xstream与camel一起使用,无法找到将工厂注入XStreamDataFormat的方法

we use xstream with camel and could not find a way to inject the factory to XStreamDataFormat

推荐答案

首先:我强烈建议简化您的生活而不是使用它,而不是依赖于JDK SPI接口。它实际上没有增加注入 XMLInputFactory 和/或 XMLOutputFactory 的价值。对于注射,您可以使用Guice(或Spring);或者只是手动传递它。由于这些工厂没有自己的依赖关系,这很容易。

First: instead of relying on JDK SPI interface, I strongly recommend simplifying your life and NOT using it. It really adds no value over injecting XMLInputFactory and/or XMLOutputFactory yourself. For injection you can use Guice (or Spring); or just pass it manually. Since these factories do not have dependencies of their own, this is easy.

但是如果选择(或必须)使用 XMLInputFactory.newInstance( ),您可以为javax.xml.stream.XMLOutputFactory和javax.xml.stream.XMLInputFactory定义一个System属性。

But if choose to (or have to) use XMLInputFactory.newInstance(), you can define a System property for "javax.xml.stream.XMLOutputFactory" and "javax.xml.stream.XMLInputFactory".

那么为什么不使用JDK方法呢?多种原因:

So why not use JDK approach? Multiple reasons:


  1. 它增加了开销:如果你没有指定系统属性,它将必须扫描整个类路径,并使用大应用程序服务器大多数解析需要10x-100x

  2. 实现的优先级是未定义的:如果你在classpath中有多个,你会得到哪一个?谁知道......(注意:当你在类路径中添加新的jar时它甚至可能会改变)

  3. 你很可能通过传递依赖获得多个impl

不幸的是,Oracle似乎仍然坚持要为注册服务提供商添加这种已知错误的方法。为什么?可能是因为他们没有他们自己的DI lib /框架(Guice是谷歌,Spring by Springsource),他们往往很容易控制饥饿。

Unfortunately, Oracle still seems to insist on adding this known-faulty method for registering service providers. Why? Probably because they do not have a DI lib/framework of their own (Guice is by google, Spring by Springsource), and they tend to be pretty control hungry.

这篇关于如何在java中覆盖服务提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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