0

I have an application that is trying to access a webservice using generated classes via wsdl2java. I would like to be able to configure it so that I can use a different endpoint based on the environment (TEST/PROD).

I found the following answer to be exactly what I was looking for https://stackoverflow.com/a/3569291/346666

However, I would like to use Spring to inject an instance of the service into my service layer - is there a pure Spring approach to the above?

Or, is there a better way to inject an instance of a webservice into a class and still be able to dynamically configure the endpoint?

Community
  • 1
  • 1
Tyler Murry
  • 2,705
  • 3
  • 29
  • 46

1 Answers1

1

Using Spring Java-based configuration:

@Configuration
public class HelloServiceConfig {

    @Bean
    @Scope("prototype")
    public HelloService helloService(@Value("${webservice.endpoint.address}") String endpointAddress) {
        HelloService service = new HelloService();
        Hello port = service.getHelloPort();
        BindingProvider bindingProvider = (BindingProvider) port;
        bindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,endpointAddress);
        return service;
    }

}

@Component
public class BusinessService {

     @Autowired
     private HelloService hellowService;
     ...

     public void setHelloService(HelloService helloService) {
        this.helloService = hellowService;
     }
}

Edit

To use this with Spring XML-based configuration you just need to register the HelloServiceConfig as a bean in your Spring context xml file:

<bean class="com.service.HelloServiceConfig.class"/>

<bean id="businessService" class="com.service.BusinessService">
     <property name="helloService" ref="helloService"/>
</bean>

Other alternatives for creating web service clients in Spring include using Spring Web Services or Apache CXF. Both options allow defining a JAX-WS client based on wsdl2java using only XML but required additional dependencies.

Ricardo Veguilla
  • 3,107
  • 1
  • 18
  • 17
  • I should have clarified. I was searching for a Spring XML-based solution without Autowiring. Do you know of a way to do that? – Tyler Murry Aug 25 '14 at 01:35
  • I ended up using a pure Spring-XML approach using Apache CXF: http://cxf.apache.org/docs/writing-a-service-with-spring.html#WritingaservicewithSpring-CreateaClient(MoreManualWay) - Thanks! – Tyler Murry Aug 25 '14 at 12:58