使用Spring方法注入协调不同步的bean

 

<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >

< beans >

  
< bean  id ="chinese"  class ="Bean.lookupbean.Chinese" >
    
< property  name ="axe" >
      
< ref  local ="axe" />
    
</ property >
    
<!--  指定lookup注入方法,该方法用来保证每次产生的non-singleton bean都是新的实例,使用bean属性确定lookup方法的返回值  -->
    
< lookup-method  name ="createAxe"  bean ="axe" />
  
</ bean >
  
< bean  id ="axe"  class ="Bean.lookupbean.SteelAxe"  singleton ="false" ></ bean >
</ beans >

      spring bean有两种状态,singleton和non-singleton,区别是singletom在整个生命周期中只实例化一次,由spring进行管理,而non-singleton可以实例化多次,spring不负责进行管理,当有依赖关系存在是,会有这样一个问题,当singleton bean依赖non-singleton时,由于singleton bean只初始化一次,而non-singleton可以初始化多次,这样就会造成不同步的想象,解决办法是使用spring提供的lookup方法注入 

定义接口:

package  Bean.lookupbean;

public   interface  Person  {
    
public Axe getAxe();
    
public void useAxe();
    
public abstract Axe createAxe();
}
 

    


package  Bean.lookupbean;

public   interface  Axe  {
   
public String chop();
}


实现类:

注意,方法注入所需要的方法,必须是abstract的

 

package  Bean.lookupbean;

import  java.util.ArrayList;
import  java.util.HashMap;
import  java.util.HashSet;
import  java.util.List;
import  java.util.Map;
import  java.util.Properties;
import  java.util.Set;

import  Bean.lookupbean.Person;

public   abstract   class  Chinese  implements  Person  {
    
private Axe axe;
    
public void useAxe() {
        axe.chop();
        
    }

    
public Axe getAxe() {
        
return axe;
    }

    
public void setAxe(Axe axe) {
        
this.axe = axe;
    }

    
//方法注入所需要的方法,由spring提供实现
    public abstract Axe createAxe();

  
}



package  Bean.lookupbean;

public   class  SteelAxe  implements  Axe  {
    
private int count=0;
    
public String chop() {
        
        
return "这是一把铁斧子"+ ++count;

    }


}


配置文件;

这里要把axe bean设置成singleton=false否则,由于容器内只有一个目标bean实例,即使采用lookup注入,每次依然返回同一个实例,lookup方法不仅适用于设值注入,也适用于构造注入

<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >

< beans >

  
< bean  id ="chinese"  class ="Bean.lookupbean.Chinese" >
    
< property  name ="axe" >
      
< ref  local ="axe" />
    
</ property >
    
<!--  指定lookup注入方法,该方法用来保证每次产生的non-singleton bean都是新的实例,使用bean属性确定lookup方法的返回值  -->
    
< lookup-method  name ="createAxe"  bean ="axe" />
  
</ bean >
  
< bean  id ="axe"  class ="Bean.lookupbean.SteelAxe"  singleton ="false" ></ bean >
</ beans >

 

测试代码:

 

public   static   void  main(String[] args)  throws  Exception  {
        
        String path
=new Test().getClass().getResource("/").getPath();
        String realpath
=path.substring(1, path.length());
        ApplicationContext context
=new FileSystemXmlApplicationContext(realpath+"/lookupbean.xml");
        System.out.println(
"不使用lookup");
        Person p
=(Person)context.getBean("chinese");
        System.out.println(p.getAxe());
        System.out.println(p.getAxe());
        System.out.println(p.getAxe().chop());
        System.out.println(p.getAxe().chop());
        System.out.println(
"使用lookup");
        System.out.println(p.createAxe());
        System.out.println(p.createAxe());
        System.out.println(p.createAxe().chop());
        System.out.println(p.createAxe().chop());
        
       
       
    }


运行结构:

不使用lookup
Bean.lookupbean.SteelAxe@1e87719
Bean.lookupbean.SteelAxe@1e87719
这是一把铁斧子1
这是一把铁斧子2
使用lookup
Bean.lookupbean.SteelAxe@e32802
Bean.lookupbean.SteelAxe@b23210
这是一把铁斧子1
这是一把铁斧子1

可以看到,不使用lookup,spring返回同一个实例(由于不同步的原因),使用lookup,每次返回一个新的实例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值