2015-06-22 24 views
2

我有一個使用澤西DI注入的依賴類JerseyWebService是否有可能使用Spring DI部分構建一個對象並部分使用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; 

是否有可能只指定一些依賴關係澤西將注入Spring和其他依賴關係?

如果不是,那麼如果已經@Inject而不是@AutowiredExternalApiService是否必須指定binder類中的所有綁定?

如果Jersey沒有找到任何綁定,Jersey沒有自動裝配類似功能或委託注入Spring的依賴項嗎?

+0

如果您使用的是最新版本的Jersey 2,只需將jersey-spring3庫添加到您的應用程序中,就可以讓Jersey依賴注入Spring bean而無需自定義綁定。 –

回答

1

It should work。鑑於你有所需的彈簧,新澤西集成依賴[],並且正確配置的應用程序[]

1. See Spring DI support in Jersey
2. See official Jersey Spring example

HK2(Jersey的DI框架)會爲@Autowired尋找InjectionResolver註釋,以解決依賴關係。 jersey-spring3依賴關係具有AutowiredInjectionResolver,其中包含對Spring的ApplicationContext的引用。從那裏開始,只需在應用程序上下文中查找它即可解決依賴關係。

相關問題