Spring — Lookup Method Dependency Injection | Code Factory

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)
<beans> <!-- for Car interface lookup method -->
<bean id="c" class="com.codeFactory.beans.Car">
<lookup-method name="myCarEngine" bean="e1" />

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

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

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

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

<bean id="e3" class="com.codeFactory.beans.Engine">
<property name="name" value="BharatBenz Engine"></property>
package com.codeFactory.beans;/**
* @author code.factory
public abstract class Bus {
abstract public Engine myBusEngine();
package com.codeFactory.beans;/**
* @author code.factory
public interface Car {
public Engine myCarEngine();
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;
package com.codeFactory.beans;/**
* @author code.factory
public class Truk {
public Engine myTrukEngine() {
Engine e = new Engine();
return e;
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");

Bus b = (Bus) context.getBean("b");

Truk t = (Truk) context.getBean("t");
JLR Engine
TATA Engine
BharatBenz Engine




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Arranging the Unity editor layout for production efficiency!

Functor from a beginner perspective

Steps to create a base for mobile app development (Android or iOS)

Developing Blood Banks and Donors Finder

Painless Production — GDELT Analysis with Aero

How to deploy Cuckoo Sandbox

How to fix wrong status bar orientation in iOS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Memory Leak analysis in java

Field Injection v/s Constructor Injection

Experience with Spring batch and docker

Transforming a box of Lego into a CI build monitoring robot