๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

BeanFactory - Spring Container


Introduction

  • BeanFactory is one of the "Core Container" provided by the Spring framework.
  • It represents the simplest version of Spring IoC (Inversion of Control) container that manages and configures beans.
  • The main purpose of BeanFactory is to instantiate beans only on demand (lazy initialization), meaning it creates an object only when it is requested, not during the container startup.
  • Note : BeanFactory itself is an "interface", its concrete implementations (like XmlBeanFactory class or DefaultListableBeanFactory class) act as actual Spring containers.

When to use BeanFactory ?

BeanFactory is suitable for lightweight applications with minimal configuration.


Advantages of BeanFactory

  1. Lazy initialization of beans : Beans are created only when they are requested for the first time, which helps reduce startup time and memory footprint.
    For example; if our application has many beans, but only a subset is needed during initialization, BeanFactory ensures that only the required beans are created on demand.
  2. Lightweight Container : BeanFactory is lightweight and provides just the essential features for managing beans, making it ideal for resource-limited environments (like IoT or embedded systems).
  3. Dependency Injection Support : It provides the core functionality of dependency injection (constructor-based and setter-based injection), making it possible to decouple objects and promote code reusability.
  4. Flexibility in XML-Based Configuration : While BeanFactory can load beans from XML files, it also allows programmatic configuration and supports various bean scopes (like singleton and prototype).

Limitations of BeanFactory

While BeanFactory has its benefits, it also has several limitations (which is why ApplicationContext is usually preferred):

  1. Lack of advanced features : No built-in support for AOP support, declarative transactions or event handling.
  2. No support for internationalization (I18N) : It does not provide features like message sources for localization.
  3. Complex initialization for larger applications : We must manually refresh the container, which can become difficult as the project grows.

Note: BeanFactory is an "Old Spring Container" so we should not use it in real world projects. Instead of BeanFactory we should use "ApplicationContext".

Interview Question : What is Difference between BeanFactory & ApplicationContext ?
Click Here for answer


Implemented Class of BeanFactory

  1. XmlBeanFactory :
    • XmlBeanFactory is an implemented class of BeanFactory interface. It is a Spring container that loads bean definitions from an XML configuration file and manages their lifecycle.
    • NOTE that XmlBeanFactory container is deprecated in newer versions of Spring.
    • Syntax of creating XmlBeanFactory container is as below :
      ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");