0

i have requirement to replace private method of spring bean, can i achieve through spring replace.

My Code : Replacer Class :

public class PrivateCarRep extends Car implements MethodReplacer{

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

    // new property of Car.breaks() method.
    System.out.println("New  privateBreaksIs Done from Shiv");

    return obj;
}

}

Car.java

package org.websparrow.beans;

public class Car {
private void privateBreaks() {
    System.out.println("Old car break. privateBreaks");
}
}

My Spring Configuration:

<bean id="PrivateCarRep" class="org.websparrow.beans.PrivateCarRep"/>

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

Dear All, i already know that i can't replace private method through spring replacer but is there any workaround for this in spring..

2 Answers2

0

You need to define PrivateCarRep as a bean:

<bean id="privateCarReplacer" class="com.xx.yy.zz.PrivateCarRep" />

<bean id="car" class="org.websparrow.beans.Car">
    <replaced-method name="privateBreaks" replacer="privateCarReplacer" />
</bean>
Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51
  • that i have already define still its not working. I am asking to change the access modifier of method from private to public so i can replace the method through method replacer – Shiv Raghuwanshi Sep 01 '18 at 16:53
  • Why would you want to do this? I think you may need to use AspectJ to intercept the calls to the private method, and call your replacement, instead... This might help: https://stackoverflow.com/questions/15093894/aspectj-pointcut-for-annotated-private-methods – moilejter Sep 01 '18 at 17:14
0

I'm afraid you can't do that,I thing the method should be be protected or public.

CRISTIAN ROMERO MATESANZ
  • 1,502
  • 3
  • 14
  • 26