SpringCreating spring boot application

Creating spring boot application

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.

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.

  1. By use of IDE
  2. By SpringCLI Tool
  3. 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

  1. What kind of project you want (Maven or Gradle) to generate
  2. What is your preferred language (Apart from Java you will get an option for Kotlin and Groovy)
  3. Version of Spring boot
  4. Standard project group and descriptions of artifacts.
  5. Dependencies
Spring Initializer

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.

  1. @Configuration as a bean definition source.
  2. @EnableAutoConfiguration This gives Spring Boot an idea of how you want to configure your application.
  3. @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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe Today

GET EXCLUSIVE FULL ACCESS TO PREMIUM CONTENT

[tds_leads input_placeholder="Your email address" btn_horiz_align="content-horiz-center" pp_msg="SSd2ZSUyMHJlYWQlMjBhbmQlMjBhY2NlcHQlMjB0aGUlMjAlM0NhJTIwaHJlZiUzRCUyMiUyMyUyMiUzRVByaXZhY3klMjBQb2xpY3klM0MlMkZhJTNFLg==" pp_checkbox="yes" tdc_css="eyJhbGwiOnsibWFyZ2luLXRvcCI6IjMwIiwibWFyZ2luLWJvdHRvbSI6IjMwIiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tdG9wIjoiMjAiLCJtYXJnaW4tYm90dG9tIjoiMjAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" display="column" gap="eyJhbGwiOiIyMCIsInBvcnRyYWl0IjoiMTAifQ==" f_msg_font_family="702" f_input_font_family="702" f_btn_font_family="702" f_pp_font_family="789" f_pp_font_size="eyJhbGwiOiIxNCIsInBvcnRyYWl0IjoiMTIifQ==" f_btn_font_spacing="1" f_btn_font_weight="600" f_btn_font_size="eyJhbGwiOiIxNiIsImxhbmRzY2FwZSI6IjE0IiwicG9ydHJhaXQiOiIxMyJ9" f_btn_font_transform="uppercase" btn_text="Subscribe Today" btn_bg="#000000" btn_padd="eyJhbGwiOiIxOCIsImxhbmRzY2FwZSI6IjE0IiwicG9ydHJhaXQiOiIxNCJ9" input_padd="eyJhbGwiOiIxNSIsImxhbmRzY2FwZSI6IjEyIiwicG9ydHJhaXQiOiIxMCJ9" pp_check_color_a="#000000" f_pp_font_weight="500" pp_check_square="#000000" msg_composer="" pp_check_color="rgba(0,0,0,0.56)"]

Get unlimited access to our EXCLUSIVE Content and our archive of subscriber stories.

Latest article

Exit mobile version