Announcement Announcement Module
Collapse
No announcement yet.
lookup-method Problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • lookup-method Problem

    Hello Every Body:

    I had a problem that make I feel impossible to solve with lookup-method:

    I define two class witch have same method name "sayHello":

    Code:
    public class ClassA {
    	public String sayHello(){
    		return "Hello: class A";		
    	}
    
    public class ClassB {
    	public String sayHello(){
    		return "Hello: class B";		
    	}
    }
    }
    Then, in my spring-config.xml I defined:
    Code:
            <bean id="a" class="ClassA">
    	</bean>
    	<bean id="b" class="ClassB">
    		<lookup-method name="sayHello" bean="a"/>
    	</bean>

    When I did my test:
    Code:
    ApplicationContext context = new ClassPathXmlApplicationContext("spring/test.xml");
    		ClassB b = (ClassB)context.getBean("b");
    		System.out.println(b.sayHello());
    that throws a Exception:

    Code:
    java.lang.ClassCastException
    	at ClassB$$EnhancerByCGLIB$$9bd3da22.sayHello(<generated>)
    	at TestSpring.testMethodLookup(TestSpring.java:10)
    I donīt know why? I am using cglib-2.1.2.jar... thatīs a possible problem?

  • #2
    The method you are replacing (the "sayHello" method) returns a String object. However, by using the lookup-method, you are returning an object of type "ClassA" rather than the expected String.

    Comment


    • #3
      thank you

      Thank you Mark, again you helpme to solve my problem.

      I has other problem:

      does the method witch specific by lookup-method always returning a same Object? Let me quote your answer:

      by using the lookup-method, you are returning an object of type "ClassA" rather than the expected String
      so, does method sayHello always return a Object ClassA and nothing more?

      Comment


      • #4
        In the lookup-method element, the "name" attribute is the method to be replaced, and the "bean" attribute is the bean to be looked-up and returned instead.

        In the definition of bean "b" you have:
        <lookup-method name="sayHello" bean="a"/>

        Therefore, whenever b's sayHello() method is called, the object defined as bean a will be returned. The return value of sayHello() is a String whereas bean a is not a String. That is the source of the ClassCastException.

        If you are actually interested in arbitrary method replacement, then you can read about it in the reference doc here: http://static.springframework.org/sp...s.html#d0e1197

        Comment

        Working...
        X