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. Without modifying the source code of an existing method we can replace the logic inside the method using method replacement so, that we can easily switch back to the original method without rebuilding and redeploying and testing the application in case if the new logic is failing.

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

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


package com.sjg.beans;

import java.lang.reflect.Method;

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;



package com.sjg.beans;

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



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="";

    <bean id="planFinder" class="com.sjg.beans.PlanFinder">
        <replaced-method name="findPlans" replacer="findPlansReplacer" />
    <bean id="findPlansReplacer" class="com.sjg.beans.FindPlansReplacer" />


<?xml version="1.0" encoding="UTF-8"?>

<project xmlns=""; xmlns:xsi="";


    <!-- FIXME change it to the project's website -->


package com.sjg.test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
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) {



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

About author

User Image

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.



Be the first person to write a comment for this Blog
  • author image
      13 Oct, 2018 22:02

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

  • author image
      13 Oct, 2018 20:55

    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
      12 Oct, 2018 15:30

    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.