1
내 프로젝트에 DI가 있습니다. 그들은 모두가 좋은 것 같다 SharedPreferences
에 저장 모듈 중 하나는 AppModule
이고 그는 내 사용자 이름과 비밀번호를 입력하고 로그인 조각에 RestClient
동적 데이터를 사용하는 대거
@WithoutAuth
@Provides
@Singleton
RestAdapter provideRestAdapter(final RemontnikPreferences preferences) {
final String userAndPassword = preferences.getUsername() + ":" + preferences.getPassword();
RequestInterceptor requestInterceptor = new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
request.addHeader("Authorization", "Basic " + Base64.encodeToString(userAndPassword.getBytes(), Base64.NO_WRAP));
}
};
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("https://")
.setRequestInterceptor(
.build();
preferences.setPassword("");
return restAdapter;
}
를 제공하고 있습니다. 승인이 작동합니다.
하지만 내가 잘못된 자격 증명을 입력하면 공유 환경 설정에 저장되지만은 올바른 자격 증명을 다시 입력 할 때 AppModule
이 응용 프로그램이 시작될 때 한 번만 인스턴스화하기 때문에 업데이트되지 않습니다. 새로운 공유 환경 설정을 사용하려면 AppModule
을 어떻게 다시 만들 수 있습니까?
가장 간단한 해결책은'@ Singleton'을 제거하는 것이지만이 의존성을 요청할 때마다 새 인스턴스가 생성됩니다. – tomrozb
@tomrozb 하지만 클래스에 한 번만 종속성을 요청합니다 ... 필요할 때 종속성을 수동으로 업데이트 할 수 있습니다. – onCreate
당신은 할 수 없습니다. 종속성을 재발 명하려면 클래스의 새 인스턴스를 만들어야합니다. – tomrozb