Spring Bean Factory

In Spring, a bean can be any Java object or a class instance that is created and managed by the Spring container.

The bean factory container is used to:

Instantiate, configure, and manage objects (beans) for the Web application. Define relationships between different objects, as and when they are being instantiated.

The objects created by the bean factory are:

1. Fully configured.

2. Ready to use.

3. Aware of their relationships with the other application objects.

Spring bean container

Spring is actually a container of beans or spring behaves as a factory of beans. For e.g servlet container tomcat create the servlet object which are required in order to run an application. Tomcat reads xml i.e Deployment Descripter and identifies the servlet need to be instansed and creates those servlets.

Spring is somthing similar, it is a container which contains beans. So u can have spring container and inside this u can have as many objects.These object are managed by spring container.Spring handles life cycle events of these object.

You can have object oustside the container as well.

Factory Pattern

Say MyApplication need an object so it will ask Object factory that i need an object of such specification then Object Factory reads from Configuration file and create new object and hand over to MyApplication.

factory pattern

Now MyApplication han an object it wants but we have not hardcoded object in MyApplication. It is reffering to Object factory and object factory create this new object by reading from configuration file.

Spring does the same thing.Spring has Bean factory to create new objects.So instead of hardcoded new objcet we ask spring bean factory to create an object then bean factory reads from Spring XML i.e configuration file which contain bean defination.

Bean factory create bean from this blue print or configuration file and make new Spring bean i.e object

Since this new bean has been created in the bean factory. So spring know about it and manages all life cycle of this bean.Spring act as container for this new spring bean.

Spring bean factory

The following steps are performed during the life cycle of a bean inside the bean factory

  • Instantiate
  • Populate properties
  • Set bean name
  • Set bean factory
  • Pre-initialization
  • Initialize beans
  • Post-initialization
  • Bean ready for use
  • Destroy the bean

Spring.xml or Configuration File

This XML file is also called the bean configuration file. The configuration file is the key for setting up any Spring-enabled application.

In a bean configuration file, each bean is defined by using the tag within the tag.

A bean definition contains basic information about a bean that a container must know, such as:

  • The process for creating a bean.
  • The details about the bean life cycle.
  • The various dependencies for that bean.

The tag contains the following attributes to define the bean:

  • id
  • name
  • class
  • scope

You can load the bean configuration file from different places, such as

  • File system
  • Classpath
  • External URL