JAX-WS Guice integration

Google Guice - light weight dependecy injection framework.

How to Install

Download the extension (links below) and put it on the classpath of your web services project. You also need jax-ws and guice. In the pom, the dependecies are jax-ws 2.1.3 and guice 1.0, but i've run it against later versions without problems.
Step-by-step:
  • Download the jar (look below)
  • Put it on the classpath
  • Annotate your web service with @GuiceManaged(module = { YourModule.class, ...}) (check below for example)
  • Inject dependecies like crazy

Example

The only thing you need to change in your current web service end points is to annotate them with @GuiceManaged like below. For guice details, please check the guice documentation
@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);
    }
}
The WebServiceModule looks like this:
public class WebServiceModule extends AbstractModule {
    protected void configure() {
        bind(Calculator.class).to(CalculatorImpl.class).in(Singleton.class);
    }
}
This means that Guice will find all @Inject annotations and then inject a singleton instance of CalculatorImpl.class wherever it finds Calculator.class.

Guice scopes

It is possible to have both Singleton and the default "request"-scope. I.e. if you define your Endpoint implementation in Singleton scope, Guice will only create one instance of it, and if you don't define any scope at all, guice will create a new instance of the endpoint class for every web service request. Note that since the endpoint implementation class is not injected anywhere, you need to either annotate the class with @Singleton, or do like this:
bind(AddNumbersImpl.class).in(Singleton.class);
in your module.

Maven

Binaries are deployed to maven, to use it, add this to your pom.xml:

  <dependencies>
    <dependency>
      <groupId>org.jvnet.jax-ws-commons</groupId>
      <artifactId>guicemanaged</artifactId>
      <version>0.5</version>
    </dependency>
  </dependencies>
And also this repository since jax-ws commons stuff are not synced to the central maven repository:
  <repositories>
    <repository>
      <id>maven2-repository.dev.java.net</id>
      <name>Java.net Repository for Maven</name>
      <url>http://download.java.net/maven/2/</url>
    </repository>
  </repositories>

Download

Version ChangesAdded
0.1DownloadInitial rev.2008-11-05
0.2DownloadUpdated - allow several Modules in GuiceManaged annotation using the { Module1.class , ..} syntax. Let module instantiation errors propagate to framework2008-11-28
0.3DownloadMaven support2008-12-14
0.4DownloadSupport for @Resource and getting WebServiceContext using @Inject2008-12-25
0.5DownloadFixes, support singletons cross several web service endpoints, integrate with guiceyfruit for JSR250 compatibility (Patch sent in by Ronak Patel)2009-08-29

Sources

Sources are stored in Subversion. Read more
Terms of Use; Privacy Policy; Copyright ©2013-2014 (revision 20140418.2d69abc)
 
 
Close
loading
Please Confirm
Close