📖 Scenario: You are a developer starting a new Java web project. You want to understand why using Spring Boot can make your work easier compared to using plain Spring Framework.
🎯 Goal: Learn the key differences and benefits of Spring Boot over plain Spring by creating a simple Spring Boot application setup step-by-step.
📋 What You'll Learn
Create a basic Spring Boot application class
Add a configuration property for server port
Use Spring Boot's auto-configuration feature
Add a simple REST controller to complete the app
💡 Why This Matters
🌍 Real World
Spring Boot is widely used to quickly build production-ready web applications and microservices with minimal setup.
💼 Career
Understanding Spring Boot is essential for Java developers working in enterprise environments, as it speeds up development and reduces boilerplate code.
Progress0 / 4 steps
1
Create the Spring Boot application class
Create a Java class called Application annotated with @SpringBootApplication and add a main method that runs SpringApplication.run(Application.class, args).
Spring Boot
Hint
This class is the entry point of your Spring Boot app. The @SpringBootApplication annotation enables auto-configuration.
2
Add a configuration property for server port
Create a file called application.properties in src/main/resources and set the property server.port=8081 to configure the server port.
Spring Boot
Hint
This property changes the default server port from 8080 to 8081, showing how easy configuration is with Spring Boot.
3
Use Spring Boot's auto-configuration feature
Explain in a comment how @SpringBootApplication enables auto-configuration, so you don't need to write XML or manual setup for common features like embedded server and component scanning.
Spring Boot
Hint
Auto-configuration saves time and reduces errors by setting up defaults for you.
4
Add a simple REST controller to complete the app
Create a class called HelloController annotated with @RestController. Add a method hello() annotated with @GetMapping("/hello") that returns the string "Hello, Spring Boot!".
Spring Boot
Hint
This controller shows how easy it is to create REST endpoints with Spring Boot.
Practice
(1/5)
1. What is a main advantage of using Spring Boot over plain Spring Framework?
easy
A. It requires manual XML configuration for all components.
B. It does not support dependency injection.
C. It provides automatic configuration and embedded servers to simplify setup.
D. It only works with legacy Java versions.
Solution
Step 1: Understand Spring Boot features
Spring Boot offers automatic configuration and embedded servers to reduce manual setup.
Step 2: Compare with plain Spring
Plain Spring requires manual XML or Java config and separate server setup.
Final Answer:
It provides automatic configuration and embedded servers to simplify setup. -> Option C
Quick Check:
Spring Boot simplifies setup = B [OK]
Hint: Spring Boot automates config and server setup [OK]
Common Mistakes:
Thinking Spring Boot needs manual XML config
Believing Spring Boot lacks dependency injection
Assuming Spring Boot only supports old Java versions
2. Which of the following is the correct way to start a Spring Boot application?
easy
A. public void start() { Spring.run(App.class); }
B. public static void main(String[] args) { SpringApplication.run(App.class, args); }
C. public static void main(String[] args) { Application.run(App.class); }
D. public static void main(String[] args) { BootApplication.run(App.class); }
Solution
Step 1: Recall Spring Boot startup method
Spring Boot apps start with SpringApplication.run() inside main().
Step 2: Check options for correct syntax
Only public static void main(String[] args) { SpringApplication.run(App.class, args); } uses correct method and signature.
Final Answer:
public static void main(String[] args) { SpringApplication.run(App.class, args); } -> Option B
Quick Check:
Spring Boot starts with SpringApplication.run() = A [OK]
Hint: Spring Boot apps start with SpringApplication.run() [OK]
Common Mistakes:
Using incorrect method names like Application.run()
Missing static main method signature
Confusing Spring Boot with other frameworks
3. Given this Spring Boot application snippet, what will happen when you run it?
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApp {
public static void main(String[] args) {
SpringApplication.run(DemoApp.class, args);
}
}
medium
A. The application starts with an embedded server and default configuration.
B. The application throws a compilation error due to missing annotations.
C. The application requires manual server setup before running.
D. The application runs but does not start any server.
Solution
Step 1: Identify @SpringBootApplication role
This annotation enables auto-configuration and component scanning.
Step 2: Understand SpringApplication.run behavior
It starts the app with embedded server and default settings.
Final Answer:
The application starts with an embedded server and default configuration. -> Option A
Quick Check:
@SpringBootApplication + SpringApplication.run = embedded server start [OK]
Hint: @SpringBootApplication triggers auto-config and embedded server [OK]
Common Mistakes:
Thinking manual server setup is needed
Assuming missing annotations cause errors here
Believing app runs without server
4. You wrote a Spring Boot app but it fails to start with this error: ClassNotFoundException: org.springframework.boot.SpringApplication. What is the likely cause?
medium
A. You did not annotate your main class with @SpringBootApplication.
B. You used the wrong main method signature.
C. You are running the app with an unsupported Java version.
D. You forgot to add Spring Boot starter dependencies in your build file.
Solution
Step 1: Analyze the error message
ClassNotFoundException means the class is missing from the project dependencies.
Step 2: Identify missing dependency cause
SpringApplication class is part of spring-boot package, so missing starter dependencies cause this.
Final Answer:
You forgot to add Spring Boot starter dependencies in your build file. -> Option D
Quick Check:
Missing Spring Boot dependencies = ClassNotFoundException [OK]
Hint: ClassNotFoundException usually means missing dependencies [OK]