Tuesday, November 4, 2008

Google Guice and jax-ws

To integrate google guice with jax-ws, you need to use the InstanceResolver extension point. this way your endpoints will look like this;

@WebService(targetNamespace = "http://marcus", name="AddNumbers")
public class AddNumbersImpl {
  public void setCalculator(Calculator calc)

To achieve this you need to create the annotation GuiceManaged;
@WebServiceFeatureAnnotation(id=GuiceManagedFeature.ID, bean=GuiceManagedFeature.class)
public @interface GuiceManaged {

Then you need to implement the GuiceManagedFeature:
public class GuiceManagedFeature extends WebServiceFeature {
    public static final String ID="some_nice_id";

    public GuiceManagedFeature()
    public String getID() {
        return ID;
And finally the actual InstanceResolver:
public class GuiceManagedInstanceResolver extends AbstractMultiInstanceResolver {
    private T instance=null;
    public GuiceManagedInstanceResolver(@NotNull Class clazz)
    public T resolve(@NotNull Packet packet) {
            Injector injector= Guice.createInjector(new WebServiceModule());
        return instance;

That should do it, I'll update this post soon with a URL to the actual code (with documentation on how to use it. My plan is to make it more extensible (i.e. no hard dependency on the WebServiceModule) and possible to use in a non-singleton scope soon.

1 comment:

Anonymous said...

Your instance resolver code doesn't compile. Can you fix this? Thanks