users@jersey.java.net

Re: SV: [Jersey] Simplest Dependency Injection with Jersey

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 06 Aug 2009 10:25:16 +0200

Hi Morten,

I think Jaroslav's requirements are a little simpler that yours
because you wanted to integrate in your own IoC framework where as
Jaroslav wants to add some more stuff to be injected.

Jersey will not be providing support for the EE-related annotations
unless it is utilized within an EE 6 compliant container.

However, you can add your own stuff to inject by implementing an
InjectableProvider. For example, the following is an
InjectableProvider implementing support for the @Resource annotation:

    @Provider
    public static class ResourceInjectable implements
InjectableProvider<Resource, Type> {

        public ComponentScope getScope() {
            return ComponentScope.Singleton;
        }

        public Injectable<Object> getInjectable(ComponentContext ic,
Resource r, Type c) {
            final Object value = get(r);

            return new Injectable<Object>() {
                public Object getValue() {
                    return value;
                }
            };
        }

        private Object get(Resource r) {
            try {
                Context ctx = new InitialContext();

                // Look up a data source
                try {
                    return ctx.lookup(r.name());
                } catch (NamingException ex) {
                    return ctx.lookup("java:comp/env/" + r.name());
                }
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    }

Developers have used the above and one similar to support @EJB.

https://jersey.dev.java.net/nonav/apidocs/1.1.1-ea/jersey/com/sun/jersey/spi/inject/InjectableProvider.html

If you want to inject singletons, you can use the following helper
class:

https://jersey.dev.java.net/nonav/apidocs/1.1.1-ea/jersey/com/sun/jersey/spi/inject/SingletonTypeInjectableProvider.html


You register InjectableProvider implementations in the way way as you
register root resource classes.


Jaroslav, thanks for the offer to contribute, the best way to
contribute is to create an issue and add an attachment.

Hope this helps,
Paul.

On Aug 5, 2009, at 7:34 PM, Morten wrote:

> --- Den ons 5/8/09 skrev Jaroslav Tulach <Jaroslav.Tulach_at_Sun.COM>:
>> I am playing with Jersey and use it without any Web or
>> JavaEE container.
>> Still, I'd like to use a little bit of dependency
>> injection. Can anyone
>> advice me what is the simplest way to do that?
>>
>> Is my understanding correct that Jersey can handle the
>> injection itself
>> without any additional injection framework? I tried to
>> annotate a field in
>> my @Path("/") @Singleton class with @Resource annotation,
>> but nothing
>> happened. No errors. No warnings. No injection.
>>
>> Please point me in the right direction and I promise to
>> provide a
>> jersey/samples/dependency-injection sample into your
>> repository when the
>> injection starts working.
>>
>> Thanks in advance.
>> -jst
>>
>> PS: I found a tutorials to use Guice and Spring, but as I
>> mentioned, I am
>> looking for as simple solution as possible - e.g. without
>> any framework,
>> without changes to web.xml, etc.
>
> I had the some requirement. Got it to work but it took a lot of time
> to figure out as the documentation is sparse (luckly Sun's support
> on this email group is very good so it compensates). In brief:
>
> 1) You have to create your own little IVC class that implements
> IoCComponentProviderFactory and returns the objects for types that
> you want to inject and null in all other cases. Internally the
> factory should use an implementation of IoCManagedComponentProvider.
>
> 2) Annotate the fields/ctr parameters that you want injected with
> com.sun.jersey.spi.inject.Inject (but use javax.ws.rs.core.Context
> for build-in jersey stuff).
>
> Cheers,
> Morten
>
>
>
>
>
> ___________________________________________________________
> Skal du købe ny bil? Sammenlign priser på brugte biler med Kelkoo og
> find et godt tilbud! - Se mere her http://dk.yahoo.com/r/pat/mmb
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>