1

I want inject an inteface and dagger automatically inject my implemntation but i got error

public interface HouseInterface {
String sayHello();}

*the implementation of interface is below *

public class Stark implements  HouseInterface{
@Inject
public Stark(){}


@Override
public String sayHello() {
    return "stark";
}

}

the house interface mosule

@Module public class HouseModule {
@Provides
public HouseInterface getHouse(Stark stark){
    return stark;
}
}

*house component : *

@Component(modules = HouseModule.class) interface HouseComponent {
HouseInterface getHouse();
}

War.class :

 public class War {
@Inject
public War() {
}

//#1
//this not working
@Inject
HouseInterface houseInterface;

//this works
//#2
// @Inject
//Stark stark;

//#3
// this works//HouseComponent houseComponent = DaggerHouseComponent.builder().houseModule(new HouseModule()).build();
//HouseInterface houseInterface = houseComponent.getHouse();

public String getHouseName() {
    return houseInterface.sayHello();
}

}

WarComponent class

@Component
public interface WarComponent {
War getWar();
void inject(War war);
}

when i test code with below codes :

    @Test
public void printNameTest() {
    String expected = "stark";
    WarComponent component = DaggerWarComponent.create();
    War war= component.getWar();
    Assert.assertEquals(war.getHouseName(), expected);
}

if in War class we use #1 to inject, not working, but if use #2, or #3 that in above codes are commented injection doesnt work , any one has any idea ? because i dont want that war.class be dependent on Stark.class and i want that it just depends on HpuseInterface so i need the #1 working

Mohammad Ranjbar Z
  • 1,487
  • 1
  • 10
  • 20
  • For field injection you have to provide a method that takes `war` as a parameter in the corresponding `component` , so that `dagger` will understand that you want to inject fields of the `war` class . – Abu Yousuf May 13 '18 at 07:24
  • @AbuYousuf in HouseComponent like below ? @Component(modules = HouseModule.class) interface HouseComponent { void getHouse(War war); } this doesnt work – Mohammad Ranjbar Z May 13 '18 at 07:29
  • What error you are getting? Provide your `WarComponent` code – Abu Yousuf May 13 '18 at 07:33
  • 2
    First of all, you're [mixing field with constructor injection](https://stackoverflow.com/q/50270375/1837367) which is why #2 should compile but not work. Other than that your WarComponent most likely knows nothing about HouseComponent, which is why it can't bind Stark as a House. Either make war a subcomponent or add a component dependency on house. – David Medenjak May 13 '18 at 07:38
  • @DavidMedenjak #2 works, #1 doesnt work and #2 depends war class to stark class, i want make #1 working to to independent war.class from stark.class or any house implementation – Mohammad Ranjbar Z May 13 '18 at 08:07
  • #2 will be null, as pointed out in the linked question, unless you have some more code for field injection that you didn't share. It should compile but not "work". It's hard to say why #1 won't work, since you did not share `WarComponent` with us. I guess that your WarComponent knows nothing about HouseComponent, which is why it can't bind Stark as a House. So either make war a subcomponent or add a component dependency on house. – David Medenjak May 13 '18 at 08:10
  • sorry @DavidMedenjak i thought that i shared war component, i edit my post Component public interface WarComponent { War getWar(); void inject(War war); } – Mohammad Ranjbar Z May 13 '18 at 08:12
  • As I assumed, WarComponent knows nothing about HouseComponent, which is why it can't bind Stark as a House. So either make WarComponent a subcomponent or add a component dependency on HouseComponent. – David Medenjak May 13 '18 at 08:15
  • `WarComponent` doesn't know `HouseModule` so of course you can't `HouseInterface` from it – EpicPandaForce May 18 '18 at 22:36

0 Answers0