Spring

Introduction to Spring Boot

Introduction:

In this tutorial, we’ll take a look at Spring Boot and see how it’s different from the Spring framework. We’ll also discuss various features offered by Spring Boot.

What is the Spring Boot?

Spring is a powerful framework when it comes to developing enterprise-level applications. It provides us with features like dependency injection along with supporting many other framework extensions.

Still, there’s a lot of configurations and boilerplate code involved. For instance, for every Spring MVC application, we’ll be configuring the DispatcherServlet, InternalResourceViewResolver, and a lot of other components.

Spring Boot is an extension over the Spring framework and enables us to quickly bootstrap a production-ready Spring application.

It is extremely powerful and offers a wide range of features.

Spring Initializr:

We can use the Spring Initializr website to quickly download a Spring Boot application template with needed dependencies:

Spring Initializr

In the dependencies section, we can add the Spring Boot starter dependencies we need for our application. For instance, we can search and include the Spring Web Starter if we are developing a web application.

All Spring Boot applications have a parent section defined in their POM file:

The parent POM manages the version of the child dependencies all at once. And so, we can omit version tags for each child dependency. It also defines default configurations for several plugins.

Spring Boot Starter Dependencies:

Some of the most popular and common starter packages provided by Spring Boot are:

  • spring-boot-starter: the core starter which includes auto-configuration support, logging, YAML support, etc.
  • spring-boot-starter-test: includes support for testing frameworks like Junit, Spring Test, Mockito, Hamcrest
  • spring-boot-starter-web: provides support for Spring MVC; helpful in RESTful web development
  • spring-boot-starter-jpa: supports Spring Data JPA and Hibernate
  • spring-boot-starter-security: has dependencies for Spring Security

Spring Boot CLI:

Instead of using the Spring Initializr, we can also generate our application using the Spring Boot CLI:

It will generate a project in the current directory with Spring Data JPA Starter and Spring Web Starter dependencies.

Spring Boot Auto-Configuration:

With the auto-configuration feature, Spring Boot scans all jars present in our classpath and auto-configures a lot of things for us. It is an extremely helpful feature and significantly reduces the development effort.

We can either use a @EnableAutoConfiguration or a @SpringBootApplication annotation to enable the auto-configuration. The @SpringBootApplication annotation groups @ComponentScan, @SpringBootConfiguration and @EnableAutoConfiguration altogether.

The main class of our Spring Boot application looks similar to:

We can generate an auto-configuration report to see what all got auto-configured for our Spring Boot application by either updating our app.properties:

Or simply running our application with the –debug switch.

Embedded Server:

Spring Boot provides an embedded server and so applications are standalone and pretty easy to run.

On mvn install, a runnable jar gets generated in our target folder. All we need to do is to execute that jar for our application to be up and running:

Tomcat is the default server for Spring Boot applications, but we can change this configuration.

Spring Boot Properties:

There are a lot of ready to use Spring Boot properties available that we can directly add to our application.properties. For instance, to exclude auto-configuration for a few classes, we’ll have:

We can read the Spring Boot documentation to explore the available options.

Spring Boot Profiles:

For any enterprise application, we usually have various deployment environments: development, UAT, and Production. Each of these environments will most likely have some environment-specific configurations.

Spring Boot adds a few additional features to the Spring profile configurations. We can define the active profile in our application.properties:

Also, we can have application-specific property files: applications-{profile}.properties.

Conclusion:

In this tutorial, we explored the various features of Spring Boot and how it helps us in RAPID application development.

Be the First to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *