提交 1517a81c 编写于 作者: 门心叼龙's avatar 门心叼龙

add component

上级 07bf4fde
......@@ -26,7 +26,7 @@ dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
compile 'com.google.dagger:dagger:2.22.1'
implementation 'com.google.dagger:dagger:2.22.1'
annotationProcessor 'com.google.dagger:dagger-compiler:2.22.1'
}
......@@ -4,9 +4,10 @@ import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.android.dagger.component.DaggerAppleComponent;
import com.android.dagger.component.DaggerAcomponent;
import com.android.dagger.contract.Fruit;
import com.android.dagger.entity.Apple;
import com.android.dagger.entity.Banana;
import com.android.dagger.entity.Orange;
import com.android.dagger.module.AppleModule;
......@@ -15,35 +16,46 @@ import javax.inject.Inject;
public class MainActivity extends AppCompatActivity {
public static final String TAG = MainActivity.class.getSimpleName();
@Inject
public Fruit mFruit;
public Banana mBanana;
@Inject
public Apple mApple;
@Inject
public Orange mOrange;
// @Inject
// public Fruit mFruit;
//
// @Inject
// public Apple mApple;
//
// @Inject
// public Orange mOrange;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//DaggerAcomponent.builder().bcomponent(n).inject(this);
//DaggerAcomponent.create().inject(this);
//DaggerAcomponent.builder().build().inject(this);
//DaggerAppleComponent.create().inject(this);
DaggerAppleComponent.builder().appleModule(new AppleModule()).build().inject(this);
if(mFruit == null){
Log.v(TAG,"fruit is null");
}else{
Log.v(TAG,"fruit is not null");
Log.v(TAG, mFruit.getColor()+":"+mFruit.size);
}
if(mApple == null){
Log.v(TAG,"apple is null");
//DaggerAppleComponent.builder().appleModule(new AppleModule()).build().inject(this);
// if(mFruit == null){
// Log.v(TAG,"fruit is null");
// }else{
// Log.v(TAG,"fruit is not null");
// Log.v(TAG, mFruit.getColor()+":"+mFruit.size);
// }
//
// if(mApple == null){
// Log.v(TAG,"apple is null");
// }else{
// Log.v(TAG,"apple is not null");
// Log.v(TAG, mApple.getColor()+":"+mApple.size);
// }
// Log.v(TAG, "orange:"+mOrange.getColor()+":"+mOrange.size);
if(mBanana == null){
Log.v("MYTAG","banana is null");
}else{
Log.v(TAG,"apple is not null");
Log.v(TAG, mApple.getColor()+":"+mApple.size);
Log.v("MYTAG","banana is not null");
Log.v("MYTAG",mBanana.toString());
}
Log.v(TAG, "orange:"+mOrange.getColor()+":"+mOrange.size);
}
public static void main(String[] args){
......
package com.android.dagger.component;
import com.android.dagger.MainActivity;
import com.android.dagger.module.AppleModule;
import com.android.dagger.module.AppleModule1;
import com.android.dagger.module.AppleModule2;
import dagger.Component;
/**
* Description: <AppleComponent><br>
* Author: gxl<br>
* Date: 2019/5/17<br>
* Version: V1.0.0<br>
* Update: <br>
*/
@Component(modules = {AppleModule.class,AppleModule1.class,AppleModule2.class})
public interface AppleComponent {
public void inject(MainActivity activity);
}
package com.android.dagger.component;
import com.android.dagger.entity.Banana;
import com.android.dagger.module.BananaModule;
import dagger.Component;
import dagger.Provides;
@Component(modules = {BananaModule.class})
public interface Bcomponent {
public Banana banana();
}
package com.android.dagger.module;
import com.android.dagger.entity.Banana;
import dagger.Module;
import dagger.Provides;
@Module
public class BananaModule {
@Provides
public Banana providerBanana(){
return new Banana(12,"hahah111111111111111222222222222222221");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册