I joined a company as a junior developer and I get assigned to do maintenance and fixes to various applications.
In every project that I had to work in a team I always heard complains about my code being too "framework coupled" and that I should write the code using plain Java rather than using the Spring Framework's features (heck, I even got yelled at for using @Autowired rather than Constructor Injection)
I am really frustrated by this thing and I want to know if I am in the wrong. Aren't the framework's features the main reason a programmer is going to use that framework?
CodePudding user response:
Usually, the reasoning behind that way of thinking is that it would be easier to one fine day swap the Framework you use today to some other with the minimum required effort. You might argue that this isn't something very common or that you do very often, but still, even if the reason does not convince you it is generally a good practice to try to decouple your code as much as possible from the underlying Framework for multiple reasons (in addition to the one just mentioned you also have unit testing made easier).
In regards to the @Autowired
topic, constructor injection is usually preferred for two main reasons:
- You clearly define the dependencies for your class to work properly;
- It is easier to unit test it because mocking its dependencies is way easier if you can "inject" them via the constructor.