我正在試驗Apache CXF並對客戶端部分有疑問。如何向Spring配置的基於Apache CXF的Web服務客戶端提供服務器地址?
下面是一些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>
正如你看到的,address
屬性靜態配置。這不適合我,因爲我不提前知道服務器URL。此外,在某些情況下,我希望將此客戶端用於具有不同地址的不同服務。
因此,Spring中服務器地址的靜態配置並不合適。所以我的問題是 - 我怎樣才能使它動態?
- 目前我的解決方案是設置系統屬性 - 類似於
baseUrl
,並使用屬性佔位符配置器將其注入到Spring配置中。 - 另一種可能性是簡單地手動構建我不太喜歡的客戶端。
但我相信我真的錯過了一些東西。也許有類似clientFactory.createClientFor("http://myserver:8080")
的可能性?
大衛你好, 是的,謝謝你,我已經看到了。我今天試了一下並報告回來。 – lexicore 2010-04-21 17:59:15
我終於選擇通過jaxWsProxyFactoryBean進行編程創建: final JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean(); jaxWsProxyFactoryBean.setServiceClass(CustomerService.class); jaxWsProxyFactoryBean.setAddress(webAppEnvironment.getBaseUrl() \t \t \t +「/ CustomerServicePort」); 這就是三行代碼。目前我的目的夠了。 – lexicore 2010-04-21 20:07:57