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

Spring Interview Questions - 2


6. What is the Difference Between BeanFactory and ApplicationContext ?

Some important differences between BeanFactory & ApplicationContext are as below :-

Aspect BeanFactory ApplicationContext
1. Definition It is "basic container" that manages beans with minimal features. Its is "advanced container" extending BeanFactory with extra features.
2. Usage It is suitable for small, standalone applications. It is suitable for web applications, AOP, ORM and large enterprise applications.
3. Resource Usage It requires less memory, suitable for lightweight or memory-critical applications. It uses more memory, providing additional features for enterprise applications.
4. Initialization It uses lazy initialization i.e. it creates the beans only when needed. It uses eager initialization i.e. it loads and creates the beans at container startup.
5. Bean Scopes Supported It supports only Singleton and Prototype scopes. It supports all scopes, including Request and Session, for web contexts.
6. Annotation Support It doesn’t support annotations; requires XML configuration. It supports annotations for simpler configuration.
7. Internationalization It does not provide support for internationalization (i18n). It provides the i18n support for multilingual applications.
8. Event Handling It does not support event handling. It supports event handling via ApplicationEvent and ApplicationListener.
7. What do you mean by Spring Configuration, explain with examples.
  • In Spring, Configuration refers to the "Instructions" or "Setup" that are provided to the Spring Container.

  • There are multiple types of Spring Configurations like :-
    Bean Configurations
    Database Configurations
    MVC Setup
    Security Configurations
    AOP (Aspect-Oriented Programming)
    etc...
  • Spring Configuration instructs the Spring Container that what beans should be created, how they’re instantiated, which dependencies to be injected, what are database configurations (url, username, password), setting up the MVC configurations for Spring MVC applications etc.

  • Click Here to read Spring Configurations more deeply
8. What are the different ways to provide Configurations in Spring ?

Different ways to provide Spring Configurations are as below :-

  1. XML-Based Configuration : We can use XML File for Spring Configurations to define beans and their dependencies in a file, typically named applicationContext.xml.
  2. Java-Based Configuration : We can use Java Class annotated with @Configuration for Spring Configurations. This class contains methods annotated with @Bean to explicitly declare beans.
  3. Annotation-Based Configuration : We can use Annotations directly on Java classes for Spring Configurations to define beans and configure dependencies. Common annotations include @Component, @Controller, @Service, @Repository and @Autowired.
  • Click Here to read Spring Configurations more deeply
9. Explain @Configuration and @Bean Annotations

The @Configuration and @Bean annotations in Spring are "key to Java-Based Configuration". They allow us to define and manage beans in a configuration class, providing an alternative to XML-based configuration.

  • @Configuration Annotation :-
    • Definition: @Configuration annotation is used to "mark the class as Spring Configuration Class" for the Spring IoC (Inversion of Control) container.
    • Purpose: It tells Spring that this class contains different configurations like bean configurations, database configurations, MVC configurations etc that should be managed by the Spring container.
    • Syntax:
      import org.springframework.context.annotation.Configuration;
      
      @Configuration
      public class AppConfig
      {
          /*
              Here we provide the different configurations for example....
              Bean Configurations, Database Configurations, MVC Configurations, Security Configurations etc
          */
      }
    • Click Here to read @Configuration annotation more deeply

  • @Bean Annotation :-
    • Definition: @Bean annotation is used within a @Configuration class to define a bean that will be managed by the Spring IoC container.
    • Purpose: It tells Spring to "register a method’s return value as a bean" in the application context.
    • Syntax:
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      
      @Configuration
      public class AppConfig
      {
          // Below @Bean annotation is used to create Student class Bean
          @Bean
          public Student stdId() // Here method name is the bean-id / bean-name
          {
              // It will return the Student object
              return new Student();
          }
      }
    • Click Here to read @Bean annotation more deeply

10. Explain @Component and @ComponentScan Annotations
  • @Component Annotation :-
    • Definition: @Component is a Spring annotation used to "mark a class as a Spring-managed component". This annotation enables the Spring IoC container to autodetect and register the class as a bean when classpath scanning is enabled.
    • Purpose: It tells Spring to treat the annotated class as a Spring bean, allowing it to participate in dependency injection. By using @Component, we eliminate the need for manual bean configuration in XML or @Bean methods in configuration classes. This annotation is often used to mark core classes such as service, repository or utility classes.
    • Scope: By default, the bean created by @Component is a singleton. However, the scope can be changed with @Scope if necessary.
    • Syntax:
      import org.springframework.stereotype.Component;
      
      @Component
      public class MyService
      {
          // class implementation
      }
      
    • Click Here to read @Component annotation more deeply

  • @ComponentScan Annotation :-
    • Definition: @ComponentScan is a Spring annotation used to specify the packages that Spring should scan for components. This annotation tells the Spring container to look in the specified packages (and their sub-packages) for classes annotated with @Component or other stereotype annotations (@Service, @Repository, @Controller).
    • Purpose: It allows Spring to automatically detect and register all annotated components, eliminating the need for manual bean definitions for each component class. @ComponentScan is essential for enabling component-based configuration in larger applications.
    • When to Use: @ComponentScan is generally used within a @Configuration class. Without it, Spring would not automatically discover and register @Component beans outside the package of the configuration class itself. It’s especially useful for modular applications where classes are organized across multiple packages.
    • Syntax:
      import org.springframework.context.annotation.ComponentScan;
      import org.springframework.context.annotation.Configuration;
      
      @Configuration
      @ComponentScan(basePackages = "in.sp.package")
      public class AppConfig
      {
          // Application configuration
      }
    • Click Here to read @ComponentScan annotation more deeply