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