St.Antario
St.Antario

Reputation: 27455

Bean definition and name-lookup

I'm trying to experen=ment with spring's name lookup:

<bean id="test" class="com.badmitrii.TestBean" />

<bean id="anotherTest" class="com.badmitrii.AnotherTestBean">
    <lookup-method bean="test" name="getString"/>
</bean>

public class TestBean {

    private String testBean = "Test bean";

    //GET, SET

    public String getString(){
        return "String";
    }
}

public class AnotherTestBean {

    private String testBean = "Another test bean";

    //GET, SET

    public String getString(){
        return "Overriden string";
    }
}

But when I'm trying to run the application I got the following exception:

Exception in thread "main" java.lang.ClassCastException: com.pac.TestBean cannot be cast to java.lang.String
    at com.pac.AnotherTestBean$$EnhancerBySpringCGLIB$$d6d0f4c6.getString(<generated>)

on the line:

System.out.println(((AnotherTestBean) context.getBean("anotherTest")).getString());

What's wrong with that?

Upvotes: 0

Views: 67

Answers (1)

JB Nizet
JB Nizet

Reputation: 692231

You're saying that the bean "anotherTest" is of type com.badmitrii.AnotherTestBean, and is created by calling the method getString() of the bean test.

But this method doesn't return an instance of com.badmitrii.AnotherTestBean. It returns a String. Hence the exception.

Upvotes: 1

Related Questions