Spring查找方法和抽象类

时间:2015-01-12 03:06:36

标签: java spring

想要了解Spring如何从抽象类创建bean,就像它实例化一个Abstract类一样,因为众所周知这是不可能的。想知道,什么是实例化" abstractLookupBean"豆。

谢谢。

public abstract class AbstractLookupDemoBean implements DemoBean {

    public abstract MyHelper getMyHelper();

    public void someOperation() {
        getMyHelper().doSomethingHelpful();
    }
}

DemoBean类

public interface DemoBean {
    public MyHelper getMyHelper();

    public void someOperation();
}

的applicationContext.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.1.xsd">

    <bean id="helper" class="com.rami.methodlookup.helper.MyHelper" scope="prototype"/>     

    <bean id="abstractLookupBean" class="com.rami.methodlookup.helper.AbstractLookupDemoBean">         
        <lookup-method name="getMyHelper" bean="helper"/>     
    </bean>   

    <bean id="standardLookupBean" class="com.rami.methodlookup.helper.StandardLookupDemoBean">         
        <property name="myHelper">             
            <ref local="helper"/>         
        </property>     
    </bean> 
</beans>

主要班级

    public static void main(String[] args) {
    GenericXmlApplicationContext ctx = null; 
    try{
        ctx = new GenericXmlApplicationContext();
        ctx.load("applicationContext.xml");
        ctx.refresh();
        DemoBean abstractBean = (DemoBean) ctx.getBean("abstractLookupBean"); //What is getting instantiated?
    }finally{
        ctx.close();
    }

1 个答案:

答案 0 :(得分:0)

请参阅spring文档here

  

...如果方法是抽象的,动态生成的子类将实现该方法。否则,动态生成的子类将覆盖原始类中定义的具体方法...