</replaced-method>
</bean>
<bean id="standardTarget" class="com.apress.prospring3.ch4.mi.ReplacementTarget"/>
</beans>
As you can see from Listing 4-65, the MethodReplacer implementation is declared as a bean in the
ApplicationContext. We then used the <replaced-method> tag to replace the formatMessage(String)
method on the replacementTargetBean. The name attribute of the <replaced-method> tag specifies the name
of the method to replace, and the replacer attribute is used to specify the name of the MethodReplacer
bean that we want to replace the method implementation. In cases where there are overloaded methods
such as in the ReplacementTarget class, you can use the <arg-type> tag to specify the method signature to
match. The <arg-type> supports pattern matching, so String is matched to java.lang.String and also to
java.lang.StringBuffer.
Listing 4-66 shows a simple demo application that retrieves both the standardTarget and
replacementTarget beans from the ApplicationContext, executes their formatMessage(String) methods,
and then runs a simple performance test to see which is faster.
Listing 4-66. Method Replacement in Action
package com.apress.prospring3.ch4.mi;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.util.StopWatch;
public class MethodReplacementExample {
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:replacement.xml");
ctx.refresh();
ReplacementTarget replacementTarget = (ReplacementTarget) ctx
.getBean("replacementTarget");
ReplacementTarget standardTarget = (ReplacementTarget) ctx
.getBean("standardTarget");
displayInfo(replacementTarget);
displayInfo(standardTarget);
}
private static void displayInfo(ReplacementTarget target) {
System.out.println(target.formatMessage("Hello World!"));
StopWatch stopWatch = new StopWatch();
stopWatch.start("perfTest");
for (int x = 0; x < 1000000; x++) {
String out = target.formatMessage("foo");
}
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home