대거 2를 실험 중이므로 프레임 워크를 이해하기 위해 테스트하고 있습니다.대거 구성 요소 종속성 의미
@Module
public class ApplicationModule {
private Application appContext;
public ApplicationModule(Application appContext) {
this.appContext = appContext;
}
@Provides
@Singleton
public Context provideContext() {
return appContext;
}
}
지금 나는 또한 NetworkComponent 원하는 : 모듈
@Component(modules = {ApplicationModule.class})
@Singleton
public interface ApplicationComponent {
Context provideContext();
}
:
그래서 내가 이런 식으로 정의 된 모든 응용 프로그램에 대한 싱글이 될 필요가있는 ApplicationComponent가 있어요 앱이 살아있는 한 살아 있어야합니다. 해당 네트워크 구성 요소는 ApplicationComponent에 대한 종속성이 있어야합니다. 그래서 나는 다음과 같이 내 networkcomponent 있습니다
@Component(dependencies = {ApplicationComponent.class}, modules = {NetworkModule.class})
@PerApp
public interface NetworkComponent extends ApplicationComponent {
@Named(DaggerConstants.DEFAULT_RETROFIT)
Retrofit provideDefault();
@Named(DaggerConstants.OTHER_RETROFIT)
Retrofit provideOther();
void inject(MainActivity activity);
}
모듈 :
1) 나는 안드로이드 응용 프로그램의 두 가지 구성 요소를 저장 :
@Module
public class NetworkModule {
@Named(DaggerConstants.DEFAULT_RETROFIT)
@PerApp
@Provides
Retrofit provideDefaultRetrofit() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.someurl.com/")
.build();
return retrofit;
}
@Named(DaggerConstants.OTHER_RETROFIT)
@PerApp
@Provides
Retrofit provideOtherRetrofit() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.someotherurl.com/")
.build();
return retrofit;
}
@PerApp
@Provides
SharedPreferences networkPreferences(Context context) {
return context.getSharedPreferences("network", Context.MODE_PRIVATE);
}
}
나는 몇 가지 질문이 있습니다. 하지만 AppComponent와 NetworkComponent를 저장하는 것이 이상하게 보입니다. 내 ApplicationComponent가 NetworkComponent를 제공하는 것이 좋지 않습니까?
2) @PerApp 및 stuff는 무엇인가 의미합니까? 또는 @PerApp 주석이있는 라이브가 Object 인 것을보고 단검인데, 그렇지 않으면 삭제합니다. 이것은 나에게 명확하지 않다.
3) 예를 들어 @Singleton을 사용하여 모듈을 표시하는 것이 유용 할 수 있습니다. 가능하기 때문에 어떤 예에서도 볼 수 없습니다.
정말 고마워요. – user1007522
@RautDarpan,'YourAwesomeComponent c'를 가지면, 이처럼'c = null;'이 null이됩니다. – azizbekian