Spring — Lookup Method Dependency Injection | Code Factory

Donate : Link

WordPress Blog : Link

Applications… : Link

Spring Tutorial Index Page: Link

  • We can also inject DI using Lookup Method.
  • Lookup Method means if a method is not having any implementation or a method is require any dependencies is considered as a Lookup Method.
  • In interface all methods (abstract methods) are lookup methods. We can use this concept for interface, abstract class and concrete class also.
  • Programmer don’t require to write implementation code, Spring will write code for programmer.
  • We require cglib jar to run proxies.
  • It is not possible to create object of interface w/o implementation. But in our example Spring will create object of Car interface. How is it possible? So answer is spring internally create proxy class and implement Car and create object of that class so we can get Car object through getBean() method.

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Others… -> Java Project
  • Create DI-LookupMethod project
  • Right click on project -> Build Path -> Configure Build Path -> Libraries tab -> Add External JARs
    - cglib-nodep-X.X.X.jar
    - commons-logging-X.X.jar
    - spring-beans-X.X.X.jar
    - spring-context-X.X.X.jar
    - spring-core-X.X.X.jar
  • * You can find dtd information from spring-beans-X.X.X.jar -> org -> springframework -> beans -> factory -> xml -> spring-beans.dtd (line no 36 & 37)

spring.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans> <!-- for Car interface lookup method -->
<bean id="c" class="com.codeFactory.beans.Car">
<lookup-method name="myCarEngine" bean="e1" />
</bean>

<bean id="e1" class="com.codeFactory.beans.Engine">
<property name="name" value="JLR Engine"></property>
</bean>

<!-- for Bus abstract lookup method -->
<bean id="b" class="com.codeFactory.beans.Bus">
<lookup-method name="myBusEngine" bean="e2" />
</bean>

<bean id="e2" class="com.codeFactory.beans.Engine">
<property name="name" value="TATA Engine"></property>
</bean>

<!-- for Truk concrete lookup method -->
<bean id="t" class="com.codeFactory.beans.Truk">
<lookup-method name="myTrukEngine" bean="e3" />
</bean>

<bean id="e3" class="com.codeFactory.beans.Engine">
<property name="name" value="BharatBenz Engine"></property>
</bean>
</beans>

Bus.java

package com.codeFactory.beans;/**
* @author code.factory
*
*/
public abstract class Bus {
abstract public Engine myBusEngine();
}

Car.java

package com.codeFactory.beans;/**
* @author code.factory
*
*/
public interface Car {
public Engine myCarEngine();
}

Engine.java

package com.codeFactory.beans;/**
* @author code.factory
*
*/
public class Engine {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

Truk.java

package com.codeFactory.beans;/**
* @author code.factory
*
*/
public class Truk {
public Engine myTrukEngine() {
Engine e = new Engine();
e.setName("Eicher");
return e;
}
}

Client.java

package com.codeFactory.test;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.codeFactory.beans.Bus;
import com.codeFactory.beans.Car;
import com.codeFactory.beans.Truk;
/**
* @author code.factory
*
*/
public class Client {
public static void main(String... args) {
ApplicationContext context = new ClassPathXmlApplicationContext("com/codeFactory/resources/spring.xml");

Car c = (Car) context.getBean("c");
System.out.println(c.myCarEngine().getName());
System.out.println(c.getClass().getCanonicalName());

Bus b = (Bus) context.getBean("b");
System.out.println(b.myBusEngine().getName());
System.out.println(b.getClass().getCanonicalName());

Truk t = (Truk) context.getBean("t");
System.out.println(t.myTrukEngine().getName());
System.out.println(t.getClass().getCanonicalName());
}
}

Output:

JLR Engine
com.codeFactory.beans.Car$$EnhancerByCGLIB$$a3f74ae8
TATA Engine
com.codeFactory.beans.Bus$$EnhancerByCGLIB$$e4ceb238
BharatBenz Engine
com.codeFactory.beans.Truk$$EnhancerByCGLIB$$cc6df530