users@jersey.java.net

Re: [Jersey] auto injection for sub-resources

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 25 Jan 2010 10:09:36 +0100

And for release 1.1.5 you can use Jersey's @Inject to do the same thing.

e.g.

   @Inject MyResource mr;

Paul.

On Jan 22, 2010, at 3:56 PM, FSauer_at_dsthealthsolutions.com wrote:

>
> Yes, you have to create the sub resource using a ResourceContext , I
> use this:
>
> @Context private ResourceContext rs;
>
> /**
> * Always use this method in sub resource locators to
> instantiate the sub resource.
> * Propagates injected data from root resource to sub
> resource. Care must be taken that
> * the sub resource has the proper scoping depending on its
> use. If you need to create
> * collections of these objects make sure it's @Scope is
> "prototype"! If the scope is
> * "request", only one instance will be created per
> request.... Using this method also
> * allows the use of the @Component and @Resource
> annotations in the sub resource to inject
> * spring beans defining services.
> *
> * @param <T> type of the sub resource to create
> * @param clz class of the sub resource to create
> * @return instance of the sub resource.
> */
> public <T extends ApplicationResource> T
> createSubResource(Class<T> clz) {
> try {
> return rs.getResource(clz);
> } catch (Exception e) {
> e.printStackTrace();
> throw new
> WebApplicationException(Response.serverError().entity("Could not
> create sub resource of type " + clz.getName()).build());
> }
> }
> <mime-attachment.gif> Frank Sauer
> Principal Architect
> DST Health Solutions
> 2500 Corporate Drive
> Birmingham, AL 35242
> (205)437-5204 (Office) (Direct)
> (205)568-4684 (Cell)
> fsauer_at_dsthealthsolutions.com
> It is impossible to sharpen a pencil with a blunt axe. It is equally
> vain to try to do it with ten blunt axes instead -- Edsger W. Dijkstra
>
>
>
>
> Cheng Zhang <zhangyongjiang_at_yahoo.com>
> 01/22/2010 03:19 AM
> Please respond to
> users_at_jersey.dev.java.net
>
> To
> users_at_jersey.dev.java.net
> cc
> Subject
> [Jersey] auto injection for sub-resources
>
>
>
>
>
> It seems that there is no autowire for the @context defined in the
> sub-resources.
>
> I have a sub-resource which has two @Context fields
>
> protected @Context HttpContext hc;
> protected @Context ServletContext servletContext;
>
> When debugging, I can see both hc and servletContext are null.
>
> Did I miss anything here?
>
> Thanks a lot,
> Kevin
>
> ----------------------------------------- Please consider the
> environment before printing this email and any attachments. This e-
> mail and any attachments are intended only for the individual or
> company to which it is addressed and may contain information which
> is privileged, confidential and prohibited from disclosure or
> unauthorized use under applicable law. If you are not the intended
> recipient of this e-mail, you are hereby notified that any use,
> dissemination, or copying of this e-mail or the information
> contained in this e-mail is strictly prohibited by the sender. If
> you have received this transmission in error, please return the
> material received to the sender and delete all copies from your
> system.