Java & Guice - How to deal with inheritance and abstraction?

Does the following code need to annotate the constructor of my base class with "Inject"? what if the base class is an abstract class?

Is super call required 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); } } 
+4
source share
1 answer

Depends on what you want to tie. If you bind Base to A ( bind(Base.class).to(A.class) ), then yes, you need a second constructor and @Inject, but the one on Base is not. If you plan to build Base as well, you will need @Inject .

As for calling super() , it needs it (this has nothing to do with Guice) if you only have one constructor with IConfig. But nothing prevents you from deleting it if you don't need to enter IConfig in Base .

+4
source

Source: https://habr.com/ru/post/1398446/


All Articles