spring method-replace

通过Spring框架的方法替换功能,实现不修改原始代码的情况下更改魔术箱的内容展示。利用自定义的TigerReplacer类重新实现了MagicBoxImpl类中的getContents方法。

Basic method replacement

 

 

Code:

public interface Performer(){
       public void perform();
}


public class Magician implements Performer {

public Magician() {}

public void perform() throws PerformanceException {

System.out.println(magicWords);

System.out.println("The magic box contains...");

System.out.println(magicBox.getContents());

}

// injected
private MagicBox magicBox;
public void setMagicBox(MagicBox magicBox) {
this.magicBox = magicBox;
}

private String magicWords;
public void setMagicWords(String magicWords) {
this.magicWords = magicWords;
}
}


public class MagicBoxImpl implements MagicBox {

public MagicBoxImpl() {}

public String getContents() {
return new String("A beautiful assistant");
}
}


public class TigerReplacer implements MethodReplacer {

public Object reimplement(Object target, Method method,

Object[] args) throws Throwable {

return "A ferocious tiger";

}

}

 

 XML

<bean id="harry" class="com.springinaction.springidol.Magician">
<property name="magicBox" ref="magicBox" />
<property name="magicWords" value="Bippity boppity boo" />
</bean>
<bean id="magicBox" class="com.springinaction.springidol.MagicBoxImpl" />

<bean id="magicBox" class="com.springinaction.springidol.MagicBoxImpl">
<replaced-method name="getContents" replacer="tigerReplacer" />
</bean>
<bean id="tigerReplacer"
class="com.springinaction.springidol.TigerReplacer" />
 



TigerReplacer implements Spring’s MethodReplacer interface. MethodReplacer only requires that the reimplement() method be implemented. reimplement() takes three arguments: the target object that the method will be replaced on, the method that will be replaced, and the arguments passed to the method. In our case, we won’t be using those parameters, but they’re available if you need them. The body of the reimplement() method effectively becomes the new implementation for the magic box’s getContents() method. For our example, the only thing we want to do is return “A ferocious tiger.” Effectively, the contents of the box are replaced with a tiger, Now when we invoke the magician’s perform() method, the following is printed to the console:

 

Bippity boppity boo

The magic box contains...

A ferocious tiger

Ta-da! The beautiful assistant has been replaced by a ferocious tiger—without changing the actual MagicBoxImpl code. The magic trick has been successfully performed with the help of Spring’s .It’s worth noting here that although MagicBoxImpl has a concrete implementation of the getContents() method, it would’ve also been possible for getContents() to have been written as an abstract method. In fact, method injection is a trick that is useful when the actual implementation of the method isn’t known until deployment time. At that time, the actual method replacer class can be provided in a JAR file placed in the application’s classpath.

 

Reference:Spring in action

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值