What are some of the best practices for Spring Framework?

Technology CommunityCategory: SpringWhat are some of the best practices for Spring Framework?
VietMX Staff asked 3 years ago
  1. Avoid version numbers in schema reference, to make sure we have the latest configs.
  2. Divide spring bean configurations based on their concerns such as spring-jdbc.xml, spring-security.xml.
  3. For spring beans that are used in multiple contexts in Spring MVC, create them in the root context and initialize with listener.
  4. Configure bean dependencies as much as possible, try to avoid autowiring as much as possible.
  5. For application-level properties, the best approach is to create a property file and read it in the spring bean configuration file.
  6. For smaller applications, annotations are useful but for larger applications, annotations can become a pain. If we have all the configuration in XML files, maintaining it will be easier.
  7. Use correct annotations for components for understanding the purpose easily. For services use @Service and for DAO beans use @Repository.
  8. Spring framework has a lot of modules, use what you need. Remove all the extra dependencies that get usually added when you create projects through Spring Tool Suite templates.
  9. If you are using Aspects, make sure to keep the join pint as narrow as possible to avoid advice on unwanted methods. Consider custom annotations that are easier to use and avoid any issues.
  10. Use dependency injection when there is an actual benefit, just for the sake of loose-coupling don’t use it because it’s harder to maintain.