How to Replace Any Bean Method in Spring 6 Using MethodReplacer

This article demonstrates how to use Spring's MethodReplacer to substitute arbitrary bean methods with custom implementations, covering code preparation, bean registration, handling overloaded methods, and XML configuration, all illustrated with a complete runnable example.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
How to Replace Any Bean Method in Spring 6 Using MethodReplacer

Environment: Spring 6.1.2

1. Introduction

Method replacement allows you to replace any method of a managed bean with your own implementation. Instead of modifying source code, Spring provides a mechanism to intercept the original method and execute custom logic.

2. Practical Example

2.1 Prepare Code

@Component
public class PersonService {
  public String savePerson(Person person) {
    return "save person";
  }
}

The savePerson method will be replaced.

2.2 Replacement Implementation

public class ReplacementSavePerson implements MethodReplacer {
  @Override
  public Object reimplement(Object obj, Method method, Object[] args) throws Throwable {
    System.out.println(Arrays.toString(args));
    System.out.println(obj);
    return "replace save person";
  }
}

This class implements org.springframework.beans.factory.support.MethodReplacer and provides the new method body.

2.3 Register Beans and Define Override

public static void main(String[] args) {
  try (GenericApplicationContext context = new GenericApplicationContext()) {
    context.registerBean("replacementSavePerson", ReplacementSavePerson.class);
    context.registerBean(PersonService.class, bd -> {
      if (bd instanceof RootBeanDefinition root) {
        // specify method to replace
        ReplaceOverride replace = new ReplaceOverride("savePerson", "replacementSavePerson");
        root.getMethodOverrides().addOverride(replace);
      }
    });
    context.refresh();
    PersonService ps = context.getBean(PersonService.class);
    System.out.println(ps.savePerson(new Person()));
  }
}

Running the program prints the bean information and the string replace save person, showing that the original method has been replaced by ReplacementSavePerson.reimplement.

2.4 Overloaded Methods

If the target class contains overloaded methods, you can add type identifiers to the ReplaceOverride to select the correct method, e.g.:

ReplaceOverride replace = new ReplaceOverride("savePerson", "replacementSavePerson");
replace.addTypeIdentifier("com.pack.Person"); // or "Person", "P", "er"

2.5 XML Configuration

<bean id="personService" class="com.pack.PersonService">
  <replaced-method name="savePerson" replacer="replacementComputeValue">
    <arg-type>Person</arg-type>
  </replaced-method>
</bean>

<bean id="replacementSavePerson" class="com.pack.ReplacementSavePerson"/>

XML configuration is straightforward, while annotation‑based replacement requires manual ReplaceOverride registration.

End of article.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentspringBean OverrideMethodReplacer
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.