Core
Spring Basics Interview Questions and Answers 1. What is the Spring Framework, and what problem does it solve?
Answer: The Spring Framework is designed to simplify enterprise Java application development. It reduces repetitive boilerplate code and provides infrastructure modules such as transaction management, security, dependency injection, and data access, allowing developers to focus on business logic rather than infrastructure code.
- What is the difference between the Spring Framework and Spring Boot?
Answer:
Spring Framework
Provides the core features like IoC (Inversion of Control) and Dependency Injection. Traditionally required significant XML or Java-based configuration. Offers flexibility but requires more setup.
Spring Boot
Built on top of the Spring Framework. Eliminates most configuration through Auto-Configuration. Provides opinionated defaults. Includes embedded servers (Tomcat, Jetty, Undertow). Helps create standalone, production-ready applications quickly. 3. What is Inversion of Control (IoC)?
Answer: Inversion of Control (IoC) is a design principle where the responsibility of creating, configuring, and managing objects is transferred from the application code to the Spring IoC Container. Instead of the application creating objects manually, Spring manages their lifecycle.
- What is Dependency Injection (DI), and how does Spring perform it?
Answer: Dependency Injection (DI) is a specific implementation of IoC where Spring automatically provides the required dependencies to an object instead of the object creating them itself using the new keyword.
Spring performs dependency injection through:
Constructor Injection (Recommended) Setter Injection Field Injection
Example:
Instead of:
UserService service = new UserService();
Spring automatically injects the required dependency during runtime.
- What is a Spring Bean?
Answer: A Spring Bean is a Java object that is created, configured, managed, and destroyed by the Spring Container.
In simple terms:
Every object managed by Spring is called a Spring Bean.
- What is the Spring Container (ApplicationContext)?
Answer: The Spring Container is the core component of the Spring Framework.
Its responsibilities include:
Reading configuration metadata (Annotations, Java Config, XML) Creating Spring Beans Managing bean lifecycle Injecting dependencies between beans Maintaining all beans inside the ApplicationContext 7. How do you define or register a Spring Bean?
Answer: There are two common ways to register a Spring Bean:
- Using Stereotype Annotations
Annotate the class with:
@Component @Service @Repository @Controller
Example:
@Service public class UserService {
}
- Using @Bean
Declare a method inside a class annotated with @Configuration.
Example:
@Configuration public class AppConfig {
@Bean
public UserService userService() {
return new UserService();
}
}
- How does Component Scanning (@ComponentScan) work?
Answer: Spring Boot automatically scans the package containing the @SpringBootApplication class and all its sub-packages for components annotated with:
@Component @Service @Repository @Controller
These classes are automatically registered as Spring Beans.
Important Interview Point: If a component is located outside the package hierarchy of the main application class, Spring will not detect it unless you explicitly configure @ComponentScan.
- What happens if Spring cannot resolve a constructor dependency?
Answer: When Spring creates a bean using constructor injection, it attempts to resolve every constructor parameter from the ApplicationContext.
If a required dependency is not available as a Spring Bean, the application fails to start with a Bean Creation Exception or NoSuchBeanDefinitionException.
To resolve this issue, ensure the dependency is registered as a Spring Bean using annotations like @Component, @Service, or by defining it with @Bean.
Quick Interview Revision Question Short Answer What is Spring Framework? A framework that simplifies enterprise Java development. What is Spring Boot? An extension of Spring that provides auto-configuration and embedded servers. What is IoC? Spring controls object creation and lifecycle instead of the application. What is DI? Spring injects required dependencies automatically. What is a Spring Bean? Any object managed by the Spring Container. What is ApplicationContext? The Spring Container that manages beans and dependency injection. How are Beans created? Using @Component (or related stereotypes) or @Bean. What is Component Scan? It automatically detects and registers annotated classes as beans. What happens if a dependency is missing? Application startup fails with a bean creation or dependency resolution exception.