users@jersey.java.net

[Jersey] Re: spring-like injection with Jersey

From: Boettcher,Tom <Tom.Boettcher_at_Cerner.com>
Date: Wed, 9 Sep 2015 20:34:15 +0000

Instances you construct can be injected into other classes, but those instances will not have their own @Inject points injected. You can use the ServiceLocator's inject(...) method to inject into such instances manually, but I'm not aware of a way to do so when using the DependencyBinder approach.

I would love to be wrong, though, as this is something I have wanted before.

--Tom Boettcher

On 09/09/2015 03:25 PM, Paul O'Fallon wrote:
Hello! Not sure if this is what you're asking about, but I have an example of binding an instance of a class. This instance is then injected via a @Context annotation.

Instance binding happens here:
https://github.com/pofallon/jersey2-akka-java/blob/master/src/main/java/com/ofallonfamily/jersey2akka/ExampleApplication.java<https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_pofallon_jersey2-2Dakka-2Djava_blob_master_src_main_java_com_ofallonfamily_jersey2akka_ExampleApplication.java&d=BQMFaQ&c=NRtzTzKNaCCmhN_9N2YJR-XrNU1huIgYP99yDsEzaJo&r=9A14nJoP0MJM1-5Sr_v-32319N_9OuTZ9NAMrtQwyw0&m=Z3-CpGh4Nf_KDcTp2a9UeczUL1r_EzZIXzjaURTLl0M&s=Fz1p0smFeSa2JNd7kFkhhMuhuJp6LpRcLCpw3dEWz8k&e=>

Injected into resource here:
https://github.com/pofallon/jersey2-akka-java/blob/master/src/main/java/com/ofallonfamily/jersey2akka/ExampleService.java<https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_pofallon_jersey2-2Dakka-2Djava_blob_master_src_main_java_com_ofallonfamily_jersey2akka_ExampleService.java&d=BQMFaQ&c=NRtzTzKNaCCmhN_9N2YJR-XrNU1huIgYP99yDsEzaJo&r=9A14nJoP0MJM1-5Sr_v-32319N_9OuTZ9NAMrtQwyw0&m=Z3-CpGh4Nf_KDcTp2a9UeczUL1r_EzZIXzjaURTLl0M&s=g5si6mp1mG9eRiII2wsiJoqERpbKVOjDpx7IfqjOndI&e=>

HTH,
Paul

On Wed, Sep 9, 2015 at 12:32 PM, Patrick Lawler <patrick.lawler_at_englishcentral.com<mailto:patrick.lawler_at_englishcentral.com>> wrote:
Awesome, made the change suggested by Tom and the layered injection appears to work now. Thanks!

On Wed, Sep 9, 2015 at 11:22 AM, Boettcher,Tom <<mailto:Tom.Boettcher_at_cerner.com>Tom.Boettcher_at_cerner.com<mailto:Tom.Boettcher_at_cerner.com>> wrote:
HK2 will not perform injection on beans that you have already constructed for yourself, it simply injects them as-is. If you want it to perform injection, you will need to bind the class, rather than an instance:

public class MyBinder extends AbstractBinder {
    @Override
    protected void configure() {
       bind(MyDao.class).to(MyDao.class).in(Singleton.class);
       bind(MyService.class).to(MyService.class).in(Singleton.class);
    }
}

You can also use bindAsContract(...) to shorten the common case of binding a class to itself.


On 09/09/2015 09:55 AM, Patrick Lawler wrote:
Hello,

Is it possible to do spring-like injection in Jersey without using the jersey-spring integration?

Our project uses Jersey for RESTful web services. We want to move to a more layered architecture and use injection to inject singletons into other singletons that may be injected into yet other singletons. I have perused and experimented with @Inject and gone over what I hope was relevant parts of the Jersey and HK2 documentation and there did not seem to be an easy way to do this. We will be doing a lot of injecting of many different classes so did not want to create instances, nor do we want to create Providers for each class that we want to inject. I did do a test with the spring extension which works as expected though there are concerns about how heavy it may be. Is there a way to get the HK2 injection working in a similar manner?

We are using Jersey 2.19

simple example trying to use @Inject

@Path("/1/2/3")
public class MyRest {
   @Inject
   private MyService myService;
}

public class MyService {
   @Inject
   private MyDao myDao;
}

public class JerseyApplication extends ResourceConfig {

   @Inject
   public JerseyApplication(ServiceLocator serviceLocator) {
        packages("com.my.app.api");
        register(new MyBinder());
   }
}

public class MyBinder extends AbstractBinder {
    @Override
    protected void configure() {
       bind(new MyDao()).to(MyDao.class);
       bind(new MyService()).to(MyService.class);
    }
}


MyService gets successfully injected into the MyRest class, but the MyDao does not get injected into the MyService class. I started down the path of creating a provider, but that scenario just will not work for us given the number of classes we would need to do this for.


The spring integration I tested with is:

    <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-spring3</artifactId>
            <version>2.19</version>
        </dependency>

and this worked just fine using @Service, @Repository and @Autowired

Any suggestions or pointers to relevant documentation would be appreciated.

Thanks

CONFIDENTIALITY NOTICE This message and any included attachments are from Cerner Corporation and are intended only for the addressee. The information contained in this message is confidential and may constitute inside or non-public information under international, federal, or state securities laws. Unauthorized forwarding, printing, copying, distribution, or use of such information is strictly prohibited and may be unlawful. If you are not the addressee, please promptly delete this message and notify the sender of the delivery error by e-mail or you may call Cerner's corporate offices in Kansas City, Missouri, U.S.A at (+1) (816)221-1024<tel:%28%2B1%29%20%28816%29221-1024>.