CoreModule에서 제공하는 클래스의 인스턴스를 삽입하기 위해 @Singleton 범위를 사용하여 CoreComponent 광고를 정의하는 Core Android 라이브러리가 있습니다.단검 2 : 여러 구성 요소간에 동일한 인스턴스를 다른 라이브러리 모듈에서 동일한 범위로 제공하십시오.
@Singleton
@Component(modules = {CoreModule.class})
public interface CoreComponent {
void inject(SomeClass target);
}
@Module
public class CodeModule {
@Singleton
@Provides
CoreRepository provideCoreRepository() {
return new CoreRepositoryImpl();
}
}
나는 핵심 라이브러리에 의존하고 다른 구성 요소를 사용하는 다른 안드로이드 라이브러리에서 동일한 @Singleton의 인스턴스에 액세스하고 싶습니다.
@Singleton
@FooScope
@Component(modules = {CoreModule.class, FooModule.class})
public interface FooComponent {
void inject(SomeActivity target);
}
public class FooActivity extends AppCompatActivity {
@Inject
public CoreRepository repo;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
injectDependencies();
super.onCreate(savedInstanceState);
}
[...]
}
위의 코드는 빌드되지만 @Singleton 범위는 구성 요소에 대해 "로컬"입니다. 즉, CoreComponent와 FooComponent에 대한 두 개의 싱글 톤 인스턴스가 있습니다.
Android Application
├── Foo Library
| └── Core Library
├── Bar Library
| └── Core Library
·
·
·
└── Core Library
나는 최선의 해결책은 코어 도서관은 다른 도서관의 가시성이 없기 때문에 불행히도, 가능하지 않는 것, 하위 구성 요소를 사용하고 있지만되어야한다고 생각합니다.
클래스가 동일한 범위로 주석을 달면 구성 요소간에 한 클래스의 동일한 인스턴스를 대거와 공유 할 수있는 다른 방법이 있습니까?
@Singleton
@Component(modules = {CoreModule.class})
public interface CoreComponent {
CoreRepository coreRepository();
}
응용 프로그램 내부의이 싱글 범위의 구성 요소에 대한 참조를 만듭니다 : -
당신이 [구성 요소 종속성]로 봤어 당신에 삽입 할 수 (http://stackoverflow.com/q/29587130/1426891) 아직? [Javadoc] (https://google.github.io/dagger/api/latest/dagger/Component.html#component-dependencies) 여기를 참고하고 CoreComponent에 몇 가지 엑스트라 접근 메소드를 명시 적으로 추가해야 할 수도 있습니다. FooComponent가 호출 할 수 있습니다. –
Checkout https://stackoverflow.com/questions/27036933/how-to-set-up-dagger-dependency-injection-from-scratch-in-android-project 여기에는 범위와 종속 구성 요소를 사용하는 예가 있습니다. –