How to Build Your First Spring Boot App the Right Way
For Java developers new to Spring Boot · Based on Mosh Spring Boot Architecture Skill
// TL;DR
If you know Java OOP and basic SQL but are new to Spring Boot, this methodology walks you from environment setup to a running, well-architected app. You'll install the JDK and IntelliJ Ultimate, generate a project with the built-in generator, validate the structure, add starter dependencies correctly, and wire beans using constructor injection. It teaches the reasoning behind each decision — why interfaces decouple your code, why the parent POM manages versions, and why constructor injection fails loudly instead of silently — so you build good habits from your very first project.
What do I need to know before starting Spring Boot?
Before writing a single line of Spring Boot code, confirm two prerequisites: solid Java OOP knowledge (classes, methods, and especially interfaces) and familiarity with relational databases and basic SQL. This methodology flags these explicitly because gaps cause confusion downstream. Dependency injection and programming against interfaces assume you're comfortable with abstraction, and the data layer assumes you can read SQL. If either is shaky, close the gap first — don't treat prerequisites as optional.
How do I set up my environment and generate a project?
Install the latest stable JDK and verify it with `java -version`. Use IntelliJ IDEA Ultimate, not Community Edition — Community lacks the built-in Spring Boot generator and the tools you'll rely on constantly. Inside IntelliJ, use the built-in generator (not start.spring.io, to avoid context switching). Set Language=Java, Build Tool=Maven, Spring Boot=latest stable (never a snapshot), a reverse-domain Group ID like `com.codewithmosh`, and your project name as the Artifact. Enable Git at this step and leave dependency selection for later.
Then validate the generated structure before touching code: `.idea/` (IntelliJ config, never edit), `.mvn/` and `mvnw`/`mvnw.cmd` (the Maven Wrapper), `pom.xml` (the heart of the project), `src/main/java` (your code), `src/main/resources/application.properties` (config), and `src/test/java` (tests). Your entry point is the class containing `SpringApplication.run()`.
How do I add dependencies and wire beans correctly?
Use IntelliJ's dependency search (Cmd+N → Dependency) instead of hand-editing pom.xml, then click the Maven sync icon to download. The single most important beginner habit: delete the `
When one class needs another, don't instantiate it with `new`. Instead, extract an interface (Refactor → Extract → Interface), annotate only the concrete class with `@Service`, `@Repository`, `@Controller`, or `@Component`, and inject the interface through a constructor. If your class has exactly one constructor, you don't even need `@Autowired` — Spring infers it. This is called constructor injection, and it's the default because a missing dependency crashes the app loudly at startup rather than silently with a NullPointerException later.
Why does this approach matter for a beginner?
Because the habits you form now stick. Field injection looks convenient but hides dependencies and breaks testing. Setter injection for required dependencies leaves fields null. Annotating an interface produces no bean at all. This methodology steers you away from all of these from day one, so your first app is already decoupled and testable. It also teaches restraint — don't extract an interface where there's only ever one implementation, or you'll create an over-engineered mess. Apply the Open Closed Principle with common sense.
Once your beans are wired, add a `@Controller` with `@RequestMapping("/")`, return a view name, drop static files in `src/main/resources/static/`, and run with Ctrl+R or `./mvnw spring-boot:run`. Visit `http://localhost:8080` and you have a working, professionally structured Spring Boot app.
Next step: Generate a fresh project in IntelliJ Ultimate, validate every folder against the checklist above, then wire your first service using constructor injection before adding any business logic.
// FREQUENTLY ASKED QUESTIONS
Do I really need IntelliJ Ultimate as a beginner?
Yes, for this methodology. Community Edition lacks the built-in Spring Boot generator and dependency search tools that make development productive. Ultimate has Spring Boot project generation, built-in Maven, and smart completion. If cost is a concern, note that JetBrains offers free Ultimate licenses to students and early-access programs — but Community Edition is explicitly insufficient here.
What if I don't know SQL yet?
Close that gap before proceeding. This methodology flags missing SQL and relational database familiarity as a genuine blocker, not an optional extra. The data layer assumes you can read and write basic SQL, and jumping ahead causes confusion. Spend a short time on SQL fundamentals first, then return to Spring Boot with a solid foundation.
Why can't I just use new to create my objects?
Manually creating objects with new tightly couples classes and bypasses Spring's IoC container, which manages creation, wiring, and lifecycle for you. By handing control to Spring and injecting dependencies through constructors, you get decoupled, testable code where implementations can be swapped without editing dependent classes — the whole point of the framework.