Results 1 to 4 of 4

Thread: lookup-method Problem

  1. #1

    Unhappy 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. #2
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,840

    Default

    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.

  3. #3

    Default 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?

  4. #4
    Join Date
    Oct 2005
    Location
    Boston, MA
    Posts
    2,840

    Default

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •