Skip to content

Instantly share code, notes, and snippets.

@jliuhtonen
Created January 9, 2015 13:34
Show Gist options
  • Save jliuhtonen/58f0288a6f8712a4905b to your computer and use it in GitHub Desktop.
Save jliuhtonen/58f0288a6f8712a4905b to your computer and use it in GitHub Desktop.
Add classes to Jersey 2 HK2 injector with reflection
// Example of registering javax.injection annotated classes to Jersey 2's HK2 instance ServiceLocator
// Uses https://code.google.com/p/reflections/
public class RestApplication extends ResourceConfig {
@Inject
public RestApplication(ServiceLocator serviceLocator) {
packages("rest.resource");
ServiceLocatorUtilities.addClasses(serviceLocator, findServiceSingletons());
}
private Class[] findServiceSingletons() {
Reflections reflections = new Reflections("rest.service");
Set<Class<?>> clazzez = reflections.getTypesAnnotatedWith(Singleton.class); //or other qualifiers
return clazzez.toArray(new Class[clazzez.size()]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment