2

I'm trying to replace a calcInt method in Bank class with a new method in a new Class NewCalcInt.java. But I'm not able to do that. Help me to
solve this. I have added all the JAR's along with cglib-nodep-2.1.3 jar.

This is the following code Bank.java

package beans;

 public class Bank {
     public void deposit() {
         System.out.println("Deposit Method in Bank");
     }
     public void withdraw() {
         System.out.println("Withdraw Method in Bank");
     }
     public void calcInt() {
         System.out.println("CalcInt Method in Bank");
     }
 }

The following program is NewCalcInt.java

NewCalcInt.java

package beans;

import java.lang.reflect.Method;
import org.springframework.beans.factory.support.MethodReplacer;

 public class NewCalcInt implements MethodReplacer {
    public Object reimplement(Object o, Method m, Object[] arg2) throws
    Throwable {

        System.out.println("NewCalcInt class new CalInt imp");
        return o;
    }
}

The following code is spring.xml spring.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-3.0.xsd" >
    <bean id="b" class="beans.Bank">
        <replaced-method name="calcInt" replacer="nc"></replaced-method>
    </bean>
    <bean id="nc" class="beans.NewCalcInt"></bean>
</beans>

The following code is ClientApp.java ClientApp.java

 package beans;

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

 public class ClientApp {
     public static void main(String[] args) {

         ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
         Bank b = (Bank) context.getBean("b");
         b.deposit();
         b.withdraw();
         b.deposit();
     }
 }

I have already add the JARs

Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
  • It seems no problem, can you try to place `` at first? – Mavlarn Aug 04 '16 at 07:25
  • @Mavlarn I tried but no change in the run time. I will show u the errors WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'b' defined in class path resource [spring.xml]: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'b' defined in class path resource [spring.xml]: Instantiation of bean failed; – Shaik Yakhoob Ali Aug 04 '16 at 07:59
  • Caused by: java.lang.ExceptionInInitializerError – Shaik Yakhoob Ali Aug 04 '16 at 08:10
  • Caused by: java.lang.IllegalStateException: Unable to load cache item, Caused by: java.lang.IncompatibleClassChangeError: class org.springframework.cglib.core.DebuggingClassWriter has interface org.springframework.asm.ClassVisitor as super class – Shaik Yakhoob Ali Aug 04 '16 at 08:11
  • So the definition is wrong, check this: http://www.java2s.com/Code/Java/Spring/MethodReplacementExample.htm – Mavlarn Aug 04 '16 at 08:16
  • Error seems to be coming from incompatible cglib, try using cglib 3.x. That might solve your problem. and check http://stackoverflow.com/questions/15758151/class-conflict-when-starting-up-java-project-classmetadatareadingvisitor-has-in – kuhajeyan Aug 04 '16 at 09:11
  • @Mavlarn I used the correct definition. I just don't understand what exact JAR's are needed. did I include any extra JARs from spring directory. tell me what JARs do I need to include in that program – Shaik Yakhoob Ali Aug 04 '16 at 12:04
  • @kuhajeyan I tried both cglib 2.1.3 and 3.1 but no change in the output. tell me what exact JAR's do I need to include in that program. did I include any extra JARs from spring directory – Shaik Yakhoob Ali Aug 04 '16 at 12:06
  • 1
    @kuhajeyan I removed some of the JARs which I included and now its working fine. – Shaik Yakhoob Ali Aug 04 '16 at 12:21
  • good, that. may be you share what you did , that could help someone. – kuhajeyan Aug 04 '16 at 13:13

0 Answers0