Spring 5 method replacement example


In this tutorial, we are going to learn about Spring method replacement. You can replace or override the existing method by implementing MethodReplacer interface. This interface has only one method i.e.

public Object reimplement(Object obj, Method method, Object[] args) throws Throwable;

Suppose you have a class Car and it has many methods.

public class Car {

	public void design() {
		System.out.println("Old car design.");
	}

	public void breaks() {
		System.out.println("Old car break.");
	}
}

And you want to replace or override the breaks() method properties. To do this you can define the new property by overriding the reimplement() method.

Let’s check the complete example.

Spring Beans

Create the Car class which has some methods. In this class, I’m going to override the breaks() method properties without affecting anything else.

Car.java
package org.websparrow.beans;

public class Car {

	public void design() {
		System.out.println("Old car design.");
	}

	public void breaks() {
		System.out.println("Old car break.");
	}
}

Now create another class NewBreak and implements the MethodReplacer interface. Override the reimplement() method and define the new properties of breaks() method.

NewBreak.java
package org.websparrow.beans;

import java.lang.reflect.Method;

import org.springframework.beans.factory.support.MethodReplacer;

public class NewBreak implements MethodReplacer {

	@Override
	public Object reimplement(Object obj, Method method, Object[] args) throws Throwable {

		// new property of Car.breaks() method.
		System.out.println("New car break.");

		return obj;
	}
}

Spring Beans Configuration

In the configuration file, Spring will provide the <replaced-method /> child element of <bean /> element. Pass the name of the method you want to override in name attribute and bean id of corresponding class to the replacer attribute.

config.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="break" class="org.websparrow.beans.NewBreak" />

	<bean id="car" class="org.websparrow.beans.Car">
		<replaced-method name="breaks" replacer="break" />
	</bean>

</beans>

Run it

Load the configuration file and run it.

Test.java
package org.websparrow.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.websparrow.beans.Car;

public class Test {

	public static void main(String[] args) {

		ApplicationContext ap = new ClassPathXmlApplicationContext("config.xml");

		Car car = (Car) ap.getBean("car");
		car.design();
		car.breaks();
	}
}
Output:

You can see that breaks() method properties has been overridden by the new one.

Old car design.
New car break.

Download Source Code: spring5-method-replacement-example


Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.