@Lookup
tells Spring to return an instance of the method's return type when it gets invoked. In this case Spring will override the annotated method and will use the method's return type and parameters as arguments to the call to BeanFactory.getBean()
.@Lookup
is useful for:@Lookup
is the Java equivalent of the XML element lookup-method
in applicationContext.xml.@Lookup
for Bean Injection, is that @Lookup
allows us to inject a dependency procedurally, something that we cannot do with Provider.package net.netconomy.popcornfactory;
public abstract class PopcornShop {
public abstract Popcorn makePopcorn();
public abstract Popcorn makeSweetPopcorn();
}
package net.netconomy.popcornfactory;
import java.util.concurrent.atomic.AtomicLong;
public class Popcorn {
private static AtomicLong count = new AtomicLong(0);
private boolean sweet;
public Popcorn(sweet) {
this.sweet = sweet;
count.incrementAndGet();
}
public String toString() {
return "Made a bucket of " + (sweet ? "sweet " : "") + "Popcorn. Buckets made overall:" + count.get();
}
public void setSweet(boolean sweet) {
this.sweet = sweet;
}
}
sweet
which will be true if the Popcorn is sweet rather than salty. (I love sweet popcorn! 🤤)makePopcorn
and makeSweetPopcorn
as lookup-methods.popcorn
and a sweetPopcorn
bean as prototype scoped beans.<lookup-method../>
element.<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="popcornShop" class="net.netconomy.popcornfactory.PopcornShop">
<lookup-method name="makePopcorn" bean="popcorn"/>
<lookup-method name="makeSweetPopcorn" bean="sweetPopcorn"/>
</bean>
<bean id="popcorn" class="net.netconomy.popcornfactory.Popcorn" scope="prototype">
<constructor-arg type="boolean">
<value>false</value>
</constructor-arg>
</bean>
<bean id="sweetPopcorn" class="net.netconomy.popcornfactory.Popcorn" scope="prototype">
<constructor-arg type="boolean">
<value>true</value>
</constructor-arg>
</bean>
</beans>
@Lookup
annotation like we did in the first Blogpost.PopcornShop
then would look something like this:package net.netconomy.popcornfactory;
public abstract class PopcornShop {
@Lookup(value="popcorn")
public abstract Popcorn makePopcorn();
@Lookup(value="sweetPopcorn")
public abstract Popcorn makeSweetPopcorn();
}
@Configuration
public class AppConfig {
@Bean
@Scope("prototype")
public Popcorn popcorn() {
return new Popcorn(false);
}
@Bean
@Scope("prototype")
public Popcorn sweetPopcorn() {
return new Popcorn(true);
}
@Bean
public PopcornShop popcornShop() {
return new PopcornShop();
}
}
popcornShop.makePopcorn()
and popcornShop.makeSweetPopcorn()
.package net.netconomy.popcornfactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringPopcornLookupMethodExample {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
try {
PopcornShop popcornShop = (PopcornShop) context.getBean("popcornShop");
Popcorn firstPopcorn = popcornShop.makePopcorn();
System.out.println("- First Popcorn: " + firstPopcorn);
Popcorn secondPopcorn = popcornShop.makePopcorn();
System.out.println("- Second Popcorn: " + secondPopcorn);
Popcorn sweetPopcorn = popcornShop.makeSweetPopcorn();
System.out.println("- Yummy, sweet Popcorn!:" + sweetPopcorn);
} finally {
context.close();
}
}
}
Popcorn
.toString()
Method defined in the Popcorn class.)- First Popcorn: Made a bucket of Popcorn. Buckets made overall:
- Second Popcorn: Made a bucket of Popcorn. Buckets made overall:
- Yummy, sweet Popcorn!: Made a bucket sweet of Popcorn. Buckets made overall:
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |