Android Dagger2

时间:2018-01-11 14:00:08

标签: android dependency-injection dagger

我对Android中的Dagger2感到困惑。 我用两个范围。 @Singleton,@ PerActivity

这是我的代码。我只是想做我的代码。

//ApplicationComponent.java
@Singleton
@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {
    @Named("packageName") String packageName();
}


//ApplicationModule.java
@Module
public class ApplicationModule {  

    @Provides
    @Singleton
    public Context provideApplicationContext() {
        return MyApplication.getContext();
    }

    @Provides
    @Singleton
    @Named("packageName")
    public String providePackageName(Context context) {
        return context.getPackageName();
    }
}

//UserComponent.java
@PerActivity
@Component(modules = {UserModule.class})
public interface UserComponent {
    void inject(MainActivity activity);
}

//UserModule.java
@Module
public class UserModule {
    String packageName;

    public UserModule(String packageName) {
        this.packageName = packageName;
    }


    @Provides
    @PerActivity
    UserRepositoryImpl provideUserRepositoryImpl() {
        return new UserRepositoryImpl(packageName);
    }
}

用于注入appVersion,在UserModule中包名称

DaggerChatComponent.builder()
                .userModule(new UserModule(getApplicationComponent().packageName()))
                .build();

但看起来并不好。如何在使用不同的范围时注入?

1 个答案:

答案 0 :(得分:0)

你的 ApplicationModule.java 是正确的

@Module
public class ApplicationModule {

    private Application application;

    public ApplicationModule(Application application){
        this.application = application;
    }

    @Provides
    @Singleton
    Context provideContext(){
        return application;
    }

    @Provides
    @Singleton
    @Named("packagename")
    public String providePackageName(Context context) {
        return context.getPackageName();
    }
}

它的组件类也正确 ApplicationComponent.java

@Singleton
@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {


    @Named("packagename") String providepackagename();
}

但是在 UserModule.java 中你不需要传递包名对象,dagger的对象图就是为你做的。

@Module
public class UserModule {


    public UserModule() {
    }


    @Provides
    @PerActivity
    UserRepositoryImpl provideUserRepositoryImpl(@Named("packagename") String packageName) {
        return new UserRepositoryImpl(packageName);
    }
}

下一步是在为此模块编写组件类时,将应用程序组件添加为依赖项,即 UserComponent.java 看起来像这样

@PerActivity
@Component(dependencies = {ApplicationComponent.class},modules = {UserModule.class})
public interface UserComponent {

    void inject(MainActivity mainActivity);
}

活动范围为 PerActivity.lava

@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface PerActivity {
}

将此示例 UserRepositoryImpl.java 作为

class UserRepositoryImpl {

    private String packagename;

    public UserRepositoryImpl(String packagename){

        this.packagename = packagename;
    }

    String getPackagename(){
        return packagename;
    }
}

你最终可以在你的活动中注入它。(MainActivity.java)

public class MainActivity extends AppCompatActivity {

    @Inject
    UserRepositoryImpl userRepository;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ApplicationComponent component=DaggerApplicationComponent.builder().applicationModule(new ApplicationModule(getApplication())).build();
        UserComponent userComponent=DaggerUserComponent.builder().applicationComponent(component).userModule(new UserModule()).build();
        userComponent.inject(this);
        Log.e("name"," "+userRepository.getPackagename());
    }
}