By WebNest Studio Engineering Team
Spring Boot Tutorial
Spring Boot Project Setup
Spring Boot is an opinionated framework built on top of the Spring Framework that removes most of the manual configuration Spring traditionally required. Instead of wiring beans by hand and configuring an application server, you get a production-ready application by adding a handful of dependencies and letting Spring Boot's auto-configuration do the rest.
The fastest way to start a new project is Spring Initializr (start.spring.io), a web tool (also built into most IDEs) that generates a ready-to-run project skeleton. You choose the build tool (Maven or Gradle), the language (Java, Kotlin, or Groovy), the Spring Boot version, and the dependencies (starters) you need, and it produces a downloadable zip with the correct folder structure and build file already configured.
Standard Project Structure
A generated Spring Boot project follows a conventional Maven/Gradle layout that the framework expects by default.
src/main/java— your application source code, rooted in a base package (e.g. com.webnest.app).src/main/resources— application.properties or application.yml, static assets, and templates.src/test/java— test classes, mirroring the main package structure.pom.xml(Maven) orbuild.gradle(Gradle) — declares the Spring Boot parent/plugin and dependencies.
The Main Application Class and @SpringBootApplication
Every Spring Boot app has exactly one entry-point class annotated with @SpringBootApplication, which is itself a convenience annotation combining three separate annotations: @Configuration (marks the class as a source of bean definitions), @EnableAutoConfiguration (triggers Spring Boot's auto-configuration mechanism), and @ComponentScan (scans the current package and sub-packages for components, controllers, services, and repositories). This is why placing your entry class at the root package matters — anything outside that package tree won't be scanned by default.
Running the Application
The main method calls SpringApplication.run(YourAppClass.class, args), which bootstraps the Spring ApplicationContext, starts the embedded server (Tomcat by default for web apps), and keeps the JVM alive to serve requests. You can run the app from your IDE, with mvn spring-boot:run, or as a standalone executable jar produced by mvn package.
Examples
Minimal Spring Boot entry point generated by Initializr
package com.webnest.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebnestAppApplication {
public static void main(String[] args) {
SpringApplication.run(WebnestAppApplication.class, args);
}
}
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _
( ( )\___ | '_ | '_| | '_ \/ _` |
\\/ ___)| |_)| | | | | || (_| |
' |____| .__|_| |_|_| |_\__, |
=========|_|==============|___/=
:: Spring Boot :: (v4.1.1)
... Tomcat started on port 8080 (http) with context path '/'
... Started WebnestAppApplication in 1.842 seconds
Hello World (console): print a message when the application starts
package com.webnest.app;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class, args);
}
// A CommandLineRunner bean runs once, right after Spring Boot has started
@Bean
CommandLineRunner sayHello() {
return args -> System.out.println("Hello, World from Spring Boot!");
}
}
// Run it from the project folder:
// Maven: mvn spring-boot:run (Windows: mvnw.cmd spring-boot:run)
// Gradle: ./gradlew bootRun (Windows: gradlew.bat bootRun)
:: Spring Boot :: (v4.1.1)
... Starting HelloWorldApplication using Java 21
... Started HelloWorldApplication in 0.912 seconds
Hello, World from Spring Boot!
Hello World (web): return the message from a REST endpoint
<!-- pom.xml: add the web starter (named spring-boot-starter-web before Boot 4) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc</artifactId>
</dependency>
// src/main/java/com/webnest/app/HelloController.java
package com.webnest.app;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(@RequestParam(defaultValue = "World") String name) {
return "Hello, " + name + "!";
}
}
Open http://localhost:8080/hello in a browser, or use curl:
curl http://localhost:8080/hello
Hello, World!
curl "http://localhost:8080/hello?name=Webnest"
Hello, Webnest!
Common Mistakes
- Placing controllers or services in a package outside (or a sibling of) the package containing the @SpringBootApplication class — component scanning will silently skip them.
- Deleting or renaming the auto-generated main application class without understanding that it is the single source of auto-configuration and component scanning.
- Manually configuring beans that Spring Boot already auto-configures, leading to duplicate bean definition conflicts.
Exercise
Using the lesson's Minimal Spring Boot entry point generated by Initializr example, create a small implementation or design sketch that demonstrates Spring Initializr generates a ready-to-run project with the correct structure and build file for your chosen dependencies.. Add a test case or scenario that would catch Placing controllers or services in a package outside (or a sibling of) the package containing the @SpringBootApplication class — component scanning will silently skip them., and describe the expected result.
Show hints
- Start with Standard Project Structure and compare your approach with Minimal Spring Boot entry point generated by Initializr. Identify the decision or behavior the lesson recommends.
- Walk through your scenario step by step. Confirm that it follows Spring Initializr generates a ready-to-run project with the correct structure and build file for your chosen dependencies. and that it would catch the failure described by Placing controllers or services in a package outside (or a sibling of) the package containing the @SpringBootApplication class — component scanning will silently skip them..
Quick quiz
1. Which choice is a mistake to avoid in Spring Boot Project Setup?
- A. Spring Initializr generates a ready-to-run project with the correct structure and build file for your chosen dependencies.
- B. Placing controllers or services in a package outside (or a sibling of) the package containing the @SpringBootApplication class — component scanning will silently skip them.
- C. Deleting or renaming the auto-generated main application class without understanding that it is the single source of auto-configuration and component scanning.
Show answer
B. Avoid this approach: Placing controllers or services in a package outside (or a sibling of) the package containing the @SpringBootApplication class — component scanning will silently skip them. The lesson recommends: Spring Initializr generates a ready-to-run project with the correct structure and build file for your chosen dependencies.
Interview questions
How would you apply the guidance in Standard Project Structure to a real task, and what failure would you guard against?
Show answer
Spring Initializr generates a ready-to-run project with the correct structure and build file for your chosen dependencies. Apply this in Standard Project Structure; the example Minimal Spring Boot entry point generated by Initializr shows one way to do it. Avoid Placing controllers or services in a package outside (or a sibling of) the package containing the @SpringBootApplication class — component scanning will silently skip them. and explain how your implementation or design prevents that failure.
Key Points to Remember
- Spring Initializr generates a ready-to-run project with the correct structure and build file for your chosen dependencies.
- @SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan.
- The entry class should sit at the root of your package tree so component scanning covers everything beneath it.
- SpringApplication.run() bootstraps the ApplicationContext and starts the embedded server in one call.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs plain Java 17. Frameworks such as Spring, and databases, are not available here. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
These examples need a JDK project with their libraries on your computer, so they can be copied but not run here. Codelab runs Python, FastAPI backends with SQLite, plain Java 17 and HTML/CSS/JavaScript.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering