2012-04-03 31 views
1

我正在飛行一個基於CXF的Web服務,與CXF Web站點http://cxf.apache.org/docs/jax-ws-configuration.html上的示例不同。這項服務基於下面的示例上下文中執行端點:CXF/Spring將頂級服務名稱和名稱空間注入到JAX-WS接口中

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:jaxws="http://cxf.apache.org/jaxws" 
xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
     http://cxf.apache.org/jaxws 
     http://cxf.apache.org/schemas/jaxws.xsd"> 

    <jaxws:endpoint id="myServiceMembersImpl" 
     implementor="#myService" 
     endpointName="e:MyServiceMembersEndpoint" 
     serviceName="s:MyServiceMembersService" 
     address="http://localhost:8080/myservicemembers" 
     xmlns:e="http://localhost:8080/myservicemembers/ns" 
     xmlns:s="http://localhost:8080/myservicemembers/ns"/> 

</beans> 

然後,當然,還有就是Java ...

接口:

package com.me.service; 

@WebService 
public interface MyService { 

String MEMBER = "MEMBER"; 

@WebResult(name = MEMBER) 
Member getMember(@WebParam(name = "memberId") long memberId) throws Exception; 
    // ... 
    // more interface declarations 
    // ... 

} // end interface 

,並實現:

package com.me.service.impl; 

@WebService(endpointInterface = "com.me.service.MyService") 
@Path("/") 
public class MyServiceMembersImpl implements MyService { 

@GET 
@Path("/{id}") 
@Consumes({ APP_JSON, APP_XML }) 
@Produces({ APP_JSON, APP_XML }) 
@Transactional(readOnly = true, propagation = Propagation.REQUIRED) 
public Member getMember(@PathParam("id") final long memberId) throws Exception { 

     // ... 
     // business logic 
     // ... 
     return theMember; 

    } // end method 

} // end class 

其中返回一個WSDL,其起點有點像這樣:

<?xml version="1.0" encoding="UTF-8" ?> 
<wsdl:definitions name="MyServiceImplService" 
    targetNamespace="http://localhost:8080/myservicemembers/ns" 
    xmlns:ns1="**http://service.me.com/**" 
    xmlns:ns2="http://schemas.xmlsoap.org/soap/http" 
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
    xmlns:tns="http://localhost:8080/myservicemembers/ns" 
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <wsdl:import location="http://localhost:8080/myservicemembers?wsdl=**MyService**.wsdl" 
     namespace="**http://service.me.com/**" /> 
     <wsdl:binding name="MyServiceImplServiceSoapBinding" type="ns1:**MyService**"> 
<!-- ... --> 
</wsdl:definitions> 

使用應用程序上下文中的「jaxws:endpoint」元素更改端點設置非常簡單。這充實了服務名稱,端點名稱和其他字段。但是,頂級接口在WSDL文件中仍然有發言權。上面的STARRED項目來自頂層界面。 如何將值注入到targetNamespace和serviceName的頂級接口?

我這樣做的好理由包括:(1)不想公開WSDL中的包名;(2)當應用程序在部署跑道上移動時想切換命名空間。因此,我不能使用註釋,因爲這些是編譯時的值,我不能用屬性佔位符替換它們,也不會在生產層中重新編譯我的代碼。

+0

我歡迎任何解決方案,包括靜態塊和直接存取,只要他們能與屬性替代來設置。 – ingyhere 2012-04-03 21:10:34

回答

1

您可以通過編程方式在您的Spring配置中使用JaxWsServerFactoryBean而不是使用<jaxws:endpoint>來創建服務。以編程方式進行創建會給您更多的控制權。

例如:

@Autowired 
var myServiceImpl: MyService = _ 

val propMap = mutable.HashMap[String, AnyRef]("org.apache.cxf.logging.FaultListener"->faultListener.asInstanceOf[AnyRef]) 

val sf = new JaxWsServerFactoryBean 
sf.setServiceBean(myServiceImpl) 
sf.setAddress("/myservice") 
sf.setServiceName(new QName(WEB_SERVICE_NAMESPACE, "myService", "MyService")) 
sf.setProperties(propMap) 
sf.create 
+0

非常感謝這些信息。我正在看這個。我想發佈這個鏈接,我認爲這完成了CXF非Spring實現的圖片:http://www.axlrosen.net/stuff/cxf-without-spring.html。 – ingyhere 2012-04-04 06:06:44

相關問題