In this article, we’re going to discuss Developing an Application with Spring Boot. We’ll discuss various facets of Spring Boot along with various options for creating a Spring Boot application.
This Articles Contents
1. Introduction
Spring Boot is an opinion, a configuration convention. Spring Boot strips out much of the project set up by taking an opinionated perspective of the Spring Platform so that new and current users can easily get to the parts they need. Spring Boot makes it easier to build a Spring-powered enterprise application with minimal fuss.
2. Spring Boot Features
Spring Boot offers the following function out of the box.
- It simplifies spring dependencies by taking an opinion view (we will discuss it in more detail).
- Spring Boot offers a pre-configured range of technologies / framework to minimize error-prone configuration so that we, as a developer, concentrate on constructing our business logic rather than worrying about project setup.
- You don’t really need the large XML setups for your project.
- Undertow directly, Jetty or Embed Tomcat
Creating Spring Boot Project
One of the biggest difficulties of beginning a new project is the actual set-up of the project. We need to make a decision on the various directory structure, and we also need to make sure that we meet all industry standards. If you’re using Maven, you may even be using Maven Startup Artifact to help us do those initial setups faster.
Spring Initializr is another excellent tool to launch Spring Boot projects easily. Spring Initializr is a software application that creates a Spring Boot project. Keep in mind that it will only generate a project structure and not a code for you based on your preference (Maven or Gradle). If you’re beginning your project, my advice is to begin with Spring Initializr.
There are many ways for you to use Spring Boot Initializr to build a project structure.
- By use of IDE
- By SpringCLI Tool
- By Spring Initializr(recommened)
By Spring Initializr
This is the best way to create a project structure for your application. Open your browser’s Spring Initializr Web interface and you will be presented with a wizard to start your configuration.
It requires you to fill information in the web interface to start with
- What kind of project you want (Maven or Gradle) to generate
- What is your preferred language (Apart from Java you will get an option for Kotlin and Groovy)
- Version of Spring boot
- Standard project group and descriptions of artifacts.
- Dependencies
By clicking on generate download the project and import in favorite IDE.
package com.onurdesk.first;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FirstApplication {
public static void main(String[] args) {
SpringApplication.run(FirstApplication.class, args);
}
}
4.1 @SpringBootApplication Annotation
Our main class uses the @SpringBootApplication annotation. @SpringBootApplication is similar to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default values. If you start your project, an annotation is recommended. Using @SpringBootApplication in the key class is similar to following three annotations.
- @Configuration as a bean definition source.
- @EnableAutoConfiguration This gives Spring Boot an idea of how you want to configure your application.
- @ComponentScan will quickly pick up all Spring parts, including the @Configuration classes.
4.2 Main Method
The main method is another important aspect of our main class. This is a standard approach that meets the standard Java workflow. Our main class can shift control to the Spring Boot SpringApplication class. The SpringApplication Class run method will be used for the BootStrap application. We’ll take a deeper look at the SpringApplication section later.
4.3 FirstController
package com.onurdesk.first;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FirstController {
@RequestMapping("/")
public String fristController(){
return "Welcome to my First controller!!";
}
}
There’s nothing special about this in our Controller. It’s a standard Spring-MVC controller with a standard Spring MVC annotation.
5 Running Application
It’s time to launch our first Spring Boot driven application. We have a variety of ways to run our Spring Boot application.
⦁ If you use IDE, you can use IDE to run your application.
⦁ We may use the mvn spring-boot: run command in the root directory to launch our first Spring Boot application.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.4.RELEASE)
2020-10-12 22:21:04.551 INFO 19616 --- [ main] com.onurdesk.first.FirstApplication : Starting FirstApplication on onurdesk with PID 19616 (D:\LearningNewTech\grey\Spring boot\first\target\classes started by mingle in D:\LearningNewTech\grey\Spring boot\first)
2020-10-12 22:21:04.558 INFO 19616 --- [ main] com.onurdesk.first.FirstApplication : No active profile set, falling back to default profiles: default
2020-10-12 22:21:07.910 INFO 19616 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-10-12 22:21:07.944 INFO 19616 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-10-12 22:21:07.944 INFO 19616 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.38]
2020-10-12 22:21:08.205 INFO 19616 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-10-12 22:21:08.206 INFO 19616 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3542 ms
2020-10-12 22:21:08.605 INFO 19616 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-10-12 22:21:08.918 INFO 19616 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-10-12 22:21:08.936 INFO 19616 --- [ main] com.onurdesk.first.FirstApplication : Started FirstApplication in 5.045 seconds (JVM running for 5.92)
Open the browser of your choice and type http://localhost:8080, see “Welcome to my First controller!!” as an output.
Summary
Spring Boot offers a good boost to Spring-based applications. In this article, we heard about the various choices for constructing and applying Spring Boot. Setting up a new project is always a challenging task and we need to manage all the dependencies, but with Spring Boot, it was easy and we were able to run our first web application with only new lines of code without thinking about the required dependencies or deployment.