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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I should have clarified. I was searching for a Spring XML-based solution without Autowiring. Do you know of a way to do that?
I ended up using a pure Spring-XML approach using Apache CXF: cxf.apache.org/docs/… - Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.