In the following code is it required to annotate the constructor of my base class with "Inject"? what if the base class is an abstract class?
Is it required to call super in my constructor when using DI?
public class Base {
@Inject
public Base(IConfig config) {
// Do stuff
}
}
public class A extends Base {
@Inject
public A(IConfig config) {
super(config);
}
}
Depends on what you want to bind. If you bind Base
to A
(bind(Base.class).to(A.class)
), then yes, the second constructor and @Inject is needed, but the one on Base is not. If you plan to construct also Base
, you need the @Inject
.
Concerning the call to super()
, Java needs it (this has nothing to do with Guice) if you have only a single constructor with IConfig. But nothing prevent you to remove it if you don't need to inject IConfig
in Base
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With