users@jersey.java.net

[Jersey] Re: jersey-guice, subresource, assisted vs. child injector question

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Thu, 30 Dec 2010 13:06:03 +0100

On Dec 20, 2010, at 4:47 PM, Christopher Piggott wrote:

> Hi,
>
> I have a resource that generates a subresource using assisted
> injection and a factory (built with FactoryProvider). It works pretty
> well, but it has me thinking ... Is there perhaps a better way to
> solve the problem?
>
> One of the things the locator resource method does is builds a
> DataSource that is specific to that session (based on username and a
> lookup using a master database somewhere).
>
> public class MainResource {
> @Path("something")
> public SubResource getIt(@InjectParam SomethingFactory provider) {
> DataSource subDataSource = figureOutDatasource(username);
> return provider.create(subDataSource);
> }
> }
>
>
> I gather though that there's a way to do this without assisted
> injection, whereby I can create up a child injector that says "When
> any resources created by this injector say @Inject DataSource ds, you
> should inject subDataSource."
>
> Generally speaking, does that seem better or worse than passing them
> in via assisted injection?
>

If the "username" is not contextual to the sub-resource locator and is
more general you could declare a provider binding that provides the
DataSource (in combination with an annotation qualifier to distinguish
between other data sources). Then you can inject that DataSource
reference into the sub-resource.

Paul.