`
shameant
  • 浏览: 57227 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

spring method-replace

阅读更多

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

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics