users@jersey.java.net

[Jersey] Re: spring-like injection with Jersey

From: Patrick Lawler <patrick.lawler_at_englishcentral.com>
Date: Wed, 9 Sep 2015 12:32:38 -0400

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 <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.
>