Dragger2的使用
依赖
compile 'com.google.dagger:dagger:2.11'
annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
compile 'com.google.dagger:dagger-android:2.11'
// if you use the support libraries
compile 'com.google.dagger:dagger-android-support:2.11'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
依赖冲突处理:
android{
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:3.0.2'
}
}
初级用法
/** Worm.java **/
public class Worm {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/** WormModule **/
@Module
public class WormModule {
private String name;
public AppleModule(String name) {
this.name = name;
}
@Provides
public String provideName() {
return name;
}
@Provides
public Worm provideWorm(String name) {
Worm worm = new Worm();
worm.setName(name);
return worm;
}
}
/** WormComponent.java **/
@Component(modules = {WormModule.class})
public interface WormComponent {
Worm provideWorm();
}
WormModule wormModule = new WormModule("goFuckYouSelf");
WormComponent wormComponent = DaggerWormComponent.builder().wormModule(wormModule).build();
System.out.println(wormComponent.provideWorm().getName());
多个Module
/** Apple2Module.java **/
@Module
public class Apple2Module {
@Provides
public Apple provideApple(String color) {
Apple apple = new Apple();
apple.setColor("bitch");
return apple;
}
}
/** MultComponent.java **/
@Component(modules = {Apple2Module.class, WormModule.class})
public interface MultComponent {
Worm provideWorm();
Apple provideApple();
}
/** MultProgram.java **/
public class MultProgram {
public void mult() {
MultComponent component = DaggerMultComponent.builder()
.apple2Module(new Apple2Module())
.wormModule(new WormModule())
.build();
System.out.println(component.provideApple().getColor());
System.out.println(component.provideWorm().getName());
}
}
MultComponent依赖的多个Module里面,不能有返回相同类型的方法。
Field Injection
/** InjectModule.java **/
@Module
public class InjectModule {
@Provides
public Worm provideWorm() {
Worm worm = new Worm();
worm.setName("another worm");
return worm;
}
}
/** InjectComponent.java **/
@Component(modules = {InjectModule.class})
public interface InjectComponent {
void inject(InjectProgram program);
}
/** InjectProgram.java **/
public class InjectProgram {
@Inject
public Worm worm;
public void worm() {
InjectComponent component = DaggerInjectComponent.builder().injectModule(new InjectModule()).build();
component.inject(this);
System.out.println(worm.getName());
}
}
Singleton注解
/** SingleWormModule.java **/
@Module
public class SingleWormModule {
@Provides
public String provideName() {
return "goFuckYouSelf";
}
@Provides
@Singleton
public Worm provideWorm(String name) {
Worm worm = new Worm();
worm.setName(name);
return worm;
}
}
/** SingleWormModule.java **/
@Component(modules = {SingleWormModule.class})
@Singleton
public interface SingleWormComponent {
Worm provideSingleWorm();
}
/** SingleProgram.java **/
public class SingleProgram {
public void singleWorm() {
SingleWormComponent component = DaggerSingleWormComponent.builder().singleWormModule(new SingleWormModule()).build();
Worm worm1 = component.provideSingleWorm();
Worm worm2 = component.provideSingleWorm();
System.out.println(worm1 == worm2);
}
}
/** output **/
true
Module和Component都要加,不然无效。如果Module加了,Component没加,则代码生成时会出错。
网友评论