赞
踩
1.环境创建
Module:app
dependencies {
implementation 'com.google.dagger:dagger:2.8'
annotationProcessor "com.google.dagger:dagger-compiler:2.8"
annotationProcessor 'org.glassfish:javax.annotation:10.0-b28'
}
2.创建实例化的类
package com.example.dagger2test2;
import android.util.Log;
public class LoginCtrl {
private LoginStore mLoginStore;
public LoginCtrl(LoginStore store){
mLoginStore = store;
}
public void login(String name,String pass){
Log.e("tag@@", "name:"+name+" pass:"+pass);
}
}
3.创建module类
package com.example.dagger2test2; import android.content.Context; import dagger.Module; import dagger.Provides; @Module public class LoginModule { private Context mContext; //供给LoginStore使用 public LoginModule(Context context){ mContext = context; } //为LoginStore提供构造参数 @Provides Context provideStoreContext(){ return mContext; } @Provides LoginCtrl provideLoginCtrl(LoginStore storet){ return new LoginCtrl(storet); } }
4.创建Component接口
package com.example.dagger2test2;
import dagger.Component;
@Component(modules = LoginModule.class)
public interface LoginComponent {
//要添加方法,方法必须添加参数,参数类型必须和调用时候一致
void inject(MainActivity activity);
}
5.创建实例化的类的参数LoginStore
package com.example.dagger2test2; import android.content.Context; import javax.inject.Inject; public class LoginStore { private Context mContext; //这里@Inject构造方法,然后在Module类里面还需要Provide一个String的方法 @Inject public LoginStore(Context mContext) { this.mContext = mContext; MainActivity activity = (MainActivity)mContext; activity.changeHelloWord("LoginStore call"); } }
6.在Activity注入
package com.example.dagger2test2; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import javax.inject.Inject; public class MainActivity extends AppCompatActivity { @Inject LoginCtrl loginCtrl; //注入的方式实例化LoginCtrl @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化注入,inject是LoginComponent接口里面自定义的方法; DaggerLoginComponent.builder() .loginModule(new LoginModule(this)) .build() .inject(this); // 然后就可以调用loginCtrl里面的方法了 loginCtrl.login("tianping","pass"); } public void changeHelloWord(String str){ TextView textView = findViewById(R.id.hl_id); textView.setText(str); } }
运行效果
7.关系分析
8.生成的源码
// Generated by dagger.internal.codegen.ComponentProcessor (https://google.github.io/dagger). package com.example.dagger2test2; import android.content.Context; import dagger.MembersInjector; import dagger.internal.Preconditions; import javax.inject.Provider; public final class DaggerLoginComponent implements LoginComponent { private Provider<Context> provideStoreContextProvider; private Provider<LoginStore> loginStoreProvider; private Provider<LoginCtrl> provideLoginCtrlProvider; private MembersInjector<MainActivity> mainActivityMembersInjector; private DaggerLoginComponent(Builder builder) { assert builder != null; initialize(builder); } public static Builder builder() { return new Builder(); } @SuppressWarnings("unchecked") private void initialize(final Builder builder) { this.provideStoreContextProvider = LoginModule_ProvideStoreContextFactory.create(builder.loginModule); this.loginStoreProvider = LoginStore_Factory.create(provideStoreContextProvider); this.provideLoginCtrlProvider = LoginModule_ProvideLoginCtrlFactory.create(builder.loginModule, loginStoreProvider); this.mainActivityMembersInjector = MainActivity_MembersInjector.create(provideLoginCtrlProvider); } @Override public void inject(MainActivity activity) { mainActivityMembersInjector.injectMembers(activity); } public static final class Builder { private LoginModule loginModule; private Builder() {} public LoginComponent build() { if (loginModule == null) { throw new IllegalStateException(LoginModule.class.getCanonicalName() + " must be set"); } return new DaggerLoginComponent(this); } public Builder loginModule(LoginModule loginModule) { this.loginModule = Preconditions.checkNotNull(loginModule); return this; } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。