《Pro Spring》学习笔记之Loopup Method Injection(查找方法注入)

 

package  ch5.LookupMethod;

public   interface  DemoBean  {
  
public MyHelper getmyHelper();
  
public void someOperation();
}

Spring使用CGLIB的动态字节码增强功能,所以,必须要加入CGLIB包

当Bean依赖另一个生命周期不同的bean,尤其是当singleton依赖一个non-singleton时,常会遇到不少问题,Lookup Method Injection正是对付这些问题而出现的,在上述情况中,setter和构造注入都回导致singleton去维护一个non-singleton bean的单个实例,某些情况下,我们希望让singleton bean每次要求获得bean时候都返回一个non-singleton bean的新实例 

Myhelper Bean

package  ch5.LookupMethod;

public   class  MyHelper  {
  
public void doSomething(){
     
  }

}


以下是两个singleton bean的实现接口

 

package  ch5.LookupMethod;

public   interface  DemoBean  {
  
public MyHelper getmyHelper();
  
public void someOperation();
}

 

以下代码是StandardLookupDemoBean使用setter方式获得MyHelper bean

 

package  ch5.LookupMethod;

public   class  StandardLookupDemoBean  implements  DemoBean  {
    
private MyHelper myHelper;
    
public void setMyHelper(MyHelper myHelper) {
        
this.myHelper = myHelper;
    }

    
public MyHelper getmyHelper() {    
        
return this.myHelper;
    }

    
public void someOperation() {
        myHelper.doSomething();
    }


}


以下是使用Lookup Method的abstract class

之所以定义成抽象类,后面会有论述

package  ch5.LookupMethod;

public   abstract   class  AbstractLoopipMethodDemoBean  implements  DemoBean  {

    
    
public abstract MyHelper getmyHelper();
    
public void someOperation() {
        getmyHelper().doSomething();
    }


}

 

配置文件:

 

<? 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-2.0.xsd" >


< bean  id ="helper"  class ="ch5.LookupMethod.MyHelper"  scope ="prototype" >   <!--  spring2.0使用scope="singleton/prototype代替singleton="true/false"  -->
</ bean >

< bean  id ="standardBean"  class ="ch5.LookupMethod.StandardLookupDemoBean" >
 
< property  name ="myHelper" >
   
< ref  local ="helper" />
 
</ property >
</ bean >

< bean  id ="abstractBean"  class ="ch5.LookupMethod.AbstractLoopipMethodDemoBean" >
  
< lookup-method  name ="getmyHelper"  bean ="helper" />
</ bean >


</ beans >

 

测试代码:

 

package  ch5.LookupMethod;

import  java.io.File;

import  org.springframework.beans.factory.BeanFactory;
import  org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import  org.springframework.beans.factory.xml.XmlBeanFactory;
import  org.springframework.core.io.FileSystemResource;
import  org.springframework.util.StopWatch;

public   class  TestSpring {
  
public static void main(String args[])  throws Exception{
      
//获取bean factory
      ConfigurableListableBeanFactory factory=(ConfigurableListableBeanFactory)getBeanFactory();  //使用子beanFactory
     
     
      DemoBean standardBean
=(DemoBean)factory.getBean("standardBean");
      DemoBean abstractBean
=(DemoBean)factory.getBean("abstractBean");

      displayInfo(standardBean);
      displayInfo(abstractBean);
  }

  
public static BeanFactory getBeanFactory(){
      
//获取bean factory
      String realpath="";
   
         
//加载配置项
      realpath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"ch5/LookupMethod"+File.separator+"applicationContext.xml";
  
     
      ConfigurableListableBeanFactory  factory
=new XmlBeanFactory(new FileSystemResource(realpath));
      
      
return factory;
  }

  
  
public static void displayInfo(DemoBean bean){
      MyHelper helper1
=bean.getmyHelper();
      MyHelper helper2
=bean.getmyHelper();
      System.out.println(
"Helper instance the same:"+(helper1==helper2));
      
      StopWatch stopWatch
=new StopWatch();
      stopWatch.start(
"TestSpring");
      
for (int i = 0; i < 1000000; i++{
          MyHelper helper
=bean.getmyHelper();
          helper.doSomething();
      }

      stopWatch.stop();
      System.out.println(stopWatch.getTotalTimeMillis());
  }

}

 

结果:

Helper instance the same:true
16
Helper instance the same:false
3594

结果说明:

对于StandardLookupDemoBean,单个MyHelper实例通过setter注入,且这个实例保存下来,所以为true
对于AbstractLoopipMethodDemoBean,每次调用getMyHelper()时候都会返回新实例,所以为false
StopWatch是spring提供的一个测试程序效率的工具类

注意事项:

1.应用场合:需要处理两个生命周期不同的bean时候使用
2.假定你有三个singleton,公用一个依赖,希望每个singleton有自己的实例依赖,所以将依赖设置成singleton,但
   实际上每个singleton在其生命周期中使用协作着的同一个实例足以,这种情况下,setter是最好的选择,使用方
   法查找注入会增加不必要的开销
3.我们也可以使用实现BeanFactoryAware的方式代替Lookup Method Injection

    public void setBeanFactory(Beanfactory factory){
         this.factory=factory;
   }
   public MyHelper getMyHelper(){
      return (MyHelper)factory.getBean("helper");
   }

   使用上述方法在效率上和使用本例的方法,相差无机(100000次差别在几毫秒),所以,建议使用本里的方法,减少和spring api的耦合

4.尽管可以不把查找方法定义成abstract,但还是建议这样做,可以防止不小心忘了配置查找方法而留一个空方法在代码中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Master Spring basics and core topics, and share the authors’ insights and real–world experiences with remoting, Hibernate, and EJB. Beyond the basics, you’ll learn how to leverage the Spring Framework to build the various tiers and parts of an enterprise Java application: transactions, web and presentation tiers, deployment, and much more. A full sample application allows you to apply many of the technologies and techniques covered in Pro Spring 5 and see how they work together. This book updates the perennial bestseller with the latest that the new Spring Framework 5 has to offer. Now in its fifth edition, this popular title is by far the most comprehensive and definitive treatment of Spring available. It covers the new functional web framework and interoperability with Java 9. After reading this definitive book, you’ll be armed with the power of Spring to build complex Spring applications, top to bottom. The agile, lightweight, open-source Spring Framework continues to be the de facto leading enterprise Java application development framework for today’s Java programmers and developers. It works with other leading open-source, agile, and lightweight Java technologies such as Hibernate, Groovy, MyBatis, and more. Spring now works with Java EE and JPA 2 as well. What You’ll Learn Discover what’s new in Spring Framework 5 Use the Spring Framework with Java 9 Master data access and transactions Work with the new functional web framework Create microservices and other web services Who This Book Is For Experienced Java and enterprise Java developers and programmers. Some experience with Spring highly recommended.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值