このコンテンツは選択した言語では利用できません。
27.2. Instantiating a Proxy by Injection
Overview
Apache CXF's use of the Spring Framework allows you to avoid the hassle of using the JAX-WS APIs to create service proxies. It allows you to define a client endpoint in a configuration file and then inject a proxy directly into the implementation code. When the runtime instantiates the implementation object, it will also instantiate a proxy for the external service based on the configuration. The implementation is handed by reference to the instantiated proxy.
Because the proxy is instantiated using information in the configuration file, the WSDL location does not need to be hard coded. It can be changed at deployment time. You can also specify that the runtime should search the application's classpath for the WSDL.
Procedure
To inject a proxy for an external service into a service provider's implementation do the following:
- Deploy the required WSDL documents in a well known location that all parts of the application can access.NoteIf you are deploying the application as a WAR file, it is recommended that you place all of the WSDL documents and XML Schema documents in the
WEB-INF/wsdl
folder of the WAR.NoteIf you are deploying the application as a JAR file, it is recommended that you place all of the WSDL documents and XML Schema documents in theMETA-INF/wsdl
folder of the JAR. - Configure a JAX-WS client endpoint for the proxy that is being injected.
- Inject the proxy into your service provide using the
@Resource
annotation.
Configuring the proxy
You configure a JAX-WS client endpoint using the
jaxws:client
element in you application's configuration file. This tells the runtime to instantiate a org.apache.cxf.jaxws.JaxWsClientProxy
object with the specified properties. This object is the proxy that will be injected into the service provider.
At a minimum you need to provide values for the following attributes:
id
—Specifies the ID used to identify the client to be injected.serviceClass
—Specifies the SEI of the service on which the proxy makes requests.
Example 27.1, “Configuration for a Proxy to be Injected into a Service Implementation” shows the configuration for a JAX-WS client endpoint.
Example 27.1. Configuration for a Proxy to be Injected into a Service Implementation
<beans ... xmlns:jaxws="http://cxf.apache.org/jaxws" ... schemaLocation="... http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ..."> <jaxws:client id="bookClient" serviceClass="org.apache.cxf.demo.BookService" wsdlLocation="classpath:books.wsdl"/> ... </beans>
Note
In Example 27.1, “Configuration for a Proxy to be Injected into a Service Implementation” the
wsdlLocation
attribute instructs the runtime to load the WSDL from the classpath. If books.wsdl
is on the classpath, the runtime will be able to find it.
For more information on configuring a JAX-WS client see Section 15.2, “Configuring Consumer Endpoints”.
Coding the provider implementation
You inject the configured proxy into a service implementation as a resource using the
@Resource
as shown in Example 27.2, “Injecting a Proxy into a Service Implementation”.
Example 27.2. Injecting a Proxy into a Service Implementation
package demo.hw.server; import org.apache.hello_world_soap_http.Greeter; @javax.jws.WebService(portName = "SoapPort", serviceName = "SOAPService", targetNamespace = "http://apache.org/hello_world_soap_http", endpointInterface = "org.apache.hello_world_soap_http.Greeter") public class StoreImpl implements Store { @Resource(name="bookClient") private BookService proxy; }
The annotation's name property corresponds to the value of the JAX-WS client's
id
attribute. The configured proxy is injected into the BookService
object declared immediately after the annotation. You can use this object to make invocations on the proxy's external service.