Spring Boot Tutorial

Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)

Prerequisite (Spring Core Concepts)

Spring Boot Core

Spring Boot with REST API

Spring Boot with Database and Data JPA

Spring Boot with Kafka

Spring Boot with AOP

Spring - BeanFactory

BeanFactory is the root interface for accessing the Spring container. It provides the basic functionalities to manage and access beans, but it's often overshadowed by its more feature-rich sub-interface, ApplicationContext.

Key Features of BeanFactory:

  1. Bean Instantiation & Configuration: Allows you to configure and retrieve beans.

  2. Singleton/Prototype Bean Management: Supports both singleton (a single shared instance) and prototype (a new instance every time) bean lifecycles.

  3. Bean Aliasing: Beans can be queried and retrieved using different names.

  4. Type Conversion: Supports type conversion for properties.

  5. Aspect-Oriented Programming (AOP): Provides basic support for AOP through interceptors and advisors.

BeanFactory Implementations:

The most commonly used BeanFactory implementation is XmlBeanFactory, which loads beans based on the definitions in an XML file. However, with modern Spring applications, XmlBeanFactory is now considered deprecated in favor of ApplicationContext.

Usage of XmlBeanFactory (for older versions of Spring):

Resource resource = new ClassPathResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(resource);

MyBean bean = (MyBean) factory.getBean("myBean");

BeanFactory vs ApplicationContext:

While BeanFactory provides the basic features for DI (Dependency Injection), ApplicationContext adds more enterprise-specific functionalities:

  • MessageSource: Handling of messages, supporting internationalization.

  • Event Propagation: Supports application event propagation.

  • Web Applications: The WebApplicationContext interface integrates with web applications, adding web-related features.

  • Eager Loading: While BeanFactory lazily initializes beans by default (i.e., beans are created when requested), ApplicationContext preloads singleton beans at startup.

  • Integrated AOP features: More comprehensive support for aspect-oriented functionalities.

For most scenarios, especially in web-based applications, developers typically use ApplicationContext due to its richer set of features. However, if you're building a lightweight application and only need basic DI features, BeanFactory might suffice.

In contemporary Spring development, ApplicationContext is more commonly used, and direct interactions with BeanFactory are rare.

  1. Spring BeanFactory example:

    • XML Configuration:
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
      
          <bean id="car" class="com.example.Car"></bean>
      </beans>
      
    • Java Code:
      BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
      Car car = (Car) beanFactory.getBean("car");
      
  2. BeanFactory configuration in Spring:

    • Configure BeanFactory using XML or Java:
      • XML Configuration:
        <bean id="car" class="com.example.Car"></bean>
        
      • Java Configuration:
        @Configuration
        public class AppConfig {
            @Bean
            public Car car() {
                return new Car();
            }
        }
        
  3. XML configuration for BeanFactory in Spring:

    • Example XML configuration:
      <bean id="car" class="com.example.Car"></bean>
      
  4. Annotation-based configuration with Spring BeanFactory:

    • Example Java configuration with annotations:
      @Configuration
      public class AppConfig {
          @Bean
          public Car car() {
              return new Car();
          }
      }
      
  5. BeanFactoryPostProcessor in Spring:

    • Customize BeanFactory's bean definitions before instantiation.
    • Implement BeanFactoryPostProcessor interface.
    • Example:
      public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
          @Override
          public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
              // Customize bean definitions
          }
      }
      
  6. Customizing BeanFactory in Spring:

    • Implement custom post-processors and initialization methods.
    • Extend AbstractBeanFactory for more customization.
    • Example:
      public class CustomBeanFactory extends DefaultListableBeanFactory {
          // Customization logic
      }