Spring Boot fundamentals interview questions 15 questions in 6 chapters
- 01Core concepts6 questions
- 02Web and REST4 questions
- 03Configuration
- 04Data access
- 05Running and testing
- 06Sources
Core concepts
1.1 What is Spring Boot, and how is it different from the Spring Framework? Easy 2 min
The Spring Framework gives the core features: dependency injection, transactions, Spring MVC for web and data access support. Spring Boot sits on top and makes Spring easy to start and run: starters pull in matching dependencies, auto-configuration sets up beans based on what is on the classpath, an embedded server like Tomcat runs inside the app, and Actuator adds health and metrics. So Spring is the framework; Spring Boot is the opinionated way to build and run Spring apps with very little setup.
Read the full answer1.2 What are inversion of control and dependency injection? Easy 1 min
Inversion of control means my classes don't create the objects they depend on; the Spring container creates them and hands them in. Dependency injection is how that happens: the dependency arrives through the constructor, a setter or a field. The benefit is loose coupling. An OrderService depends on a PaymentClient interface, and in tests I can pass a fake one without changing the service.
Read the full answer1.3 What is a Spring bean, and how do @Component, @Service, @Repository and @Bean differ? Easy 2 min
A bean is an object that the Spring container creates, wires and manages. @Component marks a class so component scanning picks it up; @Service and @Repository are specialised @Components that state the layer, and @Repository also turns database exceptions into Spring's DataAccessException. @Bean is different: it goes on a method in a @Configuration class and returns an object I build myself, which is how I register third-party classes I can't annotate.
Read the full answer1.4 Constructor injection, setter injection or field injection: which do you use and why? Easy 1 min
I use constructor injection for required dependencies, and that is what the Spring docs recommend. The fields can be final, the object can never exist half-built, and in a unit test I just call the constructor with mocks. Setter injection is for optional dependencies. Field injection with @Autowired on a private field hides dependencies, can't be final, and needs reflection or a Spring context to test, so I avoid it. With one constructor, Spring uses it without @Autowired.
Read the full answer1.5 What does @SpringBootApplication do? Easy 1 min
It is a shortcut for three annotations. @SpringBootConfiguration marks the class as a configuration source, @EnableAutoConfiguration turns on Spring Boot's auto-configuration, and @ComponentScan scans the class's package and sub-packages for components. That is why the main class should sit in the root package: classes outside it are not picked up.
Read the full answer1.6 What are bean scopes, and what is the default? Easy 1 min
The default scope is singleton: one instance per Spring container, shared by everyone who injects it. Prototype creates a new instance every time it is requested. Web apps also have request and session scope, one instance per HTTP request or per user session. Because singletons are shared across threads, a singleton service must not keep per-request data in its fields.
Read the full answer
Web and REST
2.1 How do you build a simple REST endpoint in Spring Boot? Easy 2 min
I write a class with @RestController and map methods with @GetMapping, @PostMapping and so on. @PathVariable reads a part of the URL, @RequestParam reads a query parameter, and @RequestBody turns the JSON body into a Java object. Whatever the method returns is written back as JSON by Jackson. @RestController is @Controller plus @ResponseBody, so I don't need a view.
Read the full answer2.2 How do you return the right HTTP status code from a controller? Easy 1 min
By default a successful method returns 200. When I need a different status or headers, I return ResponseEntity, for example ResponseEntity.created with a Location header for 201, or noContent for 204. @ResponseStatus on a method sets a fixed status. For errors I throw an exception and map it to 404, 400 or 409 in a @RestControllerAdvice, so controllers stay clean.
Read the full answer2.3 How do you handle exceptions globally in a Spring Boot REST API? Easy 2 min
I put the error handling in one class annotated with @RestControllerAdvice, with an @ExceptionHandler method for each exception type. Each handler maps the exception to the right status and a consistent error body. Spring 6 supports ProblemDetail, the standard RFC 9457 error format, so clients always get type, title, status and detail. That keeps try-catch out of controllers and never leaks stack traces to clients.
Read the full answer2.4 How do you validate request input in Spring Boot? Easy 1 min
I add spring-boot-starter-validation, put Bean Validation annotations like @NotBlank, @Email, @Positive and @Size on the request DTO fields, and mark the controller parameter with @Valid. If validation fails, Spring throws MethodArgumentNotValidException and returns 400 before my code runs. I turn that into a clear error list in my @RestControllerAdvice. Business rules that need the database stay in the service.
Read the full answer
Configuration
3.1 How do application.properties, @Value and @ConfigurationProperties work, and how do profiles fit in? Easy 2 min
application.properties or application.yml holds settings like the port and database URL. I read a single value with @Value, but for a group of settings I bind them to a typed class with @ConfigurationProperties, which is safer and can be validated. Profiles such as dev and prod load extra files like application-prod.yml on top of the defaults. Environment variables and command-line arguments override the files, so the same JAR runs everywhere and secrets stay out of the code.
Read the full answer
Data access
4.1 What is Spring Data JPA, and what does a repository interface give you? Easy 2 min
JPA is the standard Java API for mapping objects to tables, and Hibernate is the usual implementation. Spring Data JPA removes the boilerplate: I declare an interface that extends JpaRepository, and Spring creates the implementation at runtime with save, findById, findAll, delete and paging. It can also derive queries from method names like findByEmail, and for anything complex I write @Query. I still have to watch the generated SQL, especially for N+1 queries.
Read the full answer4.2 What does @Transactional do? Easy 2 min
It runs the method inside a database transaction: Spring starts a transaction before the method, commits if it returns normally, and rolls back if it throws a RuntimeException or Error. By default a checked exception does not roll back unless I set rollbackFor. It works through a proxy, so it only applies to public calls coming from another bean; calling the method from inside the same class skips it. I put it on service methods, not controllers.
Read the full answer
Running and testing
5.1 What is Spring Boot Actuator used for? Easy 1 min
Actuator adds production endpoints to the app: health for load balancers and Kubernetes probes, metrics through Micrometer, info, environment, loggers and more. By default only the health endpoint is exposed over HTTP, so I choose which others to expose and protect them, because env or heapdump can leak sensitive data.
Read the full answer5.2 How do you test a Spring Boot application? Easy 1 min
Most tests are plain unit tests: I create the service with its constructor and pass mocks, with no Spring at all. For the web layer I use @WebMvcTest with MockMvc, which loads only the controllers and checks status codes, JSON and validation. @DataJpaTest tests repositories against a database. @SpringBootTest starts the whole context for a few end-to-end tests, ideally with Testcontainers for a real database.
Read the full answer
Sources
- Spring Boot reference: overview
- Spring Boot reference: Auto-configuration
- Spring Boot reference: Build systems and starters
- Spring Boot reference: Using the @SpringBootApplication annotation
- Spring Framework: Introduction to the IoC container and beans
- Spring Framework: Dependency injection
- Spring Framework: Classpath scanning and managed components
- Spring Framework: Using the @Bean annotation
- Spring Framework: Bean scopes
- Spring Framework: Mapping requests
- Spring Framework: @RequestBody
- Spring Framework: DispatcherServlet
- Spring Framework: ResponseEntity
- Spring Framework: Exceptions (@ExceptionHandler)
- Spring Framework: Controller advice
- Spring Framework: Error responses (RFC 9457)
- Spring Boot reference: Validation
- Spring Framework: MVC validation
- Spring Boot reference: Externalized configuration
- Spring Boot reference: Profiles
- Spring Data JPA: Core concepts
- Spring Data JPA: Defining query methods
- Spring Framework: Using @Transactional
- Spring Framework: Rolling back a declarative transaction
- Spring Boot reference: Actuator
- Spring Boot reference: Actuator endpoints
- Spring Boot reference: Testing Spring Boot applications