public Interface MagicBoss{
Car getCar();
}
<bean id="car" class="cn.lovepi.***.Car"
p:brand="紅旗CA72" p:price="2666" scope="prototype"/>
<bean id="magicBoss" class="cn.lovepi.***.MagicBoss">
<lookup-method name="getCar" bean="car"/>
</bean>
這樣Spring將在運行期爲MagicBoss提供動態的接口實現。他的效果就等同於如下的代碼:
public class MagicBossImpl implements MagicBoss,ApplicationContextAware{
private ApplicationContext ctx;
public Car getCar(){
return (Car)ctx.getBean("car");
}
public void setApplicationContext(ApplicationContext ctx){
this.ctx=ctx;
}
}
public class Boss1 implements MagicBoss{
public Car getCar(){
Car car=new Car();
car.setBrand("寶馬");
return car;
}
}
public class Boss2 implements MethodReplacer{
public Object reimplement(Object arg0,Method arg1,Object[] arg2){
Car car=new Car();
car.setBrand("美人豹");
return car;
}
}
<bean id="boss2" class="cn.lovepi.injectfun.Boss2"/>
<bean id="boss1" class="cn.lovepi.injectfun.Boss1"/>
<replaced-method name="getCar" replacer="boss2"></replaced-method>
</bean>