Mastering MethodReplacer in Spring Boot 3: From Basics to Advanced Overrides

This article explains the legacy MethodReplacer mechanism in Spring Boot 3, demonstrates a step‑by‑step example of replacing a service method with custom beans, shows how to register the override, test it via a controller, and discusses extensions and why @Aspect is now preferred.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Mastering MethodReplacer in Spring Boot 3: From Basics to Advanced Overrides

1. Introduction

MethodReplacer was an early Spring mechanism for method injection, allowing replacement of a bean's method but required cumbersome configuration and only supported full replacement. With Spring AOP, @Aspect provides a more flexible way to add before, after, and around advice, and is now the standard approach.

2. Practical Example

2.1 Business class

@Service
public class UserService {
    public User query(Long id) {
        return new User(id, "默认用户", 33);
    }
}

2.2 Implement MethodReplacer

@Component
public class ReplaceUserService implements MethodReplacer {
    @Override
    public Object reimplement(Object obj, Method method, Object[] args) throws Throwable {
        return new User((Long) args[0], "替换后的名称", 33);
    }
}

2.3 Custom ReplaceOverride

public class UserReplaceOverride extends ReplaceOverride {
    public UserReplaceOverride(String methodName, String methodReplacerBeanName) {
        super(methodName, methodReplacerBeanName);
    }
}

2.4 Register BeanDefinition Override

@Component
public class ReplaceBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        BeanDefinition beanDefinition = beanFactory.getBeanDefinition("userService");
        if (beanDefinition instanceof GenericBeanDefinition beanDef) {
            beanDef.getMethodOverrides().addOverride(
                new UserReplaceOverride("query", "replaceUserService"));
        }
    }
}

2.5 Test Controller

@RestController
@RequestMapping("/users")
public class UserController {
    private final UserService userService;
    public UserController(UserService userService) {
        this.userService = userService;
    }
    @GetMapping("{id}")
    public User query(@PathVariable Long id) {
        return this.userService.query(id);
    }
}

Running the endpoint returns a User object whose name is "替换后的名称", confirming that ReplaceUserService successfully replaced the original method.

2.6 Extension – Conditional Override

By extending ReplaceOverride and overriding isOverloaded(), the replacement can be toggled via a configuration property, e.g., pack.replace.enabled=true.

public class UserReplaceOverride extends ReplaceOverride {
    private Environment environment;
    public UserReplaceOverride(Environment environment, String methodName, String methodReplacerBeanName) {
        super(methodName, methodReplacerBeanName);
        this.environment = environment;
    }
    @Override
    protected boolean isOverloaded() {
        return "true".equals(this.environment.getProperty("pack.replace.enabled"));
    }
}

In practice, using @Aspect is recommended over MethodReplacer for better readability and maintainability.

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.

JavaAOPSpring BootMethodReplacer
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.