Introduction to Spring Boot
What is Spring ?
A lightweight alternative to Java EE in terms of component code. It utilizes dependency injection and Aspect oriented programming to achieve capabilities of EJB (Enterprise Java Beans) with POJOs (Plain Old Java Objects). It handles infrastructure so that developer can focus on application and business logic.
Evolution of Spring:
- Spring 2.0 was configured with custom XML namespaces.
- Spring 2.5 introduced annotation based component scanning — such as @Component and @Autowired annotations.
- Spring 3.0 introduced Java based configuration alternative to XML — @Enable-prefixed annotations completely removing XML configuration.
- Spring 4.0 unleashed support for conditional configuration, runtime decisions determine which configuration would be used and which will be ignored.
- Spring 5.0 added support for reactive programming and which is a significant paradigm shift and baseline support for java 8.
What were the issues with Spring?
In order to write a simple Hello World Web Application, we would require -
- A project structure with Maven or Gradle build file including required dependencies.
- A web.xml file that declare Spring’s DispatcherServlet.
- A Spring Configuration that enables Spring MVC.
- A controller class that will respond to requests with “Hello World”.
- A web Application server, such as Tomcat, to deploy the application.
A lot of boiler plate code has to be written and maintained in order to create a simple REST controller that returns a “Hello World” message thus increasing developer’s effort towards application configuration over business logic.
Advantages of Spring Boot? What does it bring to the table?
- Automatic Configuration
Spring Boot automatically provides configuration for application functionality common to many Spring Applications. If Spring Boot detects that there is a library (for example — Security, JPA, H2, Thymeleaf, Spring MVC) in application class path, it automatically configures it. No need to explicitly declare beans and provide boilerplate code.
2. Starter Dependencies
Just tell Spring Boot what kind of functionalities are needed and it will ensure that the libraries are added to the build. It helps with project dependency management by providing special Maven/Gradle dependencies that take advantage of transitive dependency resolution. Also, there is no need to worry about versions of the libraries needed since the libraries pulled by starters are tested together.
3. Command line interface
Optional feature of Spring Boot lets write complete applications with just application code. No need of traditional project build. It leverages starter dependencies and auto-configuration to let the user focus on writing code. No need for import statements as the cli detects what dependencies are required and adds them onto class path. Once those dependencies are added, auto-configuration kicks in and ensures DispatcherServlet and Spring MVC are enabled so that the controller can response to HTTP requests.
Spring Boot Actuator gives insight into what is going on inside of a running spring boot application. It offers ability to inspect application at runtime that include -
- What beans have been configured in Spring Application context.
- What decisions were made by Spring’s auto-configuration.
- What environment variables, system properties, configuration properties, and command line arguments are available to your application.
- The current state of the threads in and supporting the application.
- A trace of recent HTTP requests handled by the application.
- Various metrics pertaining to memory usage, garbage collection, web requests and data source usage.
What Spring Boot is NOT?
- Spring Boot is not an Application Server. It accomplishes this by embedding a servlet container (tomcat, jetty, undertow) within the application.
- Spring Boot does not implement Java Specifications such as JPA or JMS.
- Spring Boot does not employ any code generation to accomplish auto-configuration.