Thursday, November 6, 2008

Google Guice and JAX-WS RI revisited

I've been coding a bit more with the guice-extension for jax-ws, and the result is actually quite nice, see below for how your web service endpoint would look when it is handled by guice:
@GuiceManaged(module = WebServiceModule.class)
@WebService
public class AddNumbersImpl implements AddNumbers {

    private Calculator calculator;

    @Inject
    public void setCalculator(Calculator calc)
    {
        this.calculator=calc;
    }
    @WebMethod
    public int addNumbers(int num1, int num2)
    {
        return this.calculator.calc(num1,num2);
    }
}
And WebServiceModule looks like this:
public class WebServiceModule extends AbstractModule {
    protected void configure() {
        bind(Calculator.class).to(CalculatorImpl.class).in(Singleton.class);
    }
}

It can handle endpoints in any scope you want, if you want just one instance of the endpoint, you either annotate the class with @Singleton or bind it in Singleton scope in your module.

Head over to the jax-ws commons project on dev.java.net to get the code, binaries and some information on how to use it.


1 comment:

Daddy Parks said...

Followed your instructions but with no luck. Using guicemanaged-0.5.jar and my injected dependency (equivalent to your 'calculator' above) is null.

Using guice-2.0 and jboss 5.

What method did you use to start up guice? Did you create a sar? ...