05 - Running Your First Spring Boot Application
- 01 - What is Spring? What Problems Does It Solve?
- 02 - Spring vs Spring Boot: Understanding the Difference
- 03 - Spring MVC vs Spring Boot: When and Why to Use Each
- 04 - Installing Java, Maven, & IDE Setup (STS, Eclipse, IntelliJ)
- 05 - Running Your First Spring Boot ApplicationCurrent
- 06 - Inversion of Control (IoC) in Spring
- 07 - Dependency Injection (DI) in Spring
- 08 - BeanFactory vs ApplicationContext
- 09 - Spring Bean Lifecycle
- 10 - Bean Scopes: Singleton, Prototype & Custom Scopes
You have installed Java, Maven, and your IDE. Now it's time to run your first Spring Boot application.
Spring Boot makes this incredibly simple because it provides:
- An embedded server
- Auto-configuration
- Zero setup requirements
In this post, you'll learn what happens when a Spring Boot application starts and how to verify it's working properly.
1. Create a Spring Boot Project
To run your first Spring Boot application, start by generating a project using Spring Initializr.
Open:
https://start.spring.io
Select:
- Project: Maven
- Language: Java
- Spring Boot Version: 3.x
- Dependencies: Spring Web
Click Generate, then extract the project and open it in your IDE.
2. Understanding the Project Structure
A fresh Spring Boot project looks like this:
src/
├── main/
│ ├── java/
│ │ └── com.example.demo/
│ │ └── DemoApplication.java
│ └── resources/
│ ├── application.properties
│ ├── static/
│ └── templates/
└── test/
└── java/
Let's break it down:
-
DemoApplication.java
The main class that starts the Spring Boot application. -
application.properties
Where configuration goes (port, DB settings, etc.) -
static/
Stores static files like CSS, JS, images. -
templates/
Stores server-side view templates (Thymeleaf, FreeMarker).
3. The Main Class Explained
Open the main class:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
What does @SpringBootApplication do?
It's a combination of:
@Configuration@EnableAutoConfiguration@ComponentScan
This tells Spring Boot to:
- Configure the app automatically
- Scan for components
- Load beans into the IoC container
4. Run the Spring Boot Application
You can run the project in two ways:
4.1 Run from IDE
In IntelliJ / STS / Eclipse, click the green Run button next to the main class.
You should see logs like:
Tomcat started on port 8080
Started DemoApplication in 2.345 seconds
This means the application successfully started.
4.2 Run from Terminal
Navigate to your project folder and run:
mvn spring-boot:run
Or build a jar file:
mvn clean package
java -jar target/demo-0.0.1-SNAPSHOT.jar
5. Test the Application
Spring Boot starts an embedded server on port 8080 by default.
Open in your browser:
http://localhost:8080
You should see a generic Whitelabel Error Page.
This is expected because you haven't created any endpoints yet.
6. Create Your First REST Endpoint
Let's add a simple REST Controller.
Create a file:
src/main/java/com/example/demo/HelloController.java
Paste this code:
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Spring Boot!";
}
}
Restart the application and visit:
http://localhost:8080/hello
You should see:
Hello, Spring Boot!
Congratulations — you've successfully created and run your first Spring Boot application.
7. What Happens Behind the Scenes?
When the application starts:
- Spring Boot loads auto-configuration
- An embedded Tomcat server starts
- Beans are scanned and initialized
- Routes are mapped
- Application logs are printed to the console
All without configuring a single XML file.
8. Common Issues & Fixes
Problem: java: command not found
Java is not added to PATH.
Fix: Update environment variables.
Problem: Maven build failure
Run:
mvn -version
Ensure Maven is installed and PATH is correctly set.
Problem: Port already in use
Change the port in:
application.properties
server.port=9090
What's Next?
In the next post:
06 - Inversion of Control (IoC) in Spring
We'll explore the core concept that powers all of Spring and Spring Boot.
