Applied Code,Project Standards and Spring boot Application tuning. #37
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I have made the changes to the code considering the standards and tuning:
Spring Dependency Injection Standards:
Constructor Based Dependency Injection should be used, instead of Field Injection, as it helps during testing for creation of mocking objects. Also if we identify that there are lots of dependencies that we are injecting through Constructor this possibly means that our class is doing too many things and not following the SRP(Single Responsibility Principle).
By default the Spring beans are eagerly initialized but some beans/dependencies are better to be injected only when they are required,for ex: EmailService bean is not required unless and until there is requirement to send the mail, hence @lazy should be used.
Code standards:
Instead of creating an object every time,it is better to create an method that will provide that object as needed, like factory,for
ex As ModelAndView is created every time controller, we can declare a method will provide the ModelAndView on basis of parameters, for which concepts like Method Overloading can be used.
Constants should be used for view name and for request mapping path because if in the future view name changes, and if you have used it multiple time like admin/product/add, then we have to make changes add only one place.
Minor Code Changes/Best Practices:
I would also like to add:
Thanks you:)