Constructor injection on abstract class and children
Solution 1
NotificationCenter
is not a real class but an abstract class, so you can't create the instance of it. On the other hand, it has a field (final field!) EmailService
that has to be initialized in constructor! Setter won't work here, because the final field gets initialized exactly once. It's Java, not even Spring.
Any class that extends NotificationCenter
inherits the field EmailService because this child "is a" notification center
So, you have to supply a constructor that gets the instance of email service and passes it to super for initialization. It's again, Java, not Spring.
public class NotificationCenterA extends NotificationCenter {
private final TemplateBuildingService templateBuildingService;
public NotificationCenterA(EmailService emailService, TemplateBuildingService templateBuildingService) {
super(emailService);
this.templateBuildingService = templateBuildingService;
}
}
Now spring manages beans for you, it initializes them and injects the dependencies. You write something that frankly I don't understand:
...as the first statement to NotificationCenterA constructor but I don't have an instance of the emailService and I don't intend to populate the base field from children.
But Spring will manage only a NotificationCenterA
bean (and of course EmailService implementation), it doesn't manage the abstract class, and since Java puts the restrictions (for a reason) described above, I think the direct answer to your question will be:
- You can't use setter injection in this case (again, because of final, it is Java, not because of Spring)
- Constructor injection, being in a general case better than setter injection can exactly handle your case
Solution 2
First point :
@Component
is not designed to be used in abstract class that you will explicitly implement. An abstract class cannot be a component as it is abstract.
Remove it and consider it for the next point.
Second point :
I don't intend to populate the base field from children.
Without Spring and DI, you can hardcoded the dependency directly in the parent class but is it desirable ? Not really.
It makes the dependency hidden and also makes it much more complex to switch to another implementation for any subclass or even for testing.
So, the correct way is injecting the dependency in the subclass and passing the injected EmailService in the parent constructor :
@Service
public class NotificationCenterA extends NotificationCenter {
private final TemplateBuildingService templateBuildingService;
public NotificationCenterA(TemplateBuildingService templateBuildingService, EmailService emailService) {
super(emailService);
this.templateBuildingService = templateBuildingService;
}
}
And in the parent class just remove the useless @Component
annotation.
Any idea what's the proper way to handle this situation? Maybe I should use field injection?
Not it will just make your code less testable/flexible and clear.
Related videos on Youtube
Comments
-
prettyvoid over 1 year
I have a class as follows
@Component public abstract class NotificationCenter { protected final EmailService emailService; protected final Logger log = LoggerFactory.getLogger(getClass()); protected NotificationCenter(EmailService emailService) { this.emailService = emailService; } protected void notifyOverEmail(String email, String message) { //do some work emailService.send(email, message); } }
EmailService
is a@Service
and should be auto-wired by constructor injection.Now I have a class that extends
NotificationCenter
and should also auto-wire components@Service public class NotificationCenterA extends NotificationCenter { private final TemplateBuildingService templateBuildingService; public NotificationCenterA(TemplateBuildingService templateBuildingService) { this.templateBuildingService = templateBuildingService; } }
Based on the above example the code won't compile because there is no default constructor in the abstract class
NotificationCenter
unless I addsuper(emailService);
as the first statement toNotificationCenterA
constructor but I don't have an instance of theemailService
and I don't intend to populate the base field from children.Any idea what's the proper way to handle this situation? Maybe I should use field injection?
-
x80486 over 5 yearsYou would need both, right? Inject a compatible instance of
EmailService
andTemplateBuildingService
, then call super forEmailService
. I do not see any other way(s).
-
-
prettyvoid over 5 yearsSolid points that cleared my confusion, thanks for the write-up.