users@jersey.java.net

[Jersey] Providers for _at_RequestScoped dependencies (jersey-guice question)

From: Christopher Piggott <cpiggott_at_gmail.com>
Date: Tue, 12 Jun 2012 11:59:26 -0400

Hi,

Can you have bind any object to scope RequestScoped ?

I want to inject a JdbcTemplate that is based on a property of
MagicUserPrincpal (implements Princpal).

What I kind of want to do is to set up a provider so that my
subresource can be created like this:

@Inject public MySubResource( @LoginSpecificTemplate JdbcTemplate t) {
    this.t = t;
}


For that to work, though, I would want to:

   bind( JdbcTemplate.class )
      .annotatedWith( LoginSpecificTemplate.class )
      .toProvider ( MyTemplateProvider.class )
       .in( Scopes.RequestScoped );

then make a provider MyTemplateProvider that is RequestScoped and gets
injected into him the MagicUserPrincipal. Here lies the problem:
providers have to be bound as singletons ... so I can't do this the
way I'm thinking.

Currently I manage all of this through root resources and assisted
injection. It works OK, I was just hoping to come up with something
prettier.

--Chris