• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method Replacer concept in Spring

 
Debabrata Patnaik
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please edit your post and UseCodeTags.
 
Debabrata Patnaik
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me know if any one can throw light on the above metioned method replacer property.

Regards
Debabrata
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic