是否可以使用Spring DI部分构建对象,部分使用Jersey DI构建对象?

时间:2015-06-22 18:23:59

标签: spring dependency-injection jersey jersey-2.0 hk2

我有一个JerseyWebService类,它使用Jersey DI来注入依赖

@Path("/baskets")
public class JerseyWebService {
    @Inject
    ExternalApiServiceInterface api;
    ...
}

依赖关系在活页夹中指定

public class CustomBinder extends AbstractBinder {
    @Override
    protected void configure() {   
       bind(ExternalApiService.class).to(ExternalApiServiceInterface.class);
       ...
    }

但问题是ExternalApiService有其他依赖关系,它使用Spring注入它们。

class ExternalApiService implements ExternalApiServiceInterface{
    @Autowired
    AnotherService aservice;

是否可以在绑定器中仅指定一些依赖项,其中Jersey将注入和其他依赖项由Spring注入?

如果没有,那么如果在@Inject@Autowired代替ExternalApiService,是否必须在binder类中指定所有绑定?

如果找不到任何绑定,Jersey DI是否没有自动装配功能或委托向Spring注入依赖关系?

1 个答案:

答案 0 :(得分:1)

It should work。鉴于您具有所需的Spring-Jersey集成依赖性 [ 1 ] 并且已正确配置应用程序 [ 2 ]

<子> 1。 See Spring DI support in Jersey
<子> 2。 See official Jersey Spring example

HK2(泽西岛的DI框架)会为@Autowired注释寻找InjectionResolver,以解决依赖关系。 jersey-spring3依赖项具有AutowiredInjectionResolver,其中包含对Spring ApplicationContext的引用。从那里开始,只需在应用程序上下文中查找它,即可解决依赖问题。

相关问题