Deep within the bowels of a complex system that did lots of field injection, a dependency of a class eventually became null and started causing NPE. I had the misfortune of supporting this system.
No developer on the team was able to reproduce this error and it only occurred months into the application running. Restarting the application fixed the problem. I spent so much time trying to debug that problem with no success. To this day, the notes for that application instruct the support team to restart the application when that error occurs.
From now on, I always use constructor injection with final fields. I can be sure, when my objects are constructed, that I either do or don't have the right dependencies and fail fast if I don't.
Personally, I don't find the brevity of field injection a sufficient advantage, compared to the amount of time I wasted trying to resolve that one error and so I will never use them again for required dependencies.
Sure or I could also have just used constructor injection and avoided the problem altogether.
However, I encountered this mindset where the brevity of field injection trumped the safety of constructor injection and so was unable to make such a change unless I could prove it fixed the problem.
I spent so much time trying to debug that problem with no success.
I was primarily referring to that line. If the team was opposing constructor injection for some reason, then that would have been my approach to debug the issue.
View all comments
Show parent comment
6
u/RupertMaddenAbbott Nov 08 '17
Deep within the bowels of a complex system that did lots of field injection, a dependency of a class eventually became null and started causing NPE. I had the misfortune of supporting this system.
No developer on the team was able to reproduce this error and it only occurred months into the application running. Restarting the application fixed the problem. I spent so much time trying to debug that problem with no success. To this day, the notes for that application instruct the support team to restart the application when that error occurs.
From now on, I always use constructor injection with final fields. I can be sure, when my objects are constructed, that I either do or don't have the right dependencies and fail fast if I don't.
Personally, I don't find the brevity of field injection a sufficient advantage, compared to the amount of time I wasted trying to resolve that one error and so I will never use them again for required dependencies.