Java 泽西岛 2.*。如何替换 Jersey 1.* 的 InjectableProvider 和 AbstractHttpContextInjectable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18463311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jersey 2.*. How to replace InjectableProvider and AbstractHttpContextInjectable of Jersey 1.*
提问by TheCuriousOne
I would like to create a class whose objects can be injected using the @Context
annotation (or better yet a custom annotation for cases where I need to pass an argument to the annotation) into resource methods. In Jersey 1.* I would have used InjectableProvider
(in my case together with AbstractHttpContextInjectable
). What I'm trying to achieve is something like @Auth
[1] from dropwizard(which uses Jersey 1.7).
我想创建一个类,其对象可以使用@Context
注释(或者更好的自定义注释,用于我需要将参数传递给注释的情况)注入到资源方法中。在 Jersey 1.* 我会使用InjectableProvider
(在我的情况下与AbstractHttpContextInjectable
)。我想要实现的是来自dropwizard(使用 Jersey 1.7)的@Auth
[ 1] 之类的东西。
The injection capabilities of Jersey were replaced by HK2 as far as I know and I could not find any example of what I'm describing.
据我所知,Jersey 的注入功能已被 HK2 取代,我找不到我所描述的任何示例。
Edit: See this questionfor further problems I have encountered while trying to follow Michal's guide.
编辑:有关我在尝试遵循 Michal 指南时遇到的其他问题,请参阅此问题。
采纳答案by Michal Gajdos
You need to implement InjectionResolver<T>interface from HK2. Take a look at existing implementations that are present in Jersey workspace:
您需要从 HK2实现InjectionResolver<T>接口。查看 Jersey 工作区中的现有实现:
- ContextInjectionResolverhandling
@Context
- ParamInjectionResolverhandling
@PathParam
,@QueryParam
, ... (via it's subclasses) - AutowiredInjectResolverhandling
@Autowired
- ContextInjectionResolver处理
@Context
- ParamInjectionResolver处理
@PathParam
,@QueryParam
, ... (通过它的子类) - AutowiredInjectResolver处理
@Autowired
Once you have this, you need to extend AbstractBinderfrom HK2 and bind your InjectionResolver
via it's #configure()
method:
一旦你有了这个,你需要从 HK2扩展AbstractBinder并InjectionResolver
通过它的#configure()
方法绑定你:
public class MyResolverBinder extends AbstractBinder {
@Override
protected void configure() {
bind(MyInjectionResolver.class)
.to(new TypeLiteral<InjectionResolver<MyAnnotation>>() {})
.in(Singleton.class);
}
}
... and register an instance of this binder in your application class (or via feature):
...并在您的应用程序类(或通过feature)注册此活页夹的实例:
Feature
:
Feature
:
public class MyFeature implements Feature {
@Override
public boolean configure(final FeatureContext context) {
context.register(new MyResolverBinder());
return true;
}
}
register MyFeature
into Application
:
注册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;
}
}
register MyResolverBinder
or Feature
in the ResourceConfig
注册MyResolverBinder
或Feature
在ResourceConfig
new ResourceConfig()
// Register either MyFeature
.register(MyFeature.class)
// or MyResolverBinder
.register(new MyResolverBinder())
// Register other providers or resources
.packages("my.package");
回答by user1012976
Providing an implementation of InjectionResolver
only helps with injection, not when resolving values for the parameters of a resource method.
提供 的实现InjectionResolver
仅有助于注入,而不是解析资源方法参数的值。
At least with Jersey 2.11, you need to define a ValueFactoryProvider
annotated with @Provider
.
至少在 Jersey 2.11 中,您需要定义一个ValueFactoryProvider
带@Provider
.
@Provider
public class MyValueFactoryProvider implements ValueFactoryProvider {
@Inject
private MyFactory factory;
@Override
public Factory<?> getValueFactory(Parameter parameter) {
if (parameter.getAnnotation(MyAnnotationParam.class) != null) {
return factory;
}
return null;
}
@Override
public PriorityType getPriority() {
return Priority.NORMAL;
}
}
If you also want to get the value injected in, e.g., members and constructor parameters, then InjectionResolver works well.
如果您还想获得注入的值,例如,成员和构造函数参数,那么 InjectionResolver 工作得很好。