这个匕首简单的例子有什么问题?

时间:2017-03-02 12:01:39

标签: java android dependency-injection dagger-2

1 - 范围:

@Scope
@Retention(RetentionPolicy.CLASS)
public @interface PerInstance {}

2 - AppContextModule:

@Module
public class AppContextModule {

Application application;

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

@Provides
public Application application(){
    return this.application;
}

@Provides
public Context context(){
    return this.application;
}

@Provides
public LocationManager locationManager(Context context){
    return (LocationManager)      context.getSystemService(context.LOCATION_SERVICE);
}

}

3 - MeuPrimeiroModule:

@Module
public class MeuPrimeiroModule {

@Provides
@PerInstance
public String nome() {
    return new String("Gorick");
}

}

4 - MeuSegundoModule:

@Module(includes = MeuPrimeiroModule.class)
public class MeuSegundoModule {

@Provides
@Singleton
public String nomeCompleto(MeuPrimeiroModule meuPrimeiroModule) {
    return new String(meuPrimeiroModule + " Silva");
}

}

5 - MeuPrimeiroComponent:

@PerInstance
@Component(modules={MeuPrimeiroModule.class})
public interface MeuPrimeiroComponent {
void inject(MainActivity mainActivity);
}

6 - MeuSegundoComponent:

@Singleton
@Component(modules={MeuSegundoComponent.class})
public interface MeuSegundoComponent extends MeuPrimeiroComponent {

void inject(MainActivity mainActivity);
}

7 - AppContextComponent:

public interface AppContextComponent {

Application app(); //provision method
Context applicationContext(); //provision method
LocationManager locationManager(); //provision method
}

8 - ApplicationComponent:

@Singleton
@Component(modules={AppContextModule.class}) 
public interface ApplicationComponent extends AppContextComponent {
void inject(MainActivity mainActivity);
}

9 - MainActivity:

public class MainActivity extends AppCompatActivity {

@Inject
MeuPrimeiroComponent meuPrimeiroComponent;

@Inject
MeuSegundoComponent meuSegundoComponent;

TextView nome, nomeCompleto;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    nome = (TextView) findViewById(R.id.nome);
    nomeCompleto = (TextView) findViewById(R.id.nomeCompleto);

    setNome(nome, nomeCompleto);
}

public void setNome(TextView nome, TextView nomeCompleto){
    nome.setText(meuPrimeiroComponent.toString());
    nomeCompleto.setText(meuSegundoComponent.toString());
}

}

构建

Error:(16, 10) error: gorick.dagger2.Dagger2.Component.MeuPrimeiroComponent cannot be provided without an @Provides- or @Produces-annotated method.
gorick.dagger2.Dagger2.Component.MeuPrimeiroComponent is injected at
gorick.dagger2.MainActivity.meuPrimeiroComponent
gorick.dagger2.MainActivity is injected at
gorick.dagger2.Dagger2.Component.ApplicationComponent.inject(mainActivity)

PS:如果我使用meuPrimeiroComponent.nome(),android studio找不到nome()方法。

1 个答案:

答案 0 :(得分:0)

您不能在组件中注入值,而是需要注入构造函数包含由提供程序返回的值的对象

更多信息请参阅Example

您需要在onCreate()上构建这样的组件:

DaggerMeuPrimeiroComponent.builder()
                // list of modules that are part of this component need to be created here too
                .appContextModule(new AppContextModule(getApplicationContext())) // This also corresponds to the name of your module: %component_name%Module
                .build().inject(this);