The advantages of using Spring Boot
The following are the four main advantages of using Spring Boot to develop applications:
- Autoconfiguration: When you’re configuring your Spring Boot application, it downloads all the dependencies that will be needed to run your application. It will also configure your Spring Framework with the relevant third-party packages, depending on the settings you have applied. Thus, Spring Boot avoids boilerplate code and configuration errors, and you can directly start developing your Spring application.
- Opinionated approach: Spring Boot uses a narrow approach to installing dependencies based on your application needs. It will install all the required packages of your application and removes the idea of configuring it manually.
- Spring starters: You can choose a list of starter dependencies to define your application’s expected needs during the initialization process. One example of a Spring Starter is Spring Web, which allows you to initialize a Spring-based web application without configuring the dependencies that are required to run the application. Instead, it will automatically install the Apache Tomcat Web Server and Spring Security for authentication features.
- Create standalone applications: Spring Boot can run standalone applications that have no dependencies on external web servers. For example, we can embed servers such as Tomcat and run the application.
Differences between Spring and Spring Boot
So, what is the difference between Spring and Spring Boot? And do you need to learn about the Spring Framework before working with Spring Boot? Let’s start with the first question.
The following table shows the difference between the two frameworks:
The developers configure the dependencies for the project. |
Using Spring Starters, Spring Boot will configure all the dependencies that will be needed to run the application. |
Spring is a Java EE framework for building applications. |
Spring Boot is commonly used to build REST APIs. |
Spring simplifies the development of Java EE applications since modules such as Spring JDBC, Spring MVC, and Spring Security are already provided. |
Spring Boot provides the configuration for the dependencies, reducing the boilerplate code for the layouts of modules. This makes it easier to run the application. |
Dependency injection (DI) and inversion of control (IOC) are the main features of Spring for building applications. |
Spring Boot Actuator is a feature that exposes operational information about your apps, such as metrics and traffic. |
We can identify that Spring Boot is built on top of Spring and that the main difference is that Spring Boot automatically configures the dependencies we need to run a Spring application. So, to answer the question about needing to learn about the Spring Framework before working with Spring Boot, the answer is no – Spring Boot is just an extension of Spring, which makes configuring it faster because of its opinionated approach.
Now, let’s look at the dependencies we need in Spring and Spring Boot to configure a web application.
Dependency examples for Spring and Spring Boot
In Spring, the minimum dependencies that we need for our application to run are Spring Web and Spring Web MVC:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.3.5</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.5</version> </dependency>
Spring Boot only requires spring-boot-starter-web
, which is a Spring Starter for our application to run. The necessary dependencies are added automatically at build time as the starter will be responsible for the configuration:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.4.4</version> </dependency>
Another thing to consider in Spring is that we need to define some configurations, such as dispatcher servlets and mappings, for our application to run on the server:
public class SpringInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation("com.springexample"); container.addListener(new ContextLoaderListener(context)); ServletRegistration.Dynamic dispatcher = container. addServlet("dispatcher", new DispatcherServlet(context)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } }
After initializing the dispatcher servlets, we also need to use @EnableWebMvc
and have a Configuration
class with a @Configuration
annotation where we will instantiate a view resolver for the applications.
A new InternalResourceViewResolver()
instance will be created in the configuration class. This will be a bean for Spring. Here, all the files that are under the /WEB-INF/view
path with a .jsp
file extension will be resolved:
@EnableWebMvc @Configuration public class SpringWebConfig implements WebMvcConfigurer { @Bean public ViewResolver viewResolver() { InternalResourceViewResolver bean = new InternalResourceViewResolver(); bean.setViewClass(JstlView.class); bean.setPrefix("/WEB-INF/view/"); bean.setSuffix(".jsp"); return bean; } }
In Spring Boot, all these configurations will be omitted because this code is already included in the Spring Starters. We only need to define some properties for our application to run using the web starter:
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
After defining these properties, our application will run since all the necessary configurations, such as the Web Initializer and MVC Configuration, have been included.
With that, we have discussed the advantages of Spring Boot and, at the same time, the main differences between Spring Boot and the Spring Framework and how it reduces boilerplate code at configuration time.
As you may already know, the primary language of Spring is Java, and Java 17 has now been released. In the next section, we’ll learn about the new features in Java 17.