简体   繁体   English

如何配置Apache CXF通过非Spring配置文件使用单个JAXBContext?

[英]How to configure Apache CXF to use a single JAXBContext through a Non-Spring configuration file?

I have read that sharing a single JAXBContext object across multiple threads can greatly reduce the CPU usage of an application that uses JAXB. 我已经读过,跨多个线程共享一个JAXBContext对象可以大大降低使用JAXB的应用程序的CPU使用率。 A proof of this is here . 这方面的一个证明就是这里 We are currently using Apache CXF for developing web services. 我们目前正在使用Apache CXF来开发Web服务。 Apache CXF is bound to use JAXB by default. Apache CXF默认使用JAXB。

  • According to this , Apache CXF can be configured to use a single instance of JAXBContext through a configuration file. 根据 ,阿帕奇CXF可以被配置为通过一个配置文件来使用的JAXBContext的单个实例。
  • According to this , we can supply a configuration file to CXF. 根据这个 ,我们可以提供一个配置文件CXF。 This configuration file is a Spring configuration file. 此配置文件是Spring配置文件。
  • According to this , Apache CXF can be used without Spring. 根据 ,Apache的CXF可以不用弹簧配合使用。

Given the above information, how do I configure Apache CXF to use a single JAXBContext without using a Spring based configuration file since I am looking at a code base that uses the servlet transport without Spring? 鉴于以上信息,如何在不使用基于Spring的配置文件的情况下将Apache CXF配置为使用单个JAXBContext,因为我正在查看使用没有Spring的servlet传输的代码库? Are there any other configuration parameters that can be added for the JAXBContext that will further improve the performance? 是否可以为JAXBContext添加任何其他配置参数以进一步提高性能?

You could try the solution proposed here , based on the definition of a Global JAXBContext, but I'm not sure it will play well with jax-rs. 您可以根据Global JAXBContext的定义尝试此处提出的解决方案,但我不确定它是否能与jax-rs一起使用。

A simpler solution without spring would be to define your own @Provider , extending org.apache.cxf.jaxrs.provider.JAXBElementProvider , and register it in cxf, using the openejb-jar.xml file for instance, if you are working with tomee . 没有spring的更简单的解决方案是定义你自己的@Provider ,扩展org.apache.cxf.jaxrs.provider.JAXBElementProvider ,并使用openejb-jar.xml文件在cxf中注册它,如果你正在使用tomee

Your provider would look like: 您的提供商看起来像:

@Provider
public class MyJAXBElementProvider<T> extends JAXBElementProvider<T> {
  public MyJAXBElementProvider() {
    super();
    setSingleJaxbContext(true);
    setExtraClass(...);
  }
}

and it would need to be registered in the WEB-INF/openejb-jar.xml file in your webapp like: 它需要在你的webapp中的WEB-INF/openejb-jar.xml文件中注册,如:

<?xml version="1.0"?>
<openejb-jar>
  <pojo-deployment class-name="jaxrs-application">
    <properties>
    cxf.jaxrs.providers = mypackage.MyJAXBElementProvider
    </properties>
  </pojo-deployment>
</openejb-jar>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM