Lookup Method Injection

今天看到method injection这块,网上查了下,又了看了下reference,作个总结加备份吧算是,以下实例来自网络,可以运行
Lookup method injection使Spring克服当一个bean依赖其他不同生命周期的bean的情况,比如当一个单例bean依赖一个非单例对象的时候。在这种情况下,无论是使用setter注入还是constructor注入都只是在单例对象中维护一个非单例bean的一个单例(也就是一个相同的实例)。但是有些情况下,你将希望你的bean每次请求一个非单例bean的实例时都能够获得一个新的实例。

下面的例子中创建了两个单例bean和一个非单例bean;所有的单例bean实现相同的接口。一个单例bean使用setter注入获得非单例bean的实例,另外一个使用method injection。
public class MyHelper {

public void doSomethingHelpful() {
}
}

public interface DemoBean {

MyHelper getMyHelper();
void someOperation();
}


public class StandardLookupDemoBean implements DemoBean {
private MyHelper helper;
public void setMyHelper(MyHelper helper) {
this.helper = helper;
}
public MyHelper getMyHelper() {
return this.helper;
}
public void someOperation() {
helper.doSomethingHelpful();
}
}



public abstract class AbstractLookupDemoBean implements DemoBean {

public abstract MyHelper getMyHelper();
public void someOperation() {
getMyHelper().doSomethingHelpful();
}
}

<?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="helper" class="com.inspur.methodInjection.MyHelper" scope="prototype" />
<bean id="abstractLookupBean" class="com.inspur.methodInjection.AbstractLookupDemoBean">
<lookup-method name="getMyHelper" bean="helper" />
</bean>
<bean id="standardLookupBean" class="com.inspur.methodInjection.StandardLookupDemoBean">
<property name="myHelper" ref="helper" />
</bean>
</beans>

[quote]注意上面的配置文件,使用<lookup-method>配置abstractLookupBean的lookup method。<lookup-method>元素的name属性用于告诉Spring重载bean的那个方法。这个方法不能有任何参数,并且返回值应该是你希望使用得到的那个bean类型。bean属性告诉Spring,lookup method应该返回那个bean[/quote]

 public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("bean4.xml"));
stressTest(factory, "abstractLookupBean");
stressTest(factory, "standardLookupBean");
}

private static void stressTest(XmlBeanFactory factory, String beanName) {
DemoBean bean = (DemoBean) factory.getBean(beanName);
MyHelper helper1 = bean.getMyHelper();
MyHelper helper2 = bean.getMyHelper();
System.out.println("Testing " + beanName);
System.out.println("Helper Instances the Same?: "+ (helper1 == helper2));
StopWatch stopWatch = new StopWatch();
stopWatch.start("lookupDemo");
for (int i = 0; i < 100000; i++) {
MyHelper helper = bean.getMyHelper();
helper.doSomethingHelpful();
}
stopWatch.stop();
System.out.println("100000 gets took " + stopWatch.getTotalTimeMillis()+ " ms");
}

Testing abstractLookupBean
Helper Instances the Same?: false
100000 gets took 391 ms
Testing standardLookupBean
Helper Instances the Same?: true
100000 gets took 15 ms


[quote]使用standardLookupBean获得的对象是相同的,使用abstractLookupBean得到的对象不同。当然,性能差别也是显而易见的。产生性能差别的主要原因是因为我们设置helper bean的范围是prototype,这将导致每次在abstractDemoBean中调用abstractLookupBean的getMyHelper()方法都产生一个helper bean的新实例;第二个原因是因为abstractLookupBean实际上是AbstractLookupDemoBean类的一个CGLIB代理,代理拦截对getMyHelper()方法的所有调用并且返回helper bean,拦截机制的本质也是导致性能下降的原因。
[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值