🎉 Special Offer !    Code: GET300OFF    Flat ₹300 OFF on every Java Course
Grab Deal 🚀

Spring Interview Questions - 1


1. Why Spring & Spring Boot are important framework in java
(as compared to Servlet, JSP & JSF) ?
  • 1. Modern Development Practices
    Spring promotes Dependency Injection (DI) and Aspect-Oriented Programming (AOP), enabling cleaner, more modular, and testable code.
    Servlet/JSP/JSF applications tend to have tightly coupled components, making the code harder to maintain and test (Hard-Coded Dependencies, Manual Dependency Management (Factory Pattern) etc).
  • 2. Lightweight and Scalable
    Spring applications are lightweight and scalable, thanks to dependency injection and non-blocking I/O support (via Spring WebFlux).
    Servlets and JSP can be resource-heavy and less efficient for building high-performance applications.
  • 3. Separation of Concerns and Testability
    Spring’s MVC pattern promotes better separation between the business logic, controller and view, improving maintainability.
    JSP/JSF mixes logic with presentation, leading to harder-to-maintain code.
  • 4. Rich Ecosystem for Enterprise Needs
    Spring offers additional frameworks like Spring Data, Spring Security, and Spring Cloud, which are essential for modern enterprise applications.
    Servlet/JSP/JSF lacks these advanced modules and requires more external tools or custom code for similar functionality.
  • 5. Easy Integration with Modern Tools and APIs
    Spring integrates seamlessly with modern databases, REST APIs, and third-party libraries.
    Building REST APIs with Servlets or JSP/JSF is more tedious, involving a lot of boilerplate code.
  • 6. Reduced Boilerplate Code with Spring Boot
    Spring Boot reduces configuration effort with auto-configuration, starter dependencies and embedded servers; speeding up the development.
    In contrast, Servlets and JSP require extensive manual setup, such as web.xml configurations and separate server setups.
  • 7. Microservices and Cloud-Native Applications
    Spring Boot is designed with microservices architecture in mind, making it easy to build cloud-native and distributed systems.
    Older technologies like JSP/JSF were not intended for cloud or microservices use cases and struggle with scalability.
2. What is IoC in Spring and explain its use?
  • IoC stands for Inversion of Control : IoC is a "Design Principle" or "Architectural Pattern" where, instead of our code controlling how objects (like services or components) are created and managed, this responsibility is handed over to a container (such as the Spring Framework).
  • Think of it like this: If we’re throwing a party, instead of managing everything ourself (like buying food, setting up decorations, etc.), we hire a party planner. The planner (the IoC container) takes care of all the details while we focus on enjoying the party.
  • Use of IoC :-
    1. Simplifies Code: We don’t have to worry about creating and managing objects manually. The IoC container handles that for us.
    2. Loose Coupling: It helps keep our code clean and modular. Different parts of our application can work independently, making it easier to change or test them without affecting other parts.
    3. Easier Testing: Since objects are managed by the container, we can easily swap out one object for another (like using a mock object for testing) without changing the code that uses it.
    4. Lifecycle Management: The container takes care of the lifecycle of objects (like when to create, initialize, or destroy them), so we don’t have to manage that complexity.
3. What is Spring Container and Its types ?
  • Spring Container is a specific implementation of the IoC "Design Principle".
  • We can call the Spring container the "Heart of Spring".
  • It takes the responsibility for:
    1. Creating objects (beans)
    2. Managing dependencies between them
    3. Handling the lifecycle of these objects

  • Spring Container works as below:-
    The container reads configuration files (XML, annotations, or Java-based config) to:
    1. Create beans (Java objects)
    2. Inject their dependencies
    3. Manage their lifecycle (like initialization and destruction)

  • There are 2 types of Spring Containers :-
    1. "BeanFactory" Container
      • Lightweight and basic IoC container.
      • Lazy Initialization: Beans are created when they are requested.
      • Suitable for simple applications where performance is crucial and resources are limited.
    2. "ApplicationContext" Container
      • A more advanced version of BeanFactory with extra features.
      • Eager Initialization: Beans are created during container startup.
      • Suitable for Enterprise-level applications that require more features, such as transactions, events, and AOP.
4. What are the responsibilities of Spring Container (or IoC Container)?
  • Below are some responsibilities of Spring Container (or IoC Container):-
    1. Bean Creation:
      It instantiates objects defined as beans in the configuration file or annotations.
    2. Dependency Injection:
      It resolves and injects the dependencies required by a bean.
      Example: Injecting OrderService into UserService.
    3. Bean Configuration Management:
      It reads metadata (XML, annotations, or Java config) to manage the properties and behavior of beans.
    4. Lifecycle Management:
      It controls the entire lifecycle of a bean (instantiation, initialization, and destruction).
    5. Scope Management:
      It manages different bean scopes like singleton, prototype, session, etc.
    6. Event Handling:
      It supports event-based communication between beans (only in ApplicationContext).
5. How to create Spring Container?
  • We can create Spring Container by multiple ways:-

    1. Using BeanFactory implementation i.e. XmlBeanFactory
      Used when the XML configuration is in the classpath.
      BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
    2. Using ApplicationContext implementation i.e. ClassPathXmlApplicationContext:
      Used when the XML configuration is in the classpath.
      ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    3. Using ApplicationContext implementation i.e. FileSystemXmlApplicationContext:
      Used when the XML configuration is stored outside the classpath.
      ApplicationContext context = new FileSystemXmlApplicationContext("C:/config/applicationContext.xml");
    4. Using ApplicationContext implementation i.e. AnnotationConfigApplicationContext:
      Used When using Java-based configuration (recommended for modern apps).
      ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
  • Some points to Remember :
    • For modern Spring applications, use AnnotationConfigApplicationContext because it promotes type safety and avoids cumbersome XML files.
    • For legacy applications or when working with XML-based configurations, we can use ClassPathXmlApplicationContext or FileSystemXmlApplicationContext.
    • For web applications, use WebApplicationContext integrated with a web framework like Spring MVC.