Spring Framework

Spring Projects

Spring Project 1

adplus-dvertising
Example of Lookup Method Bean
Previous Home Next

Introduction: The Lookup Method injection provide a facility to one singleton bean depends on a non-singleton. It means that singleton bean depends on prototype bean.

Technology use to run this source code

  1. Spring 2.5 jar files
  2. Eclipse IDE
  3. Tomcat Server
  4. CGLIB jar file.

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.Burgar" scope="prototype"/>
<bean id="na" class="org.r4r.NewAttendant" >
<lookup-method bean="b" name="getBurgar"/>
</bean>
</beans>

Burgar.java

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

NewAttendant.java

package org.r4r;
public abstract class NewAttendant {
public abstract Burgar getBurgar();
}

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 attendent bean....");
NewAttendant a=(NewAttendant)f.getBean("na");
System.out.println("Requesting two burgar.....");
Burgar b1=a.getBurgar();
System.out.println(b1);
Burgar b2=a.getBurgar();
System.out.println("Discription of second burgar...");
System.out.println(b2);
}
}

output:

Obtaining attendent bean....
Requesting two burgar.....
New burgar is created...
It is burger number:-1
New burgar is created...
Discription of second burgar...
It is burger number:-2
Previous Home Next