gpt4 book ai didi

java - 如何使用 Dagger2 将依赖项注入(inject)任何类型的对象?

转载 作者:搜寻专家 更新时间:2023-10-30 21:08:43 25 4
gpt4 key购买 nike

根据 http://konmik.github.io/snorkeling-with-dagger-2.html我可以添加

inject(Anything anything)

进入 AppComponent.java,但这对我不起作用,在文章示例中:

@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {
void inject(MainActivity activity);
void inject(MainFragment fragment);
void inject(MainToolbarView view);
}

如果我尝试将依赖项注入(inject)到我的 fragment 中,注入(inject)的成员将保持为空。我在这里遗漏了什么明显的错误?

相关资料:

应用组件.java

@Singleton
@Component(
modules = ApplicationModule.class
)
public interface ApplicationComponent {
void injectApplication(BaseApplication application);

Prefs providePrefs();
}

应用模块.java

@Module
public class ApplicationModule {
private final Application application;

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

@Provides
Application application() {
return application;
}

@Singleton
@Provides
Prefs providePrefs() {
return Prefs.with(application);
}
}

Activity 组件.java

@AScope
@Component(
dependencies = {
ApplicationComponent.class
},
modules = ActivityModule.class
)
public interface ActivityComponent extends ApplicationComponent {
void injectActivity(BaseActivity activity);
void injectFragment(BaseFragment fragment);
}

ActivityModule.java

@Module
public class ActivityModule {
private final Activity activity;

public ActivityModule(Activity activity) {
this.activity = activity;
}

@Provides
Activity activity() {
return activity;
}

@Provides
Context context() {
return activity;
}
}

基础应用程序.java

public class BaseApplication extends Application {
private ApplicationComponent component;


@Override
public void onCreate() {
super.onCreate();
component = createComponent();

component.injectApplication(this);
}

private ApplicationComponent createComponent() {
return Dagger_ApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.build();
}

public ApplicationComponent getComponent() {
return component;
}
}

基础 Activity .java

public class BaseActivity extends ActionBarActivity implements HasComponent<ActivityComponent> {
ActivityComponent component;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

component = Dagger_ActivityComponent.builder()
.applicationComponent(((BaseApplication) getApplication()).getComponent())
.activityModule(new ActivityModule(this))
.build();

component.injectActivity(this);

}

@Override
public ActivityComponent getComponent() {
return component;
}
}

基础 fragment .java

public class BaseFragment extends Fragment {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

((BaseActivity)getActivity()).getComponent().injectFragment(this);
}
}

ListFragment.java

public class ListFragment extends BaseFragment {

@Inject ListFragmentPresenterImpl listFragmentPresenter;

public static ListFragment newInstance(){
ListFragment result = new ListFragment();

return result;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);

View rootView = inflater.inflate(R.layout.list_fragment, container, false);

Log.d("",listFragmentPresenter.sayHello()); // NPE HERE

return rootView;
}
}

ListFragmentPresenterImpl.java

public class ListFragmentPresenterImpl implements ListFragmentPresenter {

@Inject
public ListFragmentPresenterImpl() {
}

@Override
public String sayHello() {
return "hello";
}
}

在 Dagger_ActivityComponent 中生成的代码如下所示:

@Override
public void injectActivity(BaseActivity activity) {
baseActivityMembersInjector.injectMembers(activity);
}

@Override
public void injectFragment(BaseFragment fragment) {
MembersInjectors.noOp().injectMembers(fragment);
}

这里不应该是一个 baseFragmentMemebersInjector 吗?

谢谢!

最佳答案

注入(inject)子 fragment 而不是 BaseFragment。在你的情况下:

public class ListFragment extends BaseFragment {

@Inject ListFragmentPresenterImpl listFragmentPresenter;

public static ListFragment newInstance(){
ListFragment result = new ListFragment();
return result;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((MainActivity)getActivity()).getComponent().injectFragment(this);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.list_fragment, container, false);
Log.d("",listFragmentPresenter.sayHello()); // NPE HERE
return rootView;
}
}

在你的组件类中:

@AScope
@Component(dependencies = ApplicationComponent.class,
modules = ActivityModule.class)
interface ActivityComponent {
void injectActivity(MainActivity activity);
void injectFragment(ListFragment fragment);
// Put any more injects here, if BaseFragment has
// any other children that need to be injected, for example:
void inject(MapFragment fragment);
void inject(DetailFragment fragment);
}

Parent -> Child 注入(inject)在 Dagger 2 中不起作用。它被覆盖了 here , herethis SO question .

关于java - 如何使用 Dagger2 将依赖项注入(inject)任何类型的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29328639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com