Spring – Method Replacer | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

This image has an empty alt attribute; its file name is image-1.png

Spring Tutorial Index Page: Link

  • This useful for adding patches to existing implementation.
  • If you want to replace any old implementation then by using <replacer-method> tag under <bean> tag you can override old implementations. But you need to create new class and implement MethodReplacer intercace.
  • Example: Bank class have methods like deposit(), withdraw(), and etc. Now if we want to replace deposit() method with new method then we can achieve this using <replacer-method> tag. IOC will load the xml file and create one proxy class and extending Bank class (Ex. class BankProxy extends Bank). and override deposit() method.
  • If you have already existing application with number of classes. Instead of making of changes in that class, instead of building complete application. Once after completion of application or once after making changes in your application again we have to rebuild application. Instead of rebuilding your application you just implement a patch class and you just add that patch class in you xml file and load it in your server. You no need to rebuilding and recompilation the complete code. You can compile single class and you can add that class as a replacer in your xml file and you can keep this class in your server.

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Others… -> Java Project
  • Create MethodReplacer project
  • Right click on project -> Build Path -> Configure Build Path -> Libraries tab -> Add External JARs (Used 2.X jars)
    • commons-logging-X.X.jar
    • spring-beans-X.X.X.jar
    • spring-context-X.X.X.jar
    • spring-core-X.X.X.jar
    • cglib-nodep-X.X.jar
  • * You can find dtd information from spring-beans-X.X.X.jar -> org -> springframework -> beans -> factory -> xml -> spring-beans.dtd (line no 36 & 37)

spring.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
	"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
	<bean id="b" class="com.codeFactory.beans.Bank">
		<replaced-method name="calInterest" replacer="nci" />
	</bean>
	<bean id="nci" class="com.codeFactory.beans.NewCalInterest" />
</beans>

Bank.java

package com.codeFactory.beans;

/**
 * @author code.factory
 *
 */
public class Bank {

	public void deposite() {
		System.out.println("Bank.deposite()");
	}
	
	public void withdraw() {
		System.out.println("Bank.withdraw()");
	}
	
	public void calInterest() {
		System.out.println("Bank.calInterest()");
	}
}

NewCalInterest.java

package com.codeFactory.beans;

import java.lang.reflect.Method;

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

/**
 * @author code.factory
 *
 */
public class NewCalInterest implements MethodReplacer {

	@Override
	public Object reimplement(Object object, Method method, Object[] objects) throws Throwable {
		System.out.println("New Implementation");
		return null;
	}
}

Client.java

package com.codeFactory.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.codeFactory.beans.Bank;

/**
 * @author code.factory
 *
 */
public class Client {
	public static void main(String... args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("com/codeFactory/resources/spring.xml");
		
		Bank b = (Bank) context.getBean("b");
		System.out.println(b.getClass().getCanonicalName());
		b.deposite();
		b.withdraw();
		b.calInterest();
	}
}

Output:

com.codeFactory.beans.Bank$$EnhancerByCGLIB$$4c049dd4
Bank.deposite()
Bank.withdraw()
New Implementation

Leave a comment