Skip to content

Instantly share code, notes, and snippets.

@jose-mgmaestre
jose-mgmaestre / Body.java
Created April 13, 2018 15:25
Body as simple as possible to explain the injection
package com.ricston.injectionexample.domain;
import com.ricston.injectionexample.domain.com.ricston.injectionexample.domain.blood.Blood;
import javax.inject.Inject;
public class Body {
@Inject
public Body(){}
public class Person{
Body body;
@Inject
public Person(Body body){
this.body = body;
}
}
@jose-mgmaestre
jose-mgmaestre / Body.java
Created April 13, 2018 15:35
Body with Blood
public class Body {
@Inject
Blood blood;
@Inject
public Body(){}
public abstract class Blood {
public abstract String getKindOfBlood();
}
public class A_Blood extends Blood{
@Override
public String getKindOfBlood() {
return "A";
}
}
@jose-mgmaestre
jose-mgmaestre / RandomInjectionModule.java
Last active April 16, 2018 13:17
This class will inject random blood objects for a Blood interface
@Module
public class RandomInjectionModule {
private static Blood blood;
public RandomInjectionModule() {
}
@Provides
static Blood provideBlood(){
@Component( modules = RandomInjectionModule.class )
public interface Doctor {
Body injectBlood();
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Body body = createBody();