Wednesday, July 3, 2019

Spring / Spring-Boot Best Practices


  • Spring Java Configuration is recommended over xml configuration.
    • XML is not type safe, runtime error checking/corrections.
    • Java configuration can be debugged.
  • Avoid injecting Spring in domain classes. OK to inject spring in spring classes.
    • Easy to extract/port domain, if required.
    • Inject beans in spring configuration classes.
    • You can use javax annotations like @Named and @Inject in domain classes.
  • Minimise spring component scan, OK for controllers package.
    • You can inject spring (like @Autowired) here, where Spring exists like @RestController.
    • Slows down the app startup.
  • Exception handling can be a combination of central (preferred as much as it can be done) and local (as required)
    • Spring 3.2 had introduced @ControllerAdvice, in addition to the erstwhile @ExceptionHandler. This enables you implement centralised exception handling.
    • Spring 5.0 has introduced ResponseStatusException, which gives you more control over type of exceptions that can be raised.
    • Recommended approach is to have one exception being handled in one of the above manner.
  • Logging should be considered as a cross cutting concern, in addition to the method specific logging, as required.
    • Practice 1: Suggest to have Trace level logs and Debug level logs for every method. This can be done by @Around advice in a Logger aspect. Trace level log is entry/exit log and Debug level log is input parameters and return parameter for a method.
    • Practice 2: Suggest to have request tracing logs at the entry/exit of the http request. Do not log input param and return params. This can be done by @Around advice in a Logger aspect. This is typical of applications where there is very good unit test and component test coverage.
    • Specific important logging should be done in addition to the Logger Aspect, irrespective of Practice 1 or Practice 2.
    • Implement dynamic log level changes. This is available in spring boot actuator, but will need explicit handling in case of a clustered microservice, in case of Practice 1 of logging.
  • You can use Spring Initialzr for setting up Spring boot project, for the required boot modules.
    • Starter modules for keycloak, flyway, data, etc available.
    • This is typically useful only for initial setup of the project.
  • Use SpringBootTest or SpringJUnit4ClassRunner for Unit tests. Avoid using PowerMock.
  • Maven specific : Have a central spring dependency version management - this is called BOM(Bill Of Materials). The Spring BOM can be inherited as a parent or can be imported in dependency management.
  • Microservices specific - Distributed Tracing: The observability aspect of “Distributed tracing” can be implemented using Spring Sleuth/Zipkin.
  • Application Metrics - You can use Spring Boot Actuator. Micrometer is the Spring Boot 2’ application metrics collector - it is a dimensional-first metrics collection facade. Micrometer is included in Spring Boot 2’s actuator. It also has been back ported in Spring Boot 1.5, 1.4 and 1.3 with the addition of another dependency. Micrometer gives the flexibility of integration with dimensional monitoring systems like Cloudwatch, Prometheus, InfluxDB, New Relic, etc.

No comments:

Post a Comment