Introduction
Before building real applications with Spring Boot, you must understand the basic project structure and the core annotations that drive Spring Boot.
This lesson explains each part of a new Spring Boot project and gives you simple examples of the most important annotations.
1. Spring Boot Project Structure Explained
When you generate a project from Spring Initializr, the structure looks like this:
src/
└── main/
├── java/
│ └── com/example/demo/
│ ├── DemoApplication.java
│ └── controllers/
│ └── HelloController.java
└── resources/
├── application.properties
├── static/
└── templates/
Here is what each section means:
src/main/java
This contains all your Java source code.
You typically organize it into packages such as:
- controllers
- services
- repositories
- config
- models
DemoApplication.java
This is the main class that starts your application.
It contains the main method and the @SpringBootApplication annotation.
src/main/resources
Everything here is related to configuration and static content.
It includes:
- application.properties (or application.yml)
- static/ (CSS, JS, images)
- templates/ (Thymeleaf or other server-rendered templates)
application.properties
This is where you put configuration values like:
- database connection
- server port
- logging settings
2. Key Spring Boot Annotations for Beginners
Spring Boot relies heavily on annotations.
Here are the most important ones you must understand early on.
@SpringBootApplication
This annotation is placed on your main class.
Example:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
It combines three annotations:
- @Configuration
- @EnableAutoConfiguration
- @ComponentScan
This means:
- Spring Boot will auto-configure your project
- It will scan your packages for components
- It marks this class as a configuration entry point
@RestController
Used to create a REST API controller.
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello Spring Boot!";
}
}
A RestController returns JSON or plain text responses.
@GetMapping, @PostMapping, @PutMapping, @DeleteMapping
These annotations define HTTP endpoints.
Example:
@GetMapping("/users")
public List<User> getUsers() { ... }
They map specific HTTP methods to Java methods.
@Autowired
Spring handles dependency injection automatically with this annotation.
Example:
@Autowired
UserService userService;
It tells Spring Boot to provide the instance of UserService without you creating it manually.
@Service
Marks a class as a business/service layer component.
@Service
public class UserService {
}
@Repository
Used for DAO/database interaction classes.
@Repository
public class UserRepository {
}
@Entity
Marks a class as a JPA database model.
@Entity
public class User {
@Id
private Long id;
}
3. How Spring Boot Knows What to Load
Spring Boot scans your project starting from the package where your main class sits.
For example, if your main class is:
com.example.demo
Spring Boot automatically scans:
com.example.demo.*
This is why the main class should always be in the root package of your project.
4. Summary
In this lesson, you learned:
- The structure of a Spring Boot project
- What each folder and file is used for
- The most important Spring Boot annotations
- How Spring Boot scans and loads components
You are now ready to build more complex applications because you understand the foundation.