If you're using Jersey 2, it uses HK2 as it's DI framework. All resource classes go through the DI lifecycle when they are constructed. And constructor injection is not a problem.
The most basic way (with Jersey) to make an arbitrary object injectable, is to bind in an AbstractBinder
public class Binder extends AbstractBinder {
@Override
protected void configure() {
bind(EventSchedudlerDaoImpl.class).to(EventSchedulerDao.class);
}
}
Then register the binder with Jersey
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(new Binder());
}
}
Then you just need to declare the injection point by adding @Inject
on top of the constructor.
@Inject
public EventSchedulerService(EventSchedulerDao dao) {
this.dao = dao;
}
As far as the binder implementation, the binding syntax basically reads as
bind( Implementation ).to( Contract ).in( Scope );
The bind
method can take an instance or it can take a class. When you provide an instance, the Scope is automatically Singleton.
The to
method specifies the advertised contract, which is the type that can be declared at the injection point. In this case, only the interface EventSchedulerDao
can be used for the injection point. If you don't have an interface you can just do
bindAsContract(EventSchedulerDao.class)
assuming EventSchedulerDao
is the implementation class.
The scopes available are PerLookup
, RequestScoped
and Singleton
. If not specified, the default scope will be PerLookup
, meaning a new instance of the service will be created for each injection point. You should already know what Singleton
means. RequestScoped
means that a new instance will be created for each request, which may not be the same as PerLookup
, as the service may be injected at multiple points through out the request lifeclyle.
See Also: