javakaffee

Just another weblog about (web) development technologies like spring, tapestry, jersey, jsf, hibernate and more

May 18, 2008

Jersey DI - Use custom annotations for dependency injection in your resource classes

Filed under: development, java, webdev, REST — martin.grotzke @ 5:10 pm

In a previous posting I blogged about integration of spring into jersey (and IoC-containers in general), and showed, how the new Inject annotation can be used to inject dependencies from your custom ComponentProvider.

To support this Inject annotation, a feature was introduced into jersey, that allows you to define your own annotations and inject instances to fields annotated with this annotation (in your resource classes). For retrieving instances for the specific annotation you have to add an Injectable for your annotation to the WebApplication. The Injectable is then asked to resolve the value for some field annotated with the specific annotation.

The easiest thing is probably to give an example…

Register the Injectable for some annotation we now call MyAnnotation. This can be done e.g. in your custom ServletContainer, where you have access to the WebApplication:

import com.sun.jersey.api.core.ResourceConfig;
import com.sun.jersey.spi.container.WebApplication;
import com.sun.jersey.spi.container.servlet.ServletContainer;
import com.sun.jersey.spi.resource.Injectable;

public class MyServletContainer extends ServletContainer {

    @Override
    protected void initiate( ResourceConfig rc, WebApplication wa ) {
        super.initiate( rc, wa );
        
        wa.addInjectable(new Injectable<MyAnnotation, String>() {
            @Override
            public String getInjectableValue(Object o, Field f, MyAnnotation a) {
                return "foo"; // can be retrieved from somewhere
            }

            @Override
            public Class<MyAnnotation> getAnnotationClass() {
                return MyAnnotation.class;
            }
        });
    }
    
    @Target({FIELD, PARAMETER, CONSTRUCTOR })
    @Retention(RUNTIME)
    @Documented
    public static @interface MyAnnotation {

    }

}

In your resource class, you can then annotate fields with MyAnnotation to get the value from the Injectable:

@Path("/")
public class MyResource {
        
    @MyAnnotation String injectedValue;
        
    @GET
    public String get() {
        return injectedValue;
    }                

}

The GET method returns “foo” in this case, but of course you want to retrieve this instance of String from somewhere else.

With this feature you can e.g. create some DAO annotation, annotate the fields in your resource classes referencing DAOs and retrieve instances for these DAOs from your custom ComponentProvider.

How to configure a custom ComponentProvider or your custom ServletContainer see my posting concerning the spring-integration or Paul Sandoz’ blog.

Tags: , , ,

1 Comment »

  1. I tried to create custom annotation by trying your example. But since addInjectable() is not now, I dont know how to proceed further. Could you please post an example or send me a mail, to create custom annotations using jersey-1.9.1. Your help is highly needed.

    Thanks in advance

    Felix Enigo, Assistant Professor, SSN College of Engineering, India

    Comment by Felix Enigo — December 3, 2011 @ 11:23 am

RSS feed for comments on this post. TrackBack URI

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image

Powered by WordPress