dagger2 @Scope 질문
조회수 1882회
지금 dagger2(를 포함한 rxandroid등등)을 적용하기 위해서 살펴보고 있는데
@Scope 어노테이션이 좀 이해가 안되네요.
@Scope을 왜 사용해야하고, 사용한 것과 사용하지 않는 것의 차이점이 뭔가요?
(제가 보고 있는 예제를 보면 ActivityModule에서 providerActivity()에는 @Provides와 함께 @PerActivity가 설정이 되어있는데 BiteModule의 proideBike()에는 @Provides만 설정이 되어 있습니다. 왜 providerActivity()에만 @PerActivity(@Scope이 설정된 어노테이션)라는것이 설정 되어 있는지를 모르겠습니다.)
그리고 @Scope이 아니라 @Qualifier 라는것도 있는데 어떤 차이점이 있는건지...
찾아보면 @Scope에 대한 개념적인 설명들이 있긴한데 이걸 실제로 사용할 수 있을 정도로의
이해가 되지는 않네요.
혹시 아시는분 계시면 설명 좀 부탁드립니다...
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
provideBike에도 @PerActivity를 설정하게되면 액티비에서 같은 인스턴스를 리턴하기때문에 Bike를 여러개 사용하지 못하게 됩니다.
그리고, 다음과 같이 Qualifier는 같은 타입의 다른 객체 리턴이 필요할 때 사용합니다.
@Provides @Singleton @GithubRestAdapter //Qualifier RestAdapter provideRestAdapter() { return new RestAdapter.Builder() .setEndpoint("https://api.github.com") .build(); } @Provides @Singleton @FacebookRestAdapter //Qualifier RestAdapter provideRestAdapter() { return new RestAdapter.Builder() .setEndpoint("https://api.facebook.com") .build(); }
이렇게 인젝션을 하시면 되는데 태그를 만든다고 생각하시면 될 듯 합니다.
@Inject @GithubRestAdapter RestAdapter githubRestAdapter; @Inject @FacebookRestAdapter RestAdapter facebookRestAdapter;
참조: http://frogermcs.github.io/dependency-injection-with-dagger-2-the-api
참조: https://github.com/codepath/android_guides/wiki/Dependency-Injection-with-Dagger-2
댓글 입력