Spring Framework

Spring Projects

Spring Project 1

adplus-dvertising
Example of Difference scope bean
Previous Home Next

Introduction: In the case of different scope bean BeanFactoryAwar interface provide the facility to give the service one scope to more than one different scope. The BeanFactoryAwar interface provide a method name setBeanFactory() to override in our bean class than solved these problem in the spring framework.

Technology use to run this source code

  1. Spring 2.5
  2. Eclipse
  3. JDK 1.6

Source Code:

applicationContext.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="b" class="org.r4r.Burger" scope="prototype"/>
<bean id="a" class="org.r4r.Attendant" >
</bean>
</beans>

Burger.java

package org.r4r;
public class Burger {
static int burgerCounter;
int burgerNo;
public Burger(){
burgerCounter++;
burgerNo=burgerCounter;
System.out.println("New burger is created...");
}
public String toString(){
return "It is burger number:-"+burgerNo;
}
}

Attendant.java

package org.r4r;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
public class Attendant implements BeanFactoryAware{
BeanFactory beanfactory;
/*In this approach same Burger provided by Attendant
 * Then solved this approach after commented code
 * Burger burger;
public Attendant(Burger burger) {
super();
this.burger = Burger;
System.out.println("Attendant bean is created...");
}
public Burger getBurger(){
return burger;
}*/
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
beanfactory=beanFactory;
System.out.println("Reference of IOC container is provided to attendant.");
}
public Attendant() {
System.out.println("Attendant bean is created...");
}
public Burger getBurger(){
return (Burger)beanfactory.getBean("b");
}
}

Test.java

package org.r4r;
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;
public class Test {
public static void main(String[] args) {
Resource r=new ClassPathResource("applicationContext.xml");
BeanFactory f=new XmlBeanFactory(r);
System.out.println("Obtaining attendant bean....");
Attendant a=(Attendant)f.getBean("a");
System.out.println("Requesting two burger.....");
Burger b1=a.getBurger();
System.out.println(b1);
Burger b2=a.getBurger();
System.out.println("Description of second burger...");
System.out.println(b2);
}
}

output:

Obtaining attendant bean....
Attendant bean is created...
Reference of IOC container is provided to attendant.
Requesting two burger.....
New burger is created...
It is burger number:-1
New burger is created...
Description of second burger...
It is burger number:-2
Previous Home Next