【问题标题】:Define a custom InjectionResolver for @Inject为 @Inject 定义一个自定义 InjectionResolver
【发布时间】:2019-05-21 19:32:52
【问题描述】:

我的目标是围绕系统注入解析器添加一些额外的逻辑(可以说是装饰它)。基本上,我想注册一个自定义注入解析器(例如描述 here),但要注册 javax.inject.@Inject 注释。如果我创建不同的自定义注释,则设置有效,但在使用 @Inject 时无效。我的实现如下:

注入解析器:

@Singleton
@Rank(Integer.MAX_VALUE)
public class InjectInjectionResolver
    implements InjectionResolver<Inject> {

    private final InjectionResolver<Inject> injectionResolver;

    @Inject
    public InjectInjectionResolver(
            @Named(InjectionResolver.SYSTEM_RESOLVER_NAME) final InjectionResolver<Inject> injectionResolver) {
        this.injectionResolver = injectionResolver;
    }

    @Override
    public Object resolve(final Injectee injectee, final ServiceHandle<?> root) {
        throw new RuntimeException("Why is this never called?");
    }

    @Override
    public boolean isConstructorParameterIndicator() {
        return injectionResolver.isConstructorParameterIndicator();
    }

    @Override
    public boolean isMethodParameterIndicator() {
        return injectionResolver.isMethodParameterIndicator();
    }
}

我按如下方式注册解析器(在ResourceConfig 内):

register(new AbstractBinder() {

        @Override
        protected void configure() {
            bind(InjectInjectionResolver.class).to(new GenericType<InjectionResolver<Inject>>() {}).in(Singleton.class);
        }
    });

到目前为止我尝试了什么:

  • 使用@Rank
  • org.glassfish.jersey.internal.inject.AbstractBinderorg.glassfish.hk2.utilities.binding.AbstractBinderTypeLiteral 一起使用,同时实现org.glassfish.hk2.api.InjectionResolverorg.glassfish.jersey.internal.inject.InjectionResolver
  • 确保相同的设置适用于 @CustomAnnotation(在此同时调用构造函数和解析方法)

有人知道如何解决这个问题吗?

使用的版本:球衣 2.27

编辑:当我@Inject InjectionResolver 本身时,似乎为org.glassfish.hk2.api.InjectionResolver 注入了hk2 的org.jvnet.hk2.internal.ThreeThirtyResolver,而为org.glassfish.jersey.internal.inject.InjectionResolver 注入了我的自定义注入解析器。但是,自定义注入解析器用于注入。

【问题讨论】:

  • 您是否尝试过将 javax 注入的 InjectionResolver 的等级设置为 > 0 ?这将迫使它被用于支持提供的系统
  • @jwells131313 是和否。我尝试了错误的配置,并在我有正确的配置时删除了@Rank...当我使用org.glassfish.hk2.api.InjectionResolver 时确实有效,我想通了其余的部分。非常感谢。
  • 您能否发布您在配置中修复的内容,以便这个问题有答案?谢谢

标签: jersey jersey-2.0 hk2


【解决方案1】:

以下配置对我有用,感谢@jwells131313 让我重回正轨。我尝试使用完全限定名称,否则它可能来自 javax.inject.* 包。

@Singleton
@Rank(Integer.MAX_VALUE)
@org.jvnet.hk2.annotations.Service
public class MyHk2InjectionResolver
    implements org.glassfish.hk2.api.InjectionResolver<Inject> {

    private final org.glassfish.hk2.api.InjectionResolver<Inject> injectionResolver;

    @Inject
    public MyHk2InjectionResolver(
        @Named(org.glassfish.hk2.api.InjectionResolver.SYSTEM_RESOLVER_NAME) final org.glassfish.hk2.api.InjectionResolver<Inject> injectionResolver) {
        this.injectionResolver = injectionResolver;
    }

    // ... required methods, could just delegate to system resolver

}

要注册解析器,我们将其绑定到正确的类型(我在球衣应用程序的 ResourceConfig 中完成):

register(new org.glassfish.hk2.utilities.binding.AbstractBinder() {

        @Override
        protected void configure() {
            bind(MyHk2InjectionResolver.class)
                    .to(new org.glassfish.hk2.api.TypeLiteral<org.glassfish.hk2.api.InjectionResolver<Inject>>() {})
                    .in(Singleton.class);
        }
    });

通过此设置,您可以继续使用 @Inject 注释并围绕它编写一些自定义注入逻辑,可能使用默认的系统注入解析器作为后备。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 2017-02-04
    相关资源
    最近更新 更多