I find @AllArgsConstructor(onConstructor = @__(@Inject))
is helpful to keep code clean when working with Google Guice. I can save the constructor code. For example:
@AllArgsConstructor(onConstructor = @__(@Inject)) public class SomeClass { private final DependentClassOne classOne; private final DependentClassTwo classTwo; // ... }
For abstract
class, I’m able to use @Inject
for constructor.
@AllArgsConstructor(onConstructor = @__(@Inject)) public abstract class AbstractParentClass { private final DependentClassOne classOne; } public class ChildClass extends AbstractParentClass { private final DependentClassTwo classTwo; @Inject public ChildClass(final DependentClassOne classOne, final DependentClassTwo classTwo) { super(classOne); this.classTwo = classTwo; } }
Is it possible to save the constructor code in ChildClass
by using something like @AllArgsConstructor(onConstructor = @__(@Inject))
?
Advertisement
Answer
No, it is not possible to define AllArgsConstructor
in child class when there is parent constructor due to the limitation of Lombok (see this issue on GitHub and another answer on SO).
You could mix field/setter injection in parent with constructor injection in child, but I would advise to avoid it.