Dependency Injection - pford68/jersey-city GitHub Wiki

Steps

  1. Create a subclass of AbstractBinder and override its configure() method.

  2. In configure(), you'll map impls to interfaces as shown below:

    @Override
    public void configure() {
       bind(TodoDaoImpl.class).to(TodoDao.class);
    }
    
  3. Then in another class you can inject the TodoDaoImpl into a TodoDao reference:

     @Path("/todo")
     public class TodoResource {
    
        @Inject
        TodoDao dao;
    
        @GET
        @Produces({ MediaType.APPLICATION_JSON })
        public Todo test() {
           return dao.getOne();
        }
    
    }
    

    Use JSR-330 annotations (e.g., javax.inject.Inject) for the injection.

@Context

When deploying a JAX-RS application using servlet then ServletConfig, ServletContext, HttpServletRequest and HttpServletResponse are available using @Context.

References