Develop Spring Boot applications

Approaches to prepare Spring Boot Applications

  1. Using Simple MAVEN project in Eclipse or Intellij Idea.
  2. Using STS
  3. Using the “Spring Boot Initializr” web tool.
  4. Spring Boot CLI[Command Line Interface]

Using Simple MAVEN project in Eclipse or Intellij Idea

  1. Download and Install Maven Software.
  2. Open Eclipse IDE and Create MAVEN project.
  3. Provide the following configurations in pom.xml
    • Change the Java version.
    • Provide Spring Boot starter or Spring boot Starter web dependency.  
  1. Prepare Spring Boot Application class.
    • Declare a class with the main() method.
    • Provide @SpringBootApplication annotation over the main class.
    • Inside the main() method access the SpringApplication.run() method in order to bootstrap the spring application.
  1. Write application logic in the main() method.
  2. Execute the Spring Boot application.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.durgasoft</groupId>
<artifactId>app01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>app01</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<maven.compiler.source>1.17</maven.compiler.source>
<maven.compiler.target>1.17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
</project>

App.java

package com.durgasoft.app01;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
public static void main( String[] args ){
SpringApplication.run(App.class, args);
System.out.println("Welcome To Spring Boot Application");
}
}

Output :

.   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.1.0)
2024-06-19T10:50:45.732+05:30 INFO 19324 --- [ main] com.durgasoft.app01.App : Starting App using Java 21.0.3 with PID 19324 (D:\SPRINGBOOT-10AM\app01\target\classes started by ADMIN in D:\SPRINGBOOT-10AM\app01)
2024-06-19T10:50:45.755+05:30 INFO 19324 --- [ main] com.durgasoft.app01.App : No active profile set, falling back to 1 default profile: "default"
2024-06-19T10:50:48.029+05:30 INFO 19324 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2024-06-19T10:50:48.068+05:30 INFO 19324 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2024-06-19T10:50:48.069+05:30 INFO 19324 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.8]
2024-06-19T10:50:48.417+05:30 INFO 19324 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2024-06-19T10:50:48.428+05:30 INFO 19324 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2401 ms
2024-06-19T10:50:49.569+05:30 INFO 19324 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2024-06-19T10:50:49.607+05:30 INFO 19324 --- [ main] com.durgasoft.app01.App : Started App in 4.997 seconds (process running for 6.71)

Welcome To Spring Boot Application

Using STS

STS (Spring Tool Suite) is the same as the Eclipse , it provides a simple layer to prepare  Spring based applications.

  1. Download and Install STS software.
  2. Create a Spring starter project.
  3. Prepare Spring Boot Main class.
  4. Execute Spring boot Application.

Download and Install STS software

  1. Download STS from the internet with the following link.

https://spring.io/tools

  1. Double Click on the “spring-tool-suite-4-4.23.1.RELEASE-e4.32.0-win32.win32.x86_64.self-extracting.jar”.
  2. If we double click on the above jar file then it will provide a folder “sts-4.23.1.RELEASE”, copy the folder under C drive.
  3. Go inside the “sts-4.23.1.RELEASE” folder and double click on the “Spring tool Suite4”.
  4. Provide workspace.

Create a Spring Boot starter project

  1. Select File —-> New —-> Spring Starter Project
  2. Provide the following details

Name: app03
Type: MAVEN
Packaging: jar
Java Version: 17
Language : Java
Group: com.durgasoft
Artifact: app03
Version: 0.0.1-SNAPSHOT
Description: Simple SpringBoot Project
Package: com.durgasoft.app03

  1. Click on the “Next” button.
  2. Provide the Dependencies like “Spring Web”.
  3. Click on the Next button.
  4. Click on the “Finish” button.

Prepare Spring Boot Main class

We will get the Spring Boot Main class automatically under the  “com.durgasoft.app03” package in the src/main/java folder.

Provide application logic inside the main() method. 

Execute Spring boot Application

Click on the Run button

App03Application.java

package com.durgasoft.app03;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App03Application {

public static void main(String[] args) {
SpringApplication.run(App03Application.class, args);
System.out.println("Welcome To Spring Boot Application...");
}
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.durgasoft</groupId>
<artifactId>app03</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>app03</name>
<description>Simple SpringBoot Project</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

Spring Boot Application with Spring boot Initializr

Spring boot Initilizr is a web tool, it will provide an option to generate a Spring Boot project from the web that we can open in the IDEs like Eclipse , Intellij Idea,… 

To  prepare a Spring boot project we have to use the following steps.

  1. Create a Spring Boot project and download the Spring Boot project from the web.

Project: MAVEN
Language : Java
Spring Boot Version: 3.3.1
Group: com.durgasoft
Artifact: app04
Name: app04
Description:   Simple Spring boot Project
Package Name: com.durgasoft.app04
Packaging: jar
Java : 17
Dependencies: Spring web

  •  Click on the Generate button.

If we click on the “Generate” button then the Spring Boot project will be downloaded as a zip file in the form of app04.zip.

2. Import the Spring boot project in the IDEs like Eclipse, IntelliJ Idea:

  • Copy the zip file from downloads to a particular location like D:\SBapps.
  • Unzip the app04.zip file , we will get the folder app04.
  • Open Eclipse IDEs and Right click on the project explorer.
  • Select import then again select import.
  • Goto Maven and select the “Existing MAVEN Project”.
  • Click on the Next button.
  • Browse the project app04 in the Root Directory and click on the select Folder.
  • Click on the Finish Button.

3. Provide the application Logic in the Spring Boot application.

4. Execute the Spring Boot application.

App04Application.java

package com.durgasoft.app04;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App04Application {

public static void main(String[] args) {
SpringApplication.run(App04Application.class, args);
System.out.println("Welcome To Spring Boot Application");
}
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.durgasoft</groupId>
<artifactId>app04</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>app04</name>
<description>Simple Spring boot project</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

Spring Boot Project with CLI

Spring Boot CLI is a Spring boot software to run and test the spring boot application from the command prompt.

When we run the Spring Boot application, Spring boot uses the CLI internally to run the Spring boot project.

Spring Boot CLI uses an internal component like GRAPE [jar Dependency Manager] to  resolve the dependencies.

Spring Boot CLI is mainly for groovy script execution.

Steps:

  1. Download and Install Spring Boot CLI.
  2. Provide Groovy script code.
  3. Run Groovy Script code through Spring boot CLI.

Download and Install Spring Boot CLI

  1. Download the Spring Boot CLI from the internet.

https://docs.spring.io/spring-boot/installing.html#getting-started.installing.cli

  1. Copy the “spring-boot-cli-3.3.1-bin.zip” file to C drive.
  2. Extract the “spring-boot-cli-3.3.1-bin.zip” file and find the folder “spring-3.3.1”.
  3. Set “C:\spring-3.3.1\bin” to the “path” environment variable.
  4.  Check the Spring command on the command prompt by using the following command.
C:\Users\ADMIN>spring --version
Spring CLI v3.3.1

Provide Groovy script code

HelloController.groovy

@RestConroller
public class HelloController{
@RequestMappg(“/hello”)
public String sayHello(){
System.out.println(“Welcome to Spring Boot CLI”);
return “Hello User, Welcome to Spring Boot Groovy Script”.
}
}

Run Groovy Script code through Spring boot CLI

spring run HelloController.groovy