Simplifying Spring application development using Spring Boot
As we have discussed in the previous section, the Spring Framework provides lot of flexibility to configure beans in the Spring application in multiple ways such as XML, annotation, and Java configuration. But remember, if the number of modules and features increases in the Spring application, it also increases the complexity in the configuration. After a point, your Spring application tends to become tedious and error-prone.
Here, Spring Boot comes into the picture to address the complexity of the configuration of your Spring application.
Spring Boot does exactly what you are looking for. It will do things automatically for you but allows you to override the defaults if required. (Remember the point about it being an opinionated framework?)
Spring Boot is not a separate framework, but it is Spring at heart. It is built on top of the Spring Framework to remove tedious work from the developer end and allow developers to focus on the business code with minimal or zero configurations.
See the following diagram that shows what Spring Boot is exactly:
In the preceding diagram, you can see that Spring Boot is the surface layer over the Spring Framework, with all of the modules such as Web (MVC), JDBC, Security, Batch, and so on. It presents a small surface area for User to approach and extract value from the rest of Spring.
Suppose you are working with a task, a Hello World web application. If you are choosing to develop with the Spring Framework, what would you need to do?
The following are the bare minimum configurations required for a small web application:
- Creating a project structure either by using Maven or Gradle and defining required dependencies such as Spring MVC and the Servlet API dependencies for your case.
- A deployment descriptor file, that is, web.xml. In the case of Java configuration, you require the WebApplicationInitializer implementation class that declares Spring's DispatcherServlet.
- The Spring MVC configuration class to enable the Spring MVC module for your Hello World application.
- You have to create a controller class that will respond to your request.
- You require a web application server such as Tomcat.
Of these points, most are generic boilerplate code and common configuration for a Spring web application, except writing application-specific controllers. So, Spring Boot provides all common configurations and boilerplate code based on the available library of the classpath. You don't need to take responsibility for writing this common and generic code.
Let's create the same Hello World application using Spring Boot. Suppose for a moment we are using a Groovy-based controller class as follows:
@RestController class HelloController { @GetMapping("/") String hello() { "Hello World!!!" } }
This code is a complete Spring web application, with nothing required to configure. No web.xml file, no build specification, and not even an application server. This is the entire application. We can run this application using Spring Boot CLI with the following command:
$ Spring run HelloController.groovy
So, you can see how Spring Boot simplifies Spring application development. We will also see the same application using Java in the next section of this chapter.
Spring Boot does not compete with the Spring or Spring MVC Framework. It makes it easy to use them in the Spring application.