Spring Framework Tutorial

Software Setup and Configuration (STS/Eclipse/IntelliJ)

Core Spring

Spring Annotations

Spring Data

Spring JDBC

Spring Security

Spring - BeanFactory

The BeanFactory is a central interface in the Spring framework for accessing application components. It is the root interface for accessing the Spring container. The BeanFactory provides advanced configuration facilities to manage any type of object, and the objects it manages are called beans.

The ApplicationContext is a sub-interface of BeanFactory and offers a more complete and sophisticated integration for building enterprise applications. Typically, for most use cases, developers interact more with ApplicationContext, but it's essential to understand BeanFactory as the foundation.

Key Points:

  1. Configuration Mechanism: BeanFactory uses configuration metadata provided to it (typically in the form of XML configuration) to instantiate and wire up beans as required.

  2. Lazy Initialization: By default, the BeanFactory initializes beans lazily. This means that a bean isn't instantiated until an application explicitly asks for it. This behavior is in contrast with ApplicationContext, which typically pre-instantiates singleton beans on startup.

  3. Dependency Injection: BeanFactory can inject dependencies into your beans, either through constructor injection or setter injection. This inversion of control ensures that components have their dependencies met without being explicitly responsible for the creation or lookup of their dependencies.

  4. Bean Lifecycle: The BeanFactory manages the entire lifecycle of beans from creation to destruction. It also allows for custom initialization and destroy methods.

  5. Resource Loading: While BeanFactory itself doesn't provide a way to load resources (like properties files), there's a separate ResourceLoader interface (which ApplicationContext implements) for this purpose.

Basic Usage:

Here's a simple example of using BeanFactory with XML configuration:

beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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="myBean" class="com.example.MyClass" />
</beans>

Java Code:

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class BeanFactoryExample {
    public static void main(String[] args) {
        // Load bean definitions from XML
        BeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml"));

        // Retrieve the bean from the container
        MyClass myBean = (MyClass) factory.getBean("myBean");
        myBean.doSomething();
    }
}

Points to Note:

  • XmlBeanFactory is one of the implementations of the BeanFactory interface, which reads bean definitions from an XML file.

  • In most applications, especially modern ones, it's more common to use ApplicationContext (like ClassPathXmlApplicationContext or AnnotationConfigApplicationContext) instead of BeanFactory. ApplicationContext brings more features like internationalization, event propagation, and specialized contexts (like WebApplicationContext for web apps).

In summary, while BeanFactory provides the core functionality required for managing beans and dependency injection, the richer ApplicationContext interface is more widely used in modern Spring applications due to its extended features.

  1. Configuring beans with BeanFactory:

    • Use BeanFactory to configure beans by defining them in XML or Java configuration. ApplicationContext provides a more feature-rich alternative.
    XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml"));
    MyBean myBean = (MyBean) factory.getBean("myBean");
    
  2. Using BeanFactory for dependency injection in Spring:

    • BeanFactory supports dependency injection by automatically wiring dependencies. Define dependencies in bean configurations.
    XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml"));
    MyService myService = (MyService) factory.getBean("myService");
    
  3. Lazy loading beans with BeanFactory in Spring:

    • BeanFactory supports lazy loading, allowing beans to be created only when requested.
    XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml"));
    MyLazyBean lazyBean = (MyLazyBean) factory.getBean("lazyBean");
    
  4. Bean instantiation and retrieval with BeanFactory:

    • Instantiate and retrieve beans using BeanFactory. Beans are created when getBean is called.
    XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml"));
    MyBean myBean = (MyBean) factory.getBean(MyBean.class);
    
  5. Customizing BeanFactory in Spring applications:

    • Extend and customize BeanFactory for specific requirements, such as customizing bean instantiation or lifecycle.
    public class CustomBeanFactory extends DefaultListableBeanFactory {
        // Customizations...
    }
    
    CustomBeanFactory factory = new CustomBeanFactory();
    MyBean myBean = (MyBean) factory.getBean("myBean");
    
  6. Programmatic bean definition in Spring using BeanFactory:

    • Programmatically define beans using BeanFactory. This is useful when XML or annotation-based configurations are not suitable.
    XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans.xml"));
    
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MyBean.class);
    factory.registerBeanDefinition("myBean", builder.getBeanDefinition());
    
    MyBean myBean = (MyBean) factory.getBean("myBean");