Welcome to the world of Spring Boot! In previous lectures we covered the fundamental concepts of the Spring Framework, learned what Spring Boot is and how it makes life easier for Java developers. Today we'll dive into the practical side and take our first step in creating and running a Spring Boot application. Ready? Then let's go — time for our first lines of code and some hot server launches!
The project structure in Spring Boot isn't just a random pile of directories. It's designed to organize your code so it's easy to manage both small apps and huge enterprise systems.
Knowing the project structure helps you:
- Find the files and settings you need faster.
- Understand where things live (even after years working on the project).
- Prepare the project for scaling (so adding another feature won't be scary).
Spring Boot project structure
When you create a Spring Boot project (for example, via Spring Initializr), you get a basic structure automatically. Something like this:
my-spring-boot-app/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/example/demo/
│ │ │ ├── MySpringBootApplication.java // Main application class
│ │ │ └── controllers/
│ │ │ └── HelloController.java // Our controllers
│ │ ├── resources/
│ │ ├── application.properties // Application configurations
│ │ └── static/ // Static resources (CSS, JS, etc.)
│ └── test/ // Application tests
├── pom.xml (or build.gradle) // Maven/Gradle dependency configuration
Let's break down the main parts:
src/main/java/
All your business logic lives here. For example, controllers, services, repositories, and other "smart" classes.src/main/resources/
This is where configs live (for example,application.properties), resources (like static files), and templates for rendering pages (if you use Thymeleaf).src/test/
Directory for tests. We won't poke around here yet, but later this will become our best friend.pom.xml or build.gradle
The file that manages your project's dependencies. You add the necessary dependencies, likespring-boot-starter-web, here.
Main application class
The main application class is the entry point when you start a Spring Boot project. Usually it's located under src/main/java and contains just a few lines. Here's an example:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
}
What's happening here?
@SpringBootApplication: this annotation combines three others (@Configuration,@EnableAutoConfiguration,@ComponentScan) and tells Spring Boot: "Hey, this is our main class. Start here!".SpringApplication.run(): kicks off the "Spring Boot magic". It finds all configurations, starts the embedded server, and begins listening for incoming requests.
Where should our code live? Ideally the main class is right at the root of your package, e.g. com.example.demo. That makes automatic component scanning easy. If you hide the main class deeper, you might need to configure component scanning manually (spoiler: don't do that unless you really have to).
The role of application.properties
If the main class is the heart of your app, then application.properties (or its YAML cousin application.yml) is the brain. Here you define configs like the port, database settings, logging, and more.
Example application.properties:
# Server port
server.port=8081
# Logging
logging.level.org.springframework=DEBUG
logging.level.com.example=TRACE
Run the app and it'll listen on port 8081 instead of the default 8080! If you prefer the YAML version, here's how it looks:
server:
port: 8081
logging:
level:
org.springframework: DEBUG
com.example: TRACE
Running the application
Spring Boot makes running apps super comfy. Here's how to do it:
Run from your IDE
- Open your project in IntelliJ IDEA (or any other IDE).
- Find the main application class.
- Click the green "Run" button next to the
mainmethod.
- Run from the terminal. Execute one of these commands:
or
./mvnw spring-boot:run./gradlew bootRun - Build a jar. Package a jar for your app:
./mvnw clean packageThen run the jar:
java -jar target/my-app-0.0.1-SNAPSHOT.jar
Verify it's running
Now open your browser and go to:
http://localhost:8080/hello
If you see your message "Hello, World", then everything's working as expected! Hooray!
Hands-on
Let's build a minimal app that returns a greeting. We'll use a controller to handle the request.
Check out the controller code:
package com.example.demo.controllers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, World from Spring Boot!";
}
}
What's going on here?
@RestController: tells Spring that this class will handle HTTP requests.@GetMapping: means the method handles HTTP GET requests at/hello.- String response: we return a simple text greeting to the client.
Check the response
Open your browser or Postman, request http://localhost:8080/hello and enjoy your first Spring Boot controller!
Common startup errors
Spring Boot does a lot for you, but sometimes things go wrong. Here are a few typical issues and how to fix them:
- Port already in use
If you seePort 8080 is already in useon startup, someone else already grabbed the port. Try changing the port inapplication.properties:server.port=8081 - Wrong package structure
If the main application class (the one annotated with@SpringBootApplication) isn't at the root of your package, component scanning may fail. - Autoconfiguration error
If you forgot to add a needed starter (for example,spring-boot-starter-web), your app won't be able to handle requests. Check yourpom.xmlorbuild.gradle.
At this point you're ready to build and run Spring Boot apps. In the next lecture we'll talk about the bean lifecycle and learn how Spring manages our objects from the inside. Who said magic is hard? 😉
GO TO FULL VERSION