aspose file tools*
The moose likes Spring and the fly likes Method Replacer concept in Spring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Method Replacer concept in Spring" Watch "Method Replacer concept in Spring" New topic
Author

Method Replacer concept in Spring

Debabrata Patnaik
Greenhorn

Joined: Oct 27, 2009
Posts: 5
Hi All, I was trying to figure out how we can control message replacement in Spring. It seems little complicated to me. Let me know if anyone can throw light on the following.

Coding is done based on the materials suggested in Appress Pro Spring book.


ReplacementTarget.java

package com.doogle.spring.chapter4.mi;

/**
* @author DOOGLE
*
*/
public class ReplacementTarget {
public int formatMessage(int a)
{
return 100000 + a + 100000;
}
public String formatMessage(String msg)
{
return "<h4>" + msg + "</h4>";
}
}
FormatMessageReplacer.java

package com.doogle.spring.chapter4.mi;

import java.lang.reflect.Method;

import org.springframework.beans.factory.support.MethodReplacer;

/**
* @author DOOGLE
*
*/
public class FormatMessageReplacer implements MethodReplacer{
public Object reimplement (Object arg0,Method method,Object [] args) throws Throwable
{
if(method.getReturnType() == int.class)
{
return (int)(Integer)args[0];
}
else if (method.getReturnType() == String.class)
{
return (String)args[0];
}
else
return "NA";
/*
As expected, the output from the replacementTarget bean reflects the overridden implementation
that the MethodReplacer provides. Interestingly, though, the dynamically replaced method is more than
three times slower than the statically defined method. Removing the check for a valid method in the
MethodReplacer made a negligible difference across a number of executions, so we can conclude that
most of the overhead is in the CGLIB subclass

*/
}
}
replacement.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

<bean id="methodReplacer" class="com.doogle.spring.chapter4.mi.FormatMessageReplacer"/>

<bean id="replacementTarget" class="com.doogle.spring.chapter4.mi.ReplacementTarget">
<replaced-method name="formatMessage" replacer="methodReplacer">
<arg-type>int</arg-type>
</replaced-method>
</bean>

<bean id="standardTarget" class="com.doogle.spring.chapter4.mi.ReplacementTarget"/>

</beans>

MethodReplacementExample.java

/**
*
*/
package com.doogle.spring.chapter4.mi;

import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.util.StopWatch;

/**
* @author DOOGLE
*
*/
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 replacementTarget)
{
System.out.println(replacementTarget.formatMessage(2));
System.out.println(replacementTarget.formatMessage("2"));
// System.out.println(replacementTarget.formatMessage("Formating message and checking for message replacement"));
StopWatch stopWatch = new StopWatch();
stopWatch.start("perfTesting");
// Object obj = new Object();
for(int i=0;i<1000000 ; i++)
{
String out = replacementTarget.formatMessage("foo-bar");
}
stopWatch.stop();
System.out.println("1000000 invocations took: " + stopWatch.getTotalTimeMillis()+ " ms");
}
}

Based on my understanding the replacement should work on when the argument type is int. Not when we have the argument type as String.
I tried changing the same in configuration file. Once you write <arg-type> the irrespective of the value you have the method getting replaced.
I gave the value asd for the arg-type still the method got replaced. Can anyone explain how to restrict method replacement in spring.


Regards
Debabrabrta


if you think you can do
then only you can do!!!
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1666
    
    7

Please edit your post and UseCodeTags.


[How To Ask Questions][Read before you PM me]
Debabrata Patnaik
Greenhorn

Joined: Oct 27, 2009
Posts: 5
Hi All, I was trying to figure out how we can control message replacement in Spring. It seems little complicated to me. Let me know if anyone can throw light on the following.

Coding is done based on the materials suggested in Appress Pro Spring book.


ReplacementTarget.java


FormatMessageReplacer.java


replacement.xml


MethodReplacementExample.java



Based on my understanding the replacement should work on when the argument type is int. Not when we have the argument type as String.
I tried changing the same in configuration file. Once you write <arg-type> the irrespective of the value you have the method getting replaced.
I gave the value asd for the arg-type still the method got replaced. Can anyone explain how to restrict method replacement in spring.


Regards
Debabrabrta
Debabrata Patnaik
Greenhorn

Joined: Oct 27, 2009
Posts: 5
Let me know if any one can throw light on the above metioned method replacer property.

Regards
Debabrata
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1666
    
    7

A person does not often find the need to use this. If what you say is true, I think it might be a bug. I think it should be restricting your parameters to the type(s) specified. That aside you can do the same thing with AspectJ and possibly get better performance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method Replacer concept in Spring