The*_*One 20 java dependency-injection jax-rs jersey
我想创建一个类,其对象可以使用@Context注释(或者更好的是自定义注释,用于需要将参数传递给注释的情况)注入资源方法.在泽西岛1.*我会用InjectableProvider(在我的情况下AbstractHttpContextInjectable).我想要实现的是类似@Auth[ 1 ]的dropwizard(使用Jersey 1.7).
据我所知,泽西的注射能力被HK2取代,我找不到任何我所描述的例子.
编辑:请参阅此问题,了解我在尝试按照Michal的指南时遇到的其他问题.
Mic*_*dos 21
您需要从HK2 实现InjectionResolver <T>接口.看一下Jersey工作区中现有的实现:
@Context@PathParam,, @QueryParam......(通过它的子类)@Autowired一旦你有了这个,你需要从HK2 扩展AbstractBinder并绑定你的InjectionResolver通过它的#configure()方法:
public class MyResolverBinder extends AbstractBinder {
@Override
protected void configure() {
bind(MyInjectionResolver.class)
.to(new TypeLiteral<InjectionResolver<MyAnnotation>>() {})
.in(Singleton.class);
}
}
Run Code Online (Sandbox Code Playgroud)
...并在您的应用程序类中(或通过功能)注册此binder的实例:
Feature:
public class MyFeature implements Feature {
@Override
public boolean configure(final FeatureContext context) {
context.register(new MyResolverBinder());
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
注册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;
}
}
Run Code Online (Sandbox Code Playgroud)
注册MyResolverBinder或Feature在ResourceConfig
new ResourceConfig()
// Register either MyFeature
.register(MyFeature.class)
// or MyResolverBinder
.register(new MyResolverBinder())
// Register other providers or resources
.packages("my.package");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10536 次 |
| 最近记录: |