Hook: Interviewers love this question because Spring Boot is basically Spring with the repetitive setup stripped away.
Question: How does Spring Boot reduce boilerplate?
Answer: Spring Boot reduces boilerplate by giving you smart defaults, starter dependencies, and auto-configuration. Instead of manually wiring XML, servlet setup, and lots of beans, you add a few annotations and Boot fills in the common pieces for you. It also ships with an embedded server, so you can run the app directly without packaging a WAR for an external container.
Interview-Ready Answer: I’d say Spring Boot reduces boilerplate by doing three big things for me: it bundles compatible dependencies through starters, it auto-configures common beans based on what’s on the classpath, and it runs on an embedded server by default. So instead of writing a lot of XML and manual setup, I usually start with @SpringBootApplication, add a starter like spring-boot-starter-web, and let Boot create the standard web stack. I still override defaults when needed, but the common case becomes a few lines instead of a lot of wiring.
Boilerplate is the repetitive setup code you need before your app can do real work: dependency versions, servlet container wiring, controller scanning, JSON setup, database setup, and error handling. Spring Boot removes a lot of that repetition by assuming the most common choices first, then letting you override them when necessary.
spring-boot-starter-web is a curated dependency bundle. It pulls in Spring MVC, Jackson for JSON, validation support, and an embedded server choice, so you do not have to list each library by hand.@SpringBootApplication turns on the main bootstrapping features. This annotation combines @Configuration (a class that defines beans), @EnableAutoConfiguration (Boot should create common beans for you), and @ComponentScan (look for annotated classes like @RestController and @Service).DataSource bean?” and then decides what to create.8080. You do not need to install or configure a separate application server just to test locally. That removes the old “build WAR, deploy to Tomcat, restart container” loop.server.port, database URLs, and log levels can live in application.properties or application.yml. That means fewer environment-specific classes and fewer if (dev) style conditionals.In plain Spring, you often declare many beans yourself, wire them together manually, and manage dependency versions more directly. Boot trades a little startup inspection for a lot less human-written setup. That startup inspection is not usually the bottleneck; it is a one-time cost, roughly proportional to the number of auto-configuration classes and checks on the classpath, while request handling remains mostly the same as regular Spring MVC after startup.
| Area | Plain Spring | Spring Boot |
|---|---|---|
| Dependencies | Choose each library | Use starters |
| Versioning | Manage many versions | Managed for you |
| Server | External container | Embedded by default |
| Configuration | More manual wiring | Auto-config plus overrides |
| Startup path | More setup code | Shorter main path |
Use Spring Boot for most new Spring applications, especially APIs, microservices, and internal tools. It is ideal when you want fast startup, a standard production setup, and less configuration overhead. If you are in a very specialized environment where every bean and server setting must be hand-tuned, you can still override Boot defaults or disable parts of auto-configuration.
@ComponentScan only scans the package of the main application class and its subpackages by default. Put the main class too deep, and some controllers may not be found.Think of Spring Boot as a restaurant with a set menu: you get the common meal fast, and you only customize what you really need. Plain Spring is the kitchen where you order every ingredient separately.
Real-World Story: A checkout service at an e-commerce company needs a fast REST API for pricing, coupons, and payment confirmation. With Spring Boot, the team adds a web starter, creates a controller, and ships the service on the embedded server instead of spending days wiring servlet config, JSON converters, and container deployment files.
One production bug showed how Boot’s “magic” can still bite you: the main application class was placed in a package that did not sit above all the controllers. The app started successfully, but the checkout endpoints returned 404 Not Found because component scanning never found the controllers. Logs showed no request mappings for the missing routes, and the load balancer health checks began failing. The fix was simple: move the main class to the root package or widen scanBasePackages.
The lesson is that Boot removes repetitive setup, but you still need to understand the rules that replace that setup: package scanning, auto-configuration conditions, and overriding defaults correctly.
package com.example.demo;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.server.ResponseStatusException;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
// Boot lets us hook in startup work without XML or container setup.
// This keeps the example runnable while showing how little wiring is needed.
@Bean
CommandLineRunner startupBanner(@Value("${server.port:8080}") String port) {
return args -> System.out.println("Spring Boot started on port " + port);
}
}
@RestController
class GreetingController {
@GetMapping("/greet")
public String greet(@RequestParam(required = false) String name) {
// Edge case: blank input should fail clearly instead of producing a weird greeting.
if (name == null || name.isBlank()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Query parameter 'name' is required");
}
return "Hello, " + name.trim() + "!";
}
@GetMapping("/divide")
public int divide(@RequestParam int a, @RequestParam int b) {
// Another failure path: show that business validation belongs close to the request.
if (b == 0) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Parameter 'b' must not be 0");
}
return a / b;
}
}
@RestControllerAdvice
class ApiExceptionHandler {
@ExceptionHandler(ResponseStatusException.class)
public ResponseEntity<Map<String, String>> handle(ResponseStatusException ex) {
// Centralized error mapping removes repeated try/catch code from controllers.
return ResponseEntity
.status(ex.getStatusCode())
.body(Map.of(
"error", ex.getReason() == null ? "Request failed" : ex.getReason()
));
}
}
Follow-up & Tricky Questions:
@SpringBootApplication and @Configuration? @Configuration only marks a bean configuration class. @SpringBootApplication includes that plus component scanning and auto-configuration, so it is the usual entry point for Boot apps.server.port. Boot is designed to back off when it sees your explicit configuration.Common Mistakes:
Memory Hook: “Boot brings the kitchen pre-stocked.” You do not buy every pot and ingredient yourself; Boot gives you the common tools, sets the stove to a sensible default, and lets you swap ingredients only when needed.
Cheat Sheet:
@SpringBootApplication = configuration + auto-config + component scan.8080 by default.Practice Tasks:
spring-boot-starter-web and run it locally.