Good ways to develop your code
Since we just challenged some of Spring Framework's most important features and practices, now it's time to shed some light on good ways of developing your code. Let's start again with dependency injection.
Don't inject too much
As we already mentioned, the @Autowired
annotation will inject all necessary dependencies. To avoid the problem with dependency injection that we explained previously, try to avoid injecting too many dependencies into a single class. Usually, it's good if you have very few beans that your class requires. It's up to you to maintain this balance and organize your architecture.
Use a closed-visibility approach
To avoid problems caused by exposing your methods and fields publicly, consider making them at least package-level visible at first. Ideally, all fields encapsulated in your classes should be private or protected, or at package-level visibility. Expose those fields or methods only if needed. Consider the consequences of giving access...