泽西2两个自定义注入注释 [英] Two custom injection annotations in Jersey 2

查看:177
本文介绍了泽西2两个自定义注入注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该怎么办了ValueFactoryProvider才能有两个自定义注入注释泽西2共存的结合?下面我已经列入我目前的做法的一个例子,正如你所看到的Hello注解注入隐藏Smalltalk的注解注入。

How should I do the ValueFactoryProvider binding in order to have two custom injection annotations coexist in Jersey 2? Below I have included an example of my current approach and as you can see the Hello annotation injection "hides" the SmallTalk annotation injection.

您好注释:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface Hello {
}

Smalltalk的注释:

SmallTalk annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface SmallTalk {
}

您好注释解析:

@Singleton
public class HelloResolver {
    public static class HelloInjectionResolver extends ParamInjectionResolver<Hello> {
        public HelloInjectionResolver() {
            super(HelloValueFactoryProvider.class);
        }
    }

    @Singleton
    public static class HelloValueFactoryProvider extends AbstractValueFactoryProvider {
        @Inject
        public HelloValueFactoryProvider(final MultivaluedParameterExtractorProvider extractorProvider,
                                         final ServiceLocator injector) {
            super(extractorProvider, injector, UNKNOWN);
        }

        @Override
        protected Factory<?> createValueFactory(final Parameter parameter) {
            final Class<?> classType = parameter.getRawType();

            if (classType == null || (!classType.equals(String.class))) return null;

            return new AbstractContainerRequestValueFactory<String>() {
                @Override
                public String provide() {
                    return "Hello!";
                }
            };
        }
    }

    public static class Binder extends AbstractBinder {
        @Override
        protected void configure() {
            bind(HelloValueFactoryProvider.class).to(ValueFactoryProvider.class).in(Singleton.class);
            bind(HelloInjectionResolver.class).to(
                    new TypeLiteral<InjectionResolver<Hello>>() {
                    }
            ).in(Singleton.class);
        }
    }
}

Smalltalk的注解解析:

SmallTalk annotation resolver:

@Singleton
public class SmallTalkResolver {
    public static class SmallTalkInjectionResolver extends ParamInjectionResolver<SmallTalk> {
        public SmallTalkInjectionResolver() {
            super(SmallTalkValueFactoryProvider.class);
        }
    }

    @Singleton
    public static class SmallTalkValueFactoryProvider extends AbstractValueFactoryProvider {
        @Inject
        public SmallTalkValueFactoryProvider(final MultivaluedParameterExtractorProvider extractorProvider,
                                             final ServiceLocator injector) {
            super(extractorProvider, injector, UNKNOWN);
        }

        @Override
        protected Factory<?> createValueFactory(final Parameter parameter) {
            final Class<?> classType = parameter.getRawType();

            if (classType == null || (!classType.equals(String.class))) return null;

            return new AbstractContainerRequestValueFactory<String>() {
                @Override
                public String provide() {
                    return "Nice weather.";
                }
            };
        }
    }

    public static class Binder extends AbstractBinder {
        @Override
        protected void configure() {
            bind(SmallTalkValueFactoryProvider.class).to(ValueFactoryProvider.class).in(Singleton.class);
            bind(SmallTalkInjectionResolver.class).to(
                    new TypeLiteral<InjectionResolver<SmallTalk>>() {
                    }
            ).in(Singleton.class);
        }
    }
}

资源配置:

public class MyApplication extends ResourceConfig {
    public MyApplication() {
        register(new HelloResolver.Binder());
        register(new SmallTalkResolver.Binder());
        registerClasses(HelloResource.class);
    }
}

资源同时使用注入注释:

Resource using both injection annotations:

@Path("/")
public class HelloResource {
    @GET
    @Path("hello")
    @Produces("application/json")
    public String hello(@Hello final String hello, @SmallTalk final String smallTalk) {
        return hello + " " + smallTalk;
    }
}

请求资源时结果 - 本来应该是你好天气不错!

推荐答案

找到了解决办法!我加了

Found a solution! I added

if (parameter.getAnnotation(Hello.class) == null) return null;

if (parameter.getAnnotation(SmallTalk.class) == null) return null;

createValueFactory 这两个值工厂供应商的方法。

to the createValueFactory method of the two value factory providers.

这篇关于泽西2两个自定义注入注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆