HK2泽西岛。用env选项创建@Service

时间:2018-04-28 10:57:20

标签: jersey jersey-2.0 hk2

如我所见,我可以使用自动搜索注释@Service来创建singleton以通过@Inject使用它。像:

@Service
class MyService {
    //.....
}

@Service
class MyOtherService {
    @Inject MyService myService;
    //.....
}

但是想使用依赖于环境的选项来创建服务。 我可以使用AbstractBinder来做到这一点,如:

        final ResourceConfig resourceConfig = new ResourceConfig()
            .register(new AbstractBinder() {
                @Override
                protected void configure() {
                    String someOption = "optionOne";
                    String anotherOption = "optionTwo";
                    MyService myService = new MyService.create(someOption, anotherOption);
                    bind(MyService).to(MyService.class).in(Singleton.class);
                }
            })

但是我怎样才能做同样但使用注释autoconfig风格?无需创建AbstractBinder对象。

0 个答案:

没有答案
相关问题