Nearby lessons
5 of 10Spring Boot - Project Structure
- Understand Spring Boot - Project Structure
- See working code examples
- Learn from common mistakes and Q&A
Learn Spring Boot - Project Structure step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.
a)Download and install STS in our System
--------------------------------------------------------
a)Open "https://spring.io/tools" in Browser.
b)Click on "Spring Tool Suite for Eclipse "Windows 64 bit".
c)Get "spring-tool-suite-4-4.4.0.RELEASE-e4.13.0-win32.win32.x86_64.zip" from downloads
and unzip at C drive.
d)Find "sts-4.4.0.RELEASE" folder at C driver and open "sts-4.4.0.RELEASE" and double click
on "SpringToolSuite4" Application.
e)Provide workspace "D:\springbootpractice" .
b)Create Spring Starter project
-----------------------------------------
a)Select "File"--> "New"--> "Spring Starter Project".
b)Provide the following details.
a)Name: helloapp
b)Group : com.durgasoft
c)Artifact: helloapp
d)Description: HelloApp for Spring Boot
e)package: com.durgasoft
c)Click on "Next" button.
d)Select the required modules like "web" --"Spring Web".
e)Click on "Next".
f)Click on "Finish" button.
If we do the above steps then we will get SpringBoot project at Project Explorer part.
c)Open HelloApplication.java file which is existed at
"src\main\java\com.durgasoft.HelloApplication.java" and write sop(-).
EX: HelloApplication.java:
-----------------------------------
package com.durgasoft;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloappApplication {
public static void main(String[] args) {
SpringApplication.run(HelloappApplication.class, args);
System.out.println("HelloApplication from Spring");
}
}
d)Run "HelloApplication" by clicking on "Run" button then we will get Output.
| . ____ _ | __ _ _ |
|---|
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.9.RELEASE)
2019-10-07 10:43:43.483 INFO 16632 --- [ main] com.durgasoft.HelloappApplication
: Starting HelloappApplication on DESKTOP-LTICM7V with PID 16632
(D:\spring_boot_practice\sts\helloapp\target\classes started by Nagoor in
D:\spring_boot_practice\sts\helloapp)
2019-10-07 10:43:43.486 INFO 16632 --- [ main] com.durgasoft.HelloappApplication
: No active profile set, falling back to default profiles: default
2019-10-07 10:43:44.444 INFO 16632 --- [ main]
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 2222 (http)
2019-10-07 10:43:44.475 INFO 16632 --- [ main]
o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-10-07 10:43:44.475 INFO 16632 --- [ main]
org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.26]
2019-10-07 10:43:44.603 INFO 16632 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] :
Initializing Spring embedded WebApplicationContext
2019-10-07 10:43:44.603 INFO 16632 --- [ main] o.s.web.context.ContextLoader
: Root WebApplicationContext: initialization completed in 1065 ms
2019-10-07 10:43:44.796 INFO 16632 --- [ main]
o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService
'applicationTaskExecutor'
2019-10-07 10:43:44.925 INFO 16632 --- [ main]
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 2222 (http) with
context path ''
2019-10-07 10:43:44.928 INFO 16632 --- [ main] com.durgasoft.HelloappApplication
: Started HelloappApplication in 1.831 seconds (JVM running for 2.894)
HelloApplication from Spring
Note: If 8080 port is busy in our system then change server port number by using key-value
pair "server.port=2222" in application.properties which is available under
"src\main\resources\application.properties".
e)If we want to execute through Web Client then prepare controller class with handler
method.
EX: HelloController.java
---------------------------------
package com.durgasoft.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String sayHello() {
return "Hello User!";
}
}
f)Execute HelloApplication appliocation and open browser and use the following URL at
address bar.
http://localhost:2222/hello
- Spring Boot Initializr:
------------------------------
-->Spring Boot initializr is a web tool , it will provide option to generate Spring boot projects
directly as per the provided project details.
-->Spring Boot Initializr will give an option to download the project directly from web and
to execute through Eclipse IDE or some other.
Steps:
-------
- Create Spring Boot Project and Download project:
- Import Spring boot Application in STS:
- Create Spring Boot Project and Download project:
---------------------------------------------------
a)Open "https://start.spring.io" URL at address bar in browser.
b)Provide the following details
a)project: Maven Project
b)Language : JAVA
c)Spring Boot: 2.1.9
d)Project Metadata
group: com.durgasoft
artifact: helloapp
Options:
Name: helloapp
Description: Helloapp for Spring Boot
Packaging : jar
JAVA: 8
Dependencies: web --> Spring Web
c)Click on "Generate" button.
If we do the above step then helloapp.zip file is generated and downloaded.
- Import Spring boot Application in STS:
------------------------------------------------------
a)Copy helloapp.zip" file to "D:\mywebapps" location from downloads.
b)Unzip "helloapp.zip" file.
c)Open STS and import the project as existed Maven project
d)Open "com.durgasoft.HelloappApplication.java" from src\main\java and do required
modifications.
EX: HelloappApplication.java
------------------------------
package com.durgasoft;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloappApplication {
public static void main(String[] args) {
SpringApplication.run(HelloappApplication.class, args);
}
}
Note: If 8080 port is busy in our system then change port number from 8080 to some other
by providing "server.port" in application.properties file which is existed in
"src\Main\resources\application.properties".
e)Execute HelloappApplication.java file by clicking on "Run" button.
Output:
| . ____ _ | __ _ _ |
|---|
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.9.RELEASE)
2019-10-07 11:37:04.769 INFO 3492 --- [ main] com.durgasoft.HelloappApplication
: Starting HelloappApplication on DESKTOP-LTICM7V with PID 3492
(D:\mywebapps\helloapp\target\classes started by Nagoor in D:\mywebapps\helloapp)
2019-10-07 11:37:04.771 INFO 3492 --- [ main] com.durgasoft.HelloappApplication
: No active profile set, falling back to default profiles: default
2019-10-07 11:37:05.744 INFO 3492 --- [ main]
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 5555 (http)
2019-10-07 11:37:05.769 INFO 3492 --- [ main]
o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-10-07 11:37:05.769 INFO 3492 --- [ main]
org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.26]
2019-10-07 11:37:05.876 INFO 3492 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] :
Initializing Spring embedded WebApplicationContext
2019-10-07 11:37:05.876 INFO 3492 --- [ main] o.s.web.context.ContextLoader
: Root WebApplicationContext: initialization completed in 1063 ms
2019-10-07 11:37:06.098 INFO 3492 --- [ main]
o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService
'applicationTaskExecutor'
2019-10-07 11:37:06.279 INFO 3492 --- [ main]
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 5555 (http) with
context path ''
2019-10-07 11:37:06.283 INFO 3492 --- [ main] com.durgasoft.HelloappApplication
: Started HelloappApplication in 1.89 seconds (JVM running for 2.8)
HelloApp from Spring Boot initializer
e)If we want to prepare Spring Boot application through Web Client then prepare
Controller class with handler method and execute that:
EX: HelloController.java
-------------------------
package com.durgasoft.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String sayhello() {
return "<h1>Hello User!</h1>";
}
}
To run this , first we have to execute "HelloappApplication" then open browser and use the
following URL.
http://localhost:5555/hello
- Spring Boot CLI[Command Line Interface]:
-------------------------------------------------------------
--> Spring Boot CLI(Command Line Interface) is a Spring Boot software to run and test
Spring Boot applications from command prompt. When we run Spring Boot applications
using CLI, then it internally uses Spring Boot Starter and Spring Boot AutoConfigurate
components to resolve all dependencies and execute the application
--> Spring Boot internally contains Groovy and Grape (JAR Dependency Manager) to add
Spring Boot Defaults and resolve all dependencies automatically.
--> To use Spring Boot CLI we have to use the following steps.
- Download and Install Spring Boot CLI.
- Prepare Groovy Script Code.
- Run Groovy Script code through Spring Boot CLI.
- Download and Install Spring Boot CLI:
-----------------------------------------
a)Open the link in Browser address bar "https://docs.spring.io/spring-
boot/docs/current/reference/html/getting-started-installing- spring-boot.html#getting-
started-installing-the-cli". or else search for it in Google.
b)click on "spring-boot-cli-2.1.9.RELEASE-bin.zip" link under Manual Installation.
c)Copy the downloaded ""spring-boot-cli-2.1.9.RELEASE-bin.zip"" in C drive and extract it
then we will find "spring-2.1.9.RELEASE" folder in C Driver.
d)Set "path" environment variable to "C:\spring-2.1.9.RELEASE\bin" location.
set path=%path%;C:\spring-2.1.9.RELEASE\bin;
e)Check whether Spring command is running or not in Command prompt.
- Key ideas of Spring Boot - Project Structure explained simply
- Ready-to-use code examples
- Exam-style questions at the end