Spring Boot good practices

Table of Contents

Reading material

  1. https://www.e4developer.com/2018/08/06/spring-boot-best-practices/
  2. https://medium.com/@raviyasas/spring-boot-best-practices-for-developers-3f3bdffa0090

Good practices

  1. Keep your business logic free of Spring Boot code

    With the lessons from the “Clear Architecture” in mind, you should also protect your business logic. It is very tempting to mix all sorts of Spring Boot code there… Don’t do it. If you resist the temptation, you will keep your business logic reusable.

    It is common for parts of services to become libraries. These are much easier to create if you don’t have to remove a lot of Spring annotations from your code.

  2. Use Spring Initializr for starting new Spring Boot projects

    Creating your application with the Initializr ensures that you are picking up the tested and approved dependencies that will work well with Spring auto-configuration. You may even discover some new integrations that you were not aware exist.

  3. Use Spring auto configuration

  4. Consider creating your own auto-configuration for common organizational concerns

    Spring auto configuration

  5. Keep your @Controller’s clean and focused

    Spring - REST API design and implementation

  6. Build your @Service’s around business capabilities

    Spring - REST API design and implementation

  7. Make your database a detail – abstract it from the core logic

    Dao pattern

  8. Favour Constructor Injection

    Forms of Dependency Injection

  9. Be familiar with the concurrency model

    Spring - Concurrency Model

  10. Externalise and mature your configuration management

    Spring - Externalized Configuration

  11. Provide global exception handling

    Spring - exception handling

  12. Use Spring profiles

  13. Use a logging framework

  14. Test your code

  15. Use testing slices to make your testing easier and more focused

    https://spring.io/blog/2016/08/30/custom-test-slice-with-spring-boot-1-4

    Testing code with Spring Boot can be tricky- you need to initialize your data layer, wire numerous services, mock things… It actually does not have to be that hard! The answer is- use testing slices.

    With testing slices, you can wire-up only parts of your applications as necessary. That may save you a lot of time and ensure that your tests are not coupled to things that you are not using. There is a blog post titled Custom test slice with Spring Boot 1.4 from spring.io that explains that technique.


Links to this note