Spring lookup-method

来源:互联网 发布:苹果付费画图软件 编辑:程序博客网 时间:2024/06/01 13:45

先看一下这段代码有什么问题?

public class WhlCarrier {}

public  class HjlCarrier {    private  WhlCarrier whlCarrier;    public void setWhlCarrier(WhlCarrier whlCarrier){        this.whlCarrier=whlCarrier;    }    public WhlCarrier getWhlCarrier() {        return whlCarrier;    }}

 <bean id="whl" class="com.vo.collection.WhlCarrier" scope="prototype">    </bean>    <bean id="hjl" class="com.vo.collection.HjlCarrier">        <property name="whlCarrier" ref="whl"/>    </bean>

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration({"classpath:beans.xml"})public class TestLookup {    @Autowired    HjlCarrier hjlCarrier;    @Test    public void test() {        System.out.println(hjlCarrier.getWhlCarrier() == hjlCarrier.getWhlCarrier());    }}
运行的结果为true ,从配置文件可以看出我们把WhlCarrier配置成原型模式,HjlCarrier配置成单例模式。有时我们会很粗心的认为结果会返回false,因为我们把WhlCarrier设置成了原型模式,每次返回都不是同一个实例了,但别忘记了我们把HjlCarrier设置成了单例,也就是说HjlCarrier只出被实例化一次,里面的依赖也只会被设置一次,无法实时更新。要解决这个问题一般有两种方法,一是直接得到spring容器并主动向容器请求依赖的bean,这样做到实时更新,不好的地方就是有侵入式设计,不推荐。二是使用
lookup-method。下面我们来重写一下上面的代码:

public class WhlCarrier {}
public abstract class HjlCarrier {    private WhlCarrier whlCarrier;    public abstract WhlCarrier getEntity();    public WhlCarrier getWhlCarrier() {        return getEntity();    }}

    <bean id="whl" class="com.vo.collection.WhlCarrier" scope="prototype">    </bean>    <bean id="hjl" class="com.vo.collection.HjlCarrier">        <lookup-method name="getEntity" bean="whl"/>    </bean>

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration({"classpath:beans.xml"})public class TestLookup {    @Autowired    HjlCarrier hjlCarrier;    @Test    public void test() {        System.out.println(hjlCarrier.getWhlCarrier() == hjlCarrier.getWhlCarrier());    }    }

执行单元测试结果为false  ,lookup 方法注入,Spring通过使用CGLIB库修改客户端的二进制码,从而实现我们在单例依赖原型时得到实时的实例。一般单例依赖单例,原型依赖单例不会有这样的问题。好了就这样吧,收工,跑步去。


0 0
原创粉丝点击