想要了解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();
}