I recently read a great post by Justin Grant about Dependency Injection in Jersey and I wanted to update it with
a slight improvement I uncovered while working with Jersey 2.23.1.
First off, if you haven’t read Justin’s post and aren’t familiar with HK2 and its use within Jersey, I would encourage you to read it first (link above).
Secondly, while I have had luck getting this approach to work, it may rely on some APIs that are open to change in the future so I cannot comment on
whether this approach is in line with the future direction of Jersey.
My particular improvement applies to this specific note in Justin’s blog:
However, with Jersey, the ServiceLocator has been created and populated already, which means that it gets a little complicated.
It turns out there’s actually an easier way to tie into Jersey’s ServiceLocator than the one described in Justin’s blog. This is done by utilizing a JAX-RS
Feature and Jersey’s ServiceLocatorProvider
This class will cause the HK2 framework to scan your classpath for inhabitants files that are created using the hk2-inhabitant-generator. If you haven’t
set that up as part of your build you can do so by adding the following line to the plugins section of your pom.xml:
To activate the class you will need to register your new feature as you would any other Jersey feature:
From now on Jersey should automatically find all of your @Contract and @Service classes.