Java Spring查找方法与抽象类
我想了解Spring是如何从抽象类创建bean的,就好像它正在实例化一个抽象类一样,因为众所周知这是不可能的。想知道“abstractLookupBean”bean实例化了什么 多谢各位Java Spring查找方法与抽象类,java,spring,Java,Spring,我想了解Spring是如何从抽象类创建bean的,就好像它正在实例化一个抽象类一样,因为众所周知这是不可能的。想知道“abstractLookupBean”bean实例化了什么 多谢各位 public abstract class AbstractLookupDemoBean implements DemoBean { public abstract MyHelper getMyHelper(); public void someOperation() { g
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>
请参阅spring文档 …如果方法是抽象的,则动态生成的子类将实现该方法。否则,动态生成的子类将重写在原始类中定义的具体方法
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();
}