【发布时间】:2019-01-24 10:52:44
【问题描述】:
在我的应用程序中,我正在尝试为
创建Dagger 2 组件
-
Context(用于不同的类)已经完成了 - AppUtil(需要网络检查方法的上下文)需要完成这项工作!
我已经创建了组件并从 Application 类启动。
public class MyApp extends Application{
private ContextComponent mContextComponent;
private AppUtilComponent mAppUtilComponent;
private NetworkComponent mNetworkComponent;
@Override
public void onCreate() {
super.onCreate();
mNetworkComponent = createNetworkComponent();
mContextComponent = createContextComponent();
mAppUtilComponent = createAppUtilComponent();
}
private AppUtilComponent createAppUtilComponent() {
return DaggerAppUtilComponent.builder().appUtilModule(new AppUtilModule(this)).build();
}
public AppUtilComponent getAppUtilComponent() {
return mAppUtilComponent;
}
private NetworkComponent createNetworkComponent() {
return DaggerNetworkComponent.builder().networkModule(new NetworkModule()).build();
}
public NetworkComponent getNetworkComponent() {
return mNetworkComponent;
}
private ContextComponent createContextComponent() {
return DaggerContextComponent.builder().contextModule(new ContextModule(this)).build();
}
public ContextComponent getContextComponent(){
return mContextComponent;
}
}
ContextModule类如下
@Module
public class ContextModule {
private Context mContext;
public ContextModule(Context context){
mContext = context;
}
@Provides
@Singleton
Context getContext(){
return mContext;
}
}
上下文组件将是
@Singleton
@Component (modules = ContextModule.class)
public interface ContextComponent {
void inject(AppUtils appUtils);
}
AppUtilModule 就像
@Singleton
@Component (modules = AppUtilModule.class)
public interface AppUtilComponent {
void inject(SplashActivity splashActivity);
}
有了这个 AppUtilModule 已经修改为
@Module (includes = ContextModule.class)
public class AppUtilModule {
private AppUtils mAppUtils;
private Context context;
@Inject
public AppUtilModule(Context context) {
this.context = context;
}
@Provides
public AppUtils getAppUtil(){
mAppUtils = new AppUtils(context);
return mAppUtils;
}
}
现在我的 AppUtils 正在注入上下文,这完全没问题。
public class AppUtils {
public Context mContext;
@Inject
public AppUtils(Context _context){
mContext = _context;
}
public boolean isNetworkConnected(){
//Using mContext to determine Network
}
... Other Utility methods which will be used throughout my application
}
但是现在我怎样才能使
AppUtil成为一个单独的 Dagger 组件(其中 是否在内部将 Context 作为依赖项)并注入其他类?
编辑 1:在将上下文构造函数注入 AppUtils 并在 SplashActivity 中使用 AppUtil 组件后,该组件已经具有 Network 组件
将 AppUtil 设置为 Dagger 依赖项后,现在 Project 正在提供 编译时错误。在此更改之前,SplashActivity 中的 NetworkProcessor 曾经工作 很好,因为它只是 SplashActivity 的依赖项。我是什么 我错过了/做错了!
public class SplashActivity ....
{
@Inject
public NetworkProcessor mNetworkProcessor;
.....
@Inject
public AppUtils mAppUtils;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyApp.getInstance().getAppUtilComponent().inject(this);
MyApp.getInstance().getNetworkComponent().inject(this);
}
}
错误:
rror: [Dagger/MissingBinding] com.dev.myapp.networking.NetworkProcessor cannot be provided without an @Inject constructor or an @Provides-annotated method.
com.dev.myapp.networking.NetworkProcessor is injected at
com.dev.myapp.ui.activities.landing.SplashActivity.mNetworkProcessor
com.dev.myapp.ui.activities.landing.SplashActivity is injected at
com.dev.myapp.components.AppUtilComponent.inject(com.dev.myapp.ui.activities.landing.SplashActivity)
【问题讨论】:
-
好的,现在的情况和你编辑之前的情况完全不同......
-
NetworkProcessor的依赖项是什么? -
它以Retrofit为组件,在SplashActivity中使用
-
你有一个改装匕首组件?或者你在说什么样的组件?
-
我认为您的 Dagger 设置过于复杂。我制作了一个视频来帮助开发人员使用 Dagger。有些人觉得它很有用,所以也许你可以从那里得到一些想法:youtu.be/cx6pCIbOqtI
标签: android dependency-injection dagger-2