Blog Image

What is Method Replacement with example.  

Here we are going to explore about spring provided Method Replacement to know how we can work with Method Replacement in our projects.

There are many cases in an application we want to replace the logic inside a method with new logic. To replace the logic we can comment the logic and write the new logic or we can write one more class extending from the existing class and override the method and write the new logic. But in both of this mechanism we have to modify the source code. These mechanisms will take lot of time and cost also.
So, to replace the logic inside a method without modifying the existing source code inside the application spring has provided Method Replacement.

NOTE:- For replacing a method on a bean the method should not be final and should not be static.
Below example show how to use spring provided method replacement to replace a method.

Lets see the example:
Technologies used :-
1) java (1.8).
2) maven (3.5.2).
3) Spring (4.0.0).

Project Structure

Execute below command for creating maven project.
mvn archetype:generate -DgroupId=com.sjg
-DartifactId=example-on-method-replacement
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false

Below is the maven project structure with packages and java src files.

Classes
FindPlansReplacer.java
package com.sjg.beans;

import java.lang.reflect.Method;
import org.springframework.beans.factory.support.MethodReplacer;

public class FindPlansReplacer implements MethodReplacer {

    public Object reimplement(Object target, Method method, Object[] args) throws Throwable {
        if (method.getName().equals("findPlans")) {
            System.out.println("Name : " + method.getName());
            System.out.println("age Group : " + args[0]);
            System.out.println("coverageType : " + args[1]);
            System.out.println("networkType : " + args[2]);

            return new String[] { "jeevan saral" };
        }

        return null;

    }

}
PlanFinder.java
package com.sjg.beans;

public class PlanFinder {
    public String[] findPlans(int ageGroup, int coverageType, int networkType) {
        return new String[] { "Jeevan Anand" };
    }

}
application-context.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.xsd">;

    <bean id="planFinder" class="com.sjg.beans.PlanFinder">
        <replaced-method name="findPlans" replacer="findPlansReplacer" />
    </bean>
    <bean id="findPlansReplacer" class="com.sjg.beans.FindPlansReplacer" />
</beans>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">;
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sjg</groupId>
    <artifactId>example-on-method-replacer</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>example-on-method-replacer</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.srimanjavagroup.com</url>;

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.0.0.RELEASE</version>
        </dependency>
    </dependencies>
</project>
Test.java
package com.sjg.test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import com.sjg.beans.PlanFinder;

public class Test {
    public static void main(String[] args) {
        BeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/sjg/common/application-context.xml"));
        PlanFinder pf = factory.getBean("planFinder", PlanFinder.class);
        String[] plans = pf.findPlans(2, 1, 0);
        System.out.println("Plans Matching : ");
        for (String p : plans) {
            System.out.println(p);
        }

    }

}

Please find attached the example_on_method_replacer.zip as an maven project as a reference. Feel free to pass your comments in the blog.

About author

User Image
Sriman

A software developer on JDK and JEE platform. I am passionate about Java technology and always an explorer and learner in new technologies in Java. I have experience on Open Source technologies like Struts, Spring, Jsf etc.. and even strong knowledge on Integration/Distributed world like Ejb, Web Services and Restful Services.

3

-Comments

Be the first person to write a comment for this Blog
  • author image
    by:
      Avinash
      13-10-2018 10:02:57 PM

    Nice block sriman. Its really helpful to use in the project.

  • author image
    by:
      rajeev4600
      13-10-2018 08:55:45 PM

    i want to know can i get spring jar source code?Can you please provide information from where i can download jars source code.

  • author image
    by:
      ssuraj
      12-10-2018 03:30:05 PM

    it's superb.... can u plz suggest any annotation approach for method replacement ??

Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.