users@jersey.java.net

Re: [Jersey] auto injection for sub-resources

From: <FSauer_at_dsthealthsolutions.com>
Date: Fri, 22 Jan 2010 08:56:26 -0600

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());
                }
        }


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.





picture
(image/gif attachment: 01-part)