Spring Boot Core and Dependency Injection Interview Questions
15 questions on Spring Boot basics: auto-configuration, starters, dependency injection, bean scopes, profiles, and Actuator.
1. What is Spring Boot, and how is it different from the Spring Framework?
The Spring Framework provides the core container (dependency injection), web, data, and security building blocks, but needs a lot of configuration. Spring Boot sits on top and removes most of that setup: opinionated defaults, auto-configuration, starter dependencies, an embedded server, and production features such as Actuator. You can run a Boot application with a plain main method and no external server.
2. What does @SpringBootApplication do?
It combines three annotations: @Configuration (the class can declare beans), @EnableAutoConfiguration (configure beans based on the classpath and properties), and @ComponentScan (find components in the package and its sub-packages).
@SpringBootApplication public class ShopApplication { public static void main(String[] args) { SpringApplication.run(ShopApplication.class, args); } }3. How does auto-configuration work?
Boot reads a list of auto-configuration classes from the libraries on the classpath. Each one is guarded by conditions such as @ConditionalOnClass, @ConditionalOnMissingBean, and @ConditionalOnProperty, so a bean is created only if the needed class is present and you have not defined your own. That is why adding spring-boot-starter-data-jpa and a database driver gives you a working DataSource. Run with --debug to see the conditions report.
4. What are starters?
Starters are curated dependency descriptors, such as spring-boot-starter-web, -data-jpa, -security, -validation, and -test. Adding one pulls in a compatible set of libraries with managed versions, so you do not pick each dependency and version yourself.
5. What is dependency injection, and why prefer constructor injection?
Instead of creating collaborators itself, a class receives them from the Spring container, which makes code loosely coupled and testable. Constructor injection is preferred because dependencies are required and visible, fields can be final, the class cannot be created in an invalid state, and it is easy to instantiate in unit tests without Spring. With a single constructor, @Autowired is optional.
@Service public class OrderService { private final OrderRepository repository; public OrderService(OrderRepository repository) { this.repository = repository; } }6. What is the difference between @Component, @Service, @Repository, and @Controller?
All are stereotypes of @Component and register a bean found by component scanning. They communicate intent: @Service for business logic, @Repository for data access (and it also enables translation of persistence exceptions into Spring's DataAccessException), @Controller and @RestController for web layers.
7. What are bean scopes?
singleton (the default): one instance per container. prototype: a new instance each time it is requested. Web scopes: request, session, and application. Singleton beans should be stateless or thread safe, since many threads share them.
8. What is the difference between @Bean and @Component?
@Component marks your own class for component scanning. @Bean is placed on a method inside a @Configuration class and lets you create and configure a bean explicitly, which is how you register classes from third-party libraries that you cannot annotate.
9. What happens if two beans match an injection point?
Spring throws NoUniqueBeanDefinitionException. Resolve it with @Primary on the preferred bean, @Qualifier at the injection point, or by naming the parameter after the bean. You can also inject all implementations as a List or Map.
10. What is the bean lifecycle?
Spring instantiates the bean, injects dependencies, applies BeanPostProcessors, calls initialization callbacks (@PostConstruct, InitializingBean, or the init-method), and the bean is ready. When the context closes, destruction callbacks (@PreDestroy or DisposableBean) run.
11. How do application.properties and application.yml work, and what is the order of precedence?
Externalized configuration lives in these files and can be overridden by profile-specific files, environment variables, system properties, and command-line arguments, which win over files. Environment variables follow relaxed binding, so SPRING_DATASOURCE_URL maps to spring.datasource.url. Read values with @Value or type-safe @ConfigurationProperties.
12. What are Spring profiles?
Profiles group configuration and beans for an environment such as dev, test, or prod. Activate them with spring.profiles.active, and use application-dev.yml files or @Profile on beans. Keep secrets out of files that go into source control.
13. What is @ConfigurationProperties?
It binds a group of properties to a typed class, with validation and IDE support, instead of scattering @Value annotations.
@ConfigurationProperties(prefix = "app.mail") public record MailProperties(String host, int port, boolean tls) {}14. What is Spring Boot Actuator?
A module that exposes production-ready endpoints for health, metrics, environment, loggers, and more, typically under /actuator. Only health is exposed over HTTP by default; expose others deliberately with management.endpoints.web.exposure.include and secure them, because they can leak sensitive information. It integrates with Micrometer for metrics.
15. What embedded servers does Spring Boot support, and how do you package an application?
Tomcat is the default, with Jetty and Undertow as alternatives. An application is packaged as an executable JAR (java -jar app.jar) containing the embedded server, which also makes containerizing it straightforward. You can still build a WAR for deployment to an external server if required.
Interview Tips
- Explain auto-configuration with a concrete example, such as how a DataSource appears.
- Prefer constructor injection in your own code examples.
- Mention that Boot 3 and newer need Java 17 or later and use Jakarta packages instead of javax.