Bean Factory Example

BeanFactory is the root interface for accessing a spring bean container. The BeanFactory is the actual container which instantiates, configures, and manages a number of beans. XmlBeanFactory is a class which provides the implementation of BeanFactory interface.
Whenever we create the object to XmlBeanFactory we consider that spring container object is created. In spring we have an interface resource available in a package org.springframework.core.io package. This interface is used to deal with the resources. The classes ClassPathResource and FileSystemResource, as shown in the below Fig(a), provide the implementation of Resource interface.

FileSystemResource

FileSystemResource is a class which is used to read the contents from any resource available in the file system. A simple example on how to use FileSystemResource can be found here FileSystemResource Example

ClassPathResource

ClassPathResource is a class which is used to read the contents from the resources available in classpath of our project. An example of ClassPathResource is given below.
Below is a simple example of Spring BeanFactory.
First create an Address bean under src as shown below. File: com.ram.bean.Address.java
package com.ram.bean;

public class Address {
 private String street;
 private String city;
 private String state;
 
 public String getStreet() {
  return street;
 }
 public void setStreet(String street) {
  this.street = street;
 }
 public String getCity() {
  return city;
 }
 public void setCity(String city) {
  this.city = city;
 }
 public String getState() {
  return state;
 }
 public void setState(String state) {
  this.state = state;
 }
 
}
Next create applicationContext.xml file under src. The file is given below.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
 <bean id="addr" class="com.ram.bean.Address">
  <property name="street" value="Richmond"></property>
  <property name="city" value="Melbourne"></property>
  <property name="state" value="Victoria"></property>
 </bean>
 
</beans>
Next create a main class under src as shown. File: com.ram.app.BeanFactoryExample.java
package com.ram.app;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import com.ram.bean.Address;

public class BeanFactoryExample {
 public static void main(String[] args){
  Resource resource = new ClassPathResource("applicationContext.xml");
  BeanFactory container = new XmlBeanFactory(resource);
  Object obj = container.getBean("addr");
  
  Address address = (Address) obj;
  System.out.println("Street = "+address.getStreet());
  System.out.println("City = "+address.getCity());
  System.out.println("State = "+address.getState());
 }
}

When we call getBean() method, spring container takes the id and creates the object of spring bean and establishes the dependencies and return the object. Execute BeanFactoryExample.java file. Output is given below.
If we call getBean() method for multiple times, spring container checks the scope attribute. If scope is not defined in the "applicationContext.xml" file, then by default the scope value is "singleton". What this means is, spring container will create the object once and every time returns the reference of same object.
If we would like to create an object every time a getBean() method is called, then specify scope="prototype" attribute. The following are the values of the scope.
  • singleton
  • prototype
  • request
  • session

No comments:

Post a Comment