Updated July 5, 2023
Introduction to Spring Boot Application
The spring framework brings to you a module known as Spring Boot Application which enables programmers to efficiently build an application with minimalistic programming efforts. Also, we would look into annotations and use them in our explanation so that the intuition behind the usage is clearer. Also, for readers, just to reiterate, the dependencies are mentioned in pom.xml so as to extract the required tools for building the application.
The next few sections will be immensely critical in building your base on what is known to be the most widely used tool for microservices application building. In this article, we will dig deeper into the application of Spring Boot and take a ride into different aspects of how to build one using a very simple example.
Examples of Spring Boot Application
We use Spring Boot in our industry to build applications in a fast way. In layman language what Spring boot does is that it will look at your classpath and if some configuration is missing it will make reasonable assumptions to add the corresponding configurations. The classpath is nothing but a path which tells Spring where does the third-party and user-defined classes which are not a part of Java platform are residing. This will enable a programmer to focus more on feature development rather than infrastructure.
Example:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-starter-security</artifactId>
</dependency>
For example, if the Tomcat server is missing in your configuration, Spring will assume that Tomcat is not required explicitly and add embedded Jetty Server. Now, in this article, we would try to create a very simple web application and look at different aspects so as to get the intuition clear.
Example:
package com.educba.spring.web.controller
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class SpringBootWelcomeController {
@RequestMapping("/home" , method = RequestMethod.GET)
public String home() {
return "Welcome to our Spring Boot article on EduCBA!";
}
}
So, in the above example, the package name is mentioned wherein the entire package structure is followed on where this particular code resides. We explicitly import RestController and RequestMapping as we would be using annotations corresponding to that in the rest of the code. Next, we see that the class SpringBootWelcomeController is annotated to be @RestController which means that this class is mentioned as a class that can handle web requests to the Spring MVC (model-view-controller). The next annotation we have is @RequestMapping which essentially maps our /home page to home() function/method. This means that when a webpage is invoked with /home, this method will be executed and finally return the text “Welcome to our Spring Boot article on EduCBA!”
The reason of URL being localhost:8080 is that the code above is run locally at port 8080.
Why Spring Boot?
Now, it brings more curiosity about why we use Spring Boot. For programmers, as we mentioned that one can focus on feature development rather than wasting time on infrastructure development. For quite a long time Spring Boot has been quite popular because of the following reasons:
- The way Spring code is structured, it encourages writing code that can be tested along the way even locally due to the injection approach of Spring.
- The way the MVC framework is integrated in Spring makes it the state-of-the-art technology in web application development.
- One can easily integrate with other Java Frameworks like Hibernate, Struts, etc.
- One can utilize the capability which Spring brings in the form of a transaction management capability.
- Not only, Spring Boot, one can harness the power of other Spring sister projects like:
- Spring Data: Used for simplifying relational and NoSQL data access.
- Spring Social: One can use the power of Spring social to integrate with social networking sites like Facebook, Twitter, etc.
- Spring Batch: Using this one can leverage the batch processing framework technology Spring provides.
- Spring Security: This has state-of-the-art security features to provide robust security for securing applications.
- Spring Boot removes a lot of boilerplate coding.
- Spring Boot offers a lot of plug and play option for developers. It is mainly intended for doing the basic things automatically like a magic show but enhances the code to fit the technical needs in a way to best fit the business needs. It is like a mix of one size fits all and enhancement to the size.
- One can utilize the in-built production-ready features like monitoring health checks, metrics, externalized configuration.
- Use embedded Tomcat or Jetty server and remove of WAR files deployment.
Conclusion
How the spring boot application works with the help of a simple example. It looks like a magic show that by using some annotations we can build an application without thinking about the infrastructure development behind it. It looks like a robot sitting behind an annotation and performs not only the required task but assumes things for us in case we deliberately or by mistakenly forgot to mention about the configuration. Just use some starter dependencies, annotate useful terms and let the magic begin from there. It is a very useful tool for standardizing the collection of tools and makes your programming life simple and painless.
Recommended Articles
This is a guide to Spring Boot Application. Here we discuss the basic concept and brief examples of spring boot application respectively. You may also look at the following articles to learn more –