Project Structure
Understanding your project structure is like knowing the layout of your house. You need to know where everything is to find what you need. Spring Boot follows a standard Maven/Gradle structure that makes organizing your code a breeze.
Don't worry - it looks more complicated than it actually is. Once you understand the basic layout, you'll feel right at home.
src/main/java Folder
This is where all your Java code lives. It's like the kitchen in your house - where all the cooking happens. You'll put your main application class here, along with all your controllers, services, and repositories.
The package structure usually follows a reverse domain name pattern. If your company is example.com and your project is myapp, your base package might be com.example.myapp. This prevents naming conflicts with other libraries.
Here's what a typical package structure looks like:
com.example.myapp
├── MyApplication.java
├── controller
│ └── UserController.java
├── service
│ └── UserService.java
└── repository
└── UserRepository.java
Each package has a specific purpose. Controllers handle HTTP requests, services contain business logic, and repositories manage data access.
Try it Yourself →src/main/resources Folder
This folder contains all your non-code resources. It's like the pantry in your house - where you store ingredients and supplies. Configuration files, static content, and templates all live here.
The most important file here is application.properties (or application.yml). This is where you configure your application settings. It's like the control panel for your app.
You can configure things like server port, database connections, and logging levels. Spring Boot reads this file at startup and uses it to customize your application.
Application Properties
Application properties are like the settings on your phone. They control how your application behaves. Here's an example of what you might put in your application.properties file:
server.port=8081
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.hibernate.ddl-auto=update
logging.level.root=INFO
Notice how clean and simple that is? No XML configuration files, no complex setup. Just straightforward key-value pairs.
You can also use YAML format if you prefer. Just create application.yml instead. The choice is yours - Spring Boot supports both.
Package Structure
A good package structure is like a well-organized closet. Everything has its place, and you can find what you need quickly. Here are some best practices:
Keep related classes together. All controllers in one package, all services in another. This makes your codebase easy to navigate.
Don't create too many packages. Over-engineering your package structure is like having a separate drawer for each type of sock - unnecessary and hard to maintain.
Use meaningful names. Your package names should describe what's inside. "controller" is better than "handlers" or "endpoints". Keep it simple and consistent.