您需要从 HK2实现InjectionResolver<T>接口。查看 Jersey 工作区中现有的实现:
一旦你有了这个,你需要从 HK2 扩展AbstractBinderInjectionResolver
并通过它的#configure()
方法绑定你:
public class MyResolverBinder extends AbstractBinder {
@Override
protected void configure() {
bind(MyInjectionResolver.class)
.to(new TypeLiteral<InjectionResolver<MyAnnotation>>() {})
.in(Singleton.class);
}
}
...并在您的应用程序类(或通过feature )中注册此活页夹的实例:
Feature
:
public class MyFeature implements Feature {
@Override
public boolean configure(final FeatureContext context) {
context.register(new MyResolverBinder());
return true;
}
}
注册:MyFeature
_Application
public class JaxRsApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
final HashSet<Class<?>> classes = new HashSet<Class<?>>();
classes.add(MyFeature.class);
// Register other providers or resources.
return classes;
}
}
注册MyResolverBinder
或Feature
在ResourceConfig
new ResourceConfig()
// Register either MyFeature
.register(MyFeature.class)
// or MyResolverBinder
.register(new MyResolverBinder())
// Register other providers or resources
.packages("my.package");