Practice: how Spring Boot works
A review of this module. Skip it if you feel confident; the next module does not need it.
Fix the mistakes
The slips that cost marks in this module. Spot each one, then check the fix.
Startup problems rarely arrive one at a time or with a label. This review mixes the four causes of the module, starters, auto-configuration, scanning and the web server, the way real bugs arrive.
Startup failures: read the report before the stack trace
A FeeReport needs a FeeFormatter, and the app refuses to start.
Example 1 · A missing @Component: FeeFormatter.java, FeeReport.java
Runs in: the boot-lab practice project (Spring Boot 4.1)
// src/main/java/com/shoplite/lab/FeeFormatter.javapackage com.shoplite.lab;
import java.math.BigDecimal;
// @Component is missingpublic class FeeFormatter {
public String format(BigDecimal amount) { return "Rs " + amount.toPlainString(); }}
// src/main/java/com/shoplite/lab/FeeReport.javapackage com.shoplite.lab;
import java.math.BigDecimal;
import org.springframework.boot.CommandLineRunner;import org.springframework.stereotype.Component;
@Componentpublic class FeeReport implements CommandLineRunner {
private final FeeFormatter formatter;
public FeeReport(FeeFormatter formatter) { this.formatter = formatter; }
@Override public void run(String... args) { System.out.println("Hostel fee: " + formatter.format(new BigDecimal("42500.00"))); }}*************************** APPLICATION FAILED TO START *************************** Description: Parameter 0 of constructor in com.shoplite.lab.FeeReport required a bean of type 'com.shoplite.lab.FeeFormatter' that could not be found. Action: Consider defining a bean of type 'com.shoplite.lab.FeeFormatter' in your configuration.
What this code does
- The same message as the sibling-package case in the Component scanning lesson, but here the package is fine (
com.shoplite.lab, the main class's package). The cause is the missing annotation. - Fix: add
@Componentand its import toFeeFormatter. The app then starts and printsHostel fee: Rs 42500.00. - Check order for every "could not be found": annotation first, package second, then conditions or profiles.
Read a startup failure from the bottom of the log up: the APPLICATION FAILED TO START block if there is one, otherwise the last Caused by:. The four failures of this module, by their first words: "required a bean of type ... that could not be found" (annotation or package), "Failed to configure a DataSource" (JDBC starter without a URL), "Port 8080 was already in use" (another process), "Failed to bind properties under ..." (a bad setting value). A fifth, 'url' must start with "jdbc", has no report: it is an unset environment variable.
Fix one cause, start again, and read the new log: a second problem can hide behind the first.
Predicting the beans auto-configuration creates
Adding the actuator starter and calling /actuator/health, with no controller written.
Example 2 · Adding a starter adds beans: the actuator
Runs in: the boot-lab practice project (Spring Boot 4.1)
# pom.xml now also has spring-boot-starter-actuator (and validation)java -jar target/boot-lab-0.0.1-SNAPSHOT.jarcurl -i http://localhost:8080/actuator/health... o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint beneath base path '/actuator'
... com.shoplite.lab.BootLabApplication : Started BootLabApplication in 6.676 seconds (process running for 7.832)
HTTP/1.1 200
Content-Type: application/vnd.spring-boot.actuator.v3+json
Transfer-Encoding: chunked
Date: Fri, 16 Oct 2026 06:50:01 GMT
{"groups":["liveness","readiness"],"status":"UP"}What this code does
- You wrote no controller for
/actuator/health. The actuator starter put its classes on the class path, its auto-configurations matched, and they added the endpoint and a new log line. - Only
healthis exposed over HTTP by default, hence "Exposing 1 endpoint". The Configuration, Profiles & Logging module decides what ShopLite exposes. - There is no
dbpart in the health response because boot-lab has no DataSource. Add the JDBC starter with a URL and Boot adds a database health check too, again by auto-configuration.
Ask two questions for each Boot feature: is its library on the class path (@ConditionalOnClass), and have I defined that bean myself (@ConditionalOnMissingBean)? Some beans add a third: exactly one bean of a type exists (@ConditionalOnSingleCandidate), or a property has a value (@ConditionalOnProperty).
The starter decides the first answer: web MVC brings Tomcat, DispatcherServlet and Jackson's jacksonJsonMapper; JDBC brings dataSource, jdbcTemplate, jdbcClient and transactionManager; actuator brings endpoints such as /actuator/health. Your own beans decide the second. When a prediction and reality differ, --debug settles it: search the conditions report for the bean or configuration name.
Scanning and port problems, side by side
A request to /api/ping that returns 404 although the app started without any error.
Example 3 · Two kinds of 404
Runs in: the boot-lab practice project (Spring Boot 4.1)
# PingController (@GetMapping("/api/ping")) is in com.shoplite.web,# the main class in com.shoplite.labcurl -i http://localhost:8080/api/pingHTTP/1.1 404
Content-Type: application/json
Transfer-Encoding: chunked
Date: Fri, 16 Oct 2026 06:50:01 GMT
{"timestamp":"2026-10-16T06:50:01.040Z","status":404,"error":"Not Found","path":"/api/ping"}What this code does
- A JSON 404 comes from Spring: the request reached the app, but no controller is mapped to
/api/ping, becausecom.shoplite.webis outside the scan. The app started without any error. - An HTML 404 page ("HTTP Status 404 – Not Found") comes from Tomcat: the request did not even reach Spring, usually a wrong port or context path (Embedded Tomcat lesson).
- Fix here: move the controller to
com.shoplite.lab.web(or the main class up tocom.shoplite). Then the same curl printspong.
Scanning problems and port problems both look like "my endpoint does not work", but they show up differently. A class outside the scan is simply missing: the app starts, and its URLs give Spring's JSON 404, or startup fails if another bean needs it. A port problem stops startup ("Port 8080 was already in use") or sends your request somewhere else, which gives Tomcat's HTML 404 or no connection at all.
Two quick checks tell them apart. Read the "Tomcat started on port ... with context path" line and make sure your URL matches it. Then check the controller's package against the main class's package.
Interview drill: what happens when a Spring Boot app starts
What SpringApplication.run does, step by step. Say it in this order in an interview, one sentence per step.
// What SpringApplication.run(ShopliteApiApplication.class, args) does, step by step// 1. Reads settings: application.properties, environment variables, command-line args, profiles// 2. Creates the application context (a web one, because Spring MVC is on the class path)// 3. Component scanning: finds @Component/@Service/@RestController/@Configuration classes// in com.shoplite and below// 4. Auto-configuration: evaluates Boot's configuration classes and their conditions// (@ConditionalOnClass, @ConditionalOnMissingBean ...), adding beans that match// 5. Creates the singleton beans, injecting constructor dependencies// 6. Starts embedded Tomcat on server.port (8080) -> "Tomcat started on port 8080"// 7. Logs "Started ShopliteApiApplication in N seconds", then calls the runners// 8. Serves requests on Tomcat threads until stopped (graceful shutdown on Ctrl+C)Keep @SpringBootApplication and auto-configuration as two separate answers: the annotation switches things on; auto-configuration is the mechanism of conditional configuration classes. Mention one concrete example for each (the jacksonJsonMapper bean backing off, the DataSource failure), because examples show you have really used it.
Order of execution
Order the startup sequence
Put the steps of a Spring Boot start in the order they happen.
Finish up
Mark complete & continue
Next: Mini project: ShopLite startup checks