简体   繁体   English

如何为Spring配置的基于Apache CXF的Web服务客户端提供服务器地址?

[英]How to provide server address to the Spring-configured Apache CXF-based web service client?

I'm experimenting with Apache CXF and have a question about the client part. 我正在试验Apache CXF,并对客户端部分有疑问。

Below is my current Spring configuration of the WS client of some com.example.customerservice.service.CustomerService : 下面是我目前的一些com.example.customerservice.service.CustomerService的WS客户端的Spring配置:

<jaxws:client
    name="com.example.customerservice.service.CustomerServiceClient"
    serviceName="customer:CustomerServiceService" endpointName="customer:CustomerServiceEndpoint"
    address="http://localhost:8080/CustomerServicePort"
    serviceClass="com.example.customerservice.service.CustomerService">
    <jaxws:features>
        <bean class="org.apache.cxf.feature.LoggingFeature" />
    </jaxws:features>
</jaxws:client>

As you see, the address attribute is configured statically. 如您所见, address属性是静态配置的。 This is not suitable for me because I don't know the server URL in advance. 这不适合我,因为我事先不知道服务器URL。 Moreover, in certain scenarios I'd like to use this client for different services which have different addresses. 此外,在某些情况下,我想将此客户端用于具有不同地址的不同服务。

Therefore static configuration of the server address in Spring is not appropriate. 因此,Spring中服务器地址的静态配置是不合适的。 So my question is - how can I make it dynamic? 所以我的问题是 - 我怎样才能让它变得动态?

  • At the moment my solution is to set a system property - something like baseUrl and inject it into the Spring config using the property placeholder configurer. 目前我的解决方案是设置一个系统属性 - 类似于baseUrl并使用属性占位符配置器将其注入Spring配置。
  • Another possibility would be to simply construct the client manually which I don't really like either. 另一种可能性是简单地手动构建客户端,我也不喜欢。

But I believe I'm really missing something. 但我相信我真的错过了一些东西。 Maybe there is a possibility of something like clientFactory.createClientFor("http://myserver:8080") ? 也许有可能像clientFactory.createClientFor("http://myserver:8080")

See post to CXF Users Mailing List . 请参阅CXF用户邮件列表的帖子。

You have a couple options: 你有几个选择:

1) If you want to leave your Spring context as is and change the address programmatically at runtime: 1)如果要保留Spring上下文,并在运行时以编程方式更改地址:

You can set a standard property in the request context. 您可以在请求上下文中设置标准属性。 Here is an example of how to do this programmatically. 以下是以编程方式执行此操作的示例。

BindingProvider bp = (BindingProvider)port; BindingProvider bp =(BindingProvider)端口; Map context = bp.getRequestContext(); Map context = bp.getRequestContext(); Object oldAddress = context.get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY); Object oldAddress = context.get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY); context.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, newAddress); context.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,newAddress);

When doing this, you should be aware of multi-threaded access to a client proxy. 执行此操作时,您应该了解对客户端代理的多线程访问。 See the CXF FAQ (Are JAX-WS client proxies thread safe?) 请参阅CXF FAQ(JAX-WS客户端代理线程是否安全?)

2) If you are willing/able to provide WSDL URLs and use the JAX-WS APIs, you can write portable code that will create a client proxy wired to an endpoint of your choosing. 2)如果您愿意/能够提供WSDL URL并使用JAX-WS API,您可以编写可移植代码,该代码将创建连接到您选择的端点的客户端代理。 You can use the "createdFromAPI" (Configuring a Spring Client (Option 1)) attribute in your Spring context file to still allow Spring based configuration of the programmatically constructed client proxy. 您可以使用Spring上下文文件中的“createdFromAPI”(配置Spring Client(选项1))属性来仍然允许以编程方式构造的客户端代理的基于Spring的配置。 I think that wildcards are also supported here so you should be able to configure a number of clients using a single entry in your Spring context. 我认为这里也支持通配符,因此您应该能够在Spring上下文中使用单个条目配置许多客户端。 This approach will get more complex if the endpoint namespaces/local names vary greatly between the endpoints you are trying to interact with. 如果端点名称空间/本地名称在您尝试与之交互的端点之间差别很大,则此方法将变得更加复杂。

3) Use org.apache.cxf.jaxws.JaxWsProxyFactoryBean programmatically as shown in the Spring configuration of Configuring a Spring Client (Option 2) [2]. 3)以编程方式使用org.apache.cxf.jaxws.JaxWsProxyFactoryBean,如配置Spring Client的Spring配置(选项2)[2]所示。 This lets you set the interface and address and create new client proxy instances at will. 这使您可以设置接口和地址,并随意创建新的客户端代理实例。 You may even want to configure a single instance of this factory with most properties already set in Spring and then inject it into your code where you can change the address and construct a new client proxy at will (providing for synchronized access to the factory bean of course). 您甚至可能希望配置此工厂的单个实例,其中大多数属性已在Spring中设置,然后将其注入到您的代码中,您可以在其中更改地址并随意构建新的客户端代理(提供对工厂bean的同步访问)课程)。 You can also cache the client proxies to avoid the expense of re-creating them repeatedly. 您还可以缓存客户端代理,以避免重复创建它们的费用。

http://cxf.apache.org/faq.html#FAQ-AreJAXWSclientproxiesthreadsafe%253F http://cxf.apache.org/docs/jax-ws-configuration.html http://cxf.apache.org/faq.html#FAQ-AreJAXWSclientproxiesthreadsafe%253F http://cxf.apache.org/docs/jax-ws-configuration.html

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

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