What is BeanFactory in Spring

A BeanFactory is like a factory class that contains a collection of beans. The BeanFactory holds Bean Definitions of multiple beans within itself and then instantiates the bean whenever asked for by clients.
  •  BeanFactory is able to create associations between collaborating objects as they are instantiated. This removes the burden of configuration from bean itself and the beans client.
  •   BeanFactory also takes part in the life cycle of a bean, making calls to custom initialization and destruction methods.
This is the simplest container providing basic support for DI and defined by the org.springframework.beans.factory.BeanFactory interface. The BeanFactory and related interfaces, such as BeanFactoryAware, InitializingBean, DisposableBean, are still present in Spring for the purposes of backward compatibility with the large number of third-party frameworks that integrate with Spring.

BeanFactory in Spring

package com.sdnext.dineshonjava.beanfactory.tutorial;

public class Triangle 
 public void draw()
     System.out.println("Drawing Triangle");
<beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:security="http://www.springframework.org/schema/security" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
     http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd 
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
   <bean class="com.sdnext.dineshonjava.beanfactory.tutorial.Triangle" id="triangle">
package com.sdnext.dineshonjava.beanfactory.tutorial;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

 * @author Dinesh Rajput
public class DrawingApp 
  * @param args
 public static void main(String[] args) 
  //Triangle triangle = new Triangle();
      BeanFactory factory = new XmlBeanFactory(new FileSystemResource("spring.xml"));
  Triangle triangle = (Triangle) factory.getBean("triangle");

There are following two important points to note about the main program:
  1. First step is to create factory object where we used framework API XmlBeanFactory() to create the factory bean and ClassPathResource() API to load the bean configuration file available in CLASSPATH. The XmlBeanFactory() API takes care of creating and initializing all the objects ie. beans mentioned in the configuration file.
  2. Second step is used to get required bean using getBean() method of the created bean factory object. This method uses bean ID to return a generic object which finally can be casted to actual object. Once you have object, you can use this object to call any class method.
After run the Drawing class we will get following Output on console.
Jun 17, 2012 3:47:36 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from file [F:\my workspace\spring03BeanFactoryDemo\spring.xml]
Drawing Triangle

In Next Chapter we will discuss same example using Application Context Container.

                                            << Previous || Next >>

No comments:

Post a Comment