最近在学习Spring,对照着网上给的demo,自己写了一个lookup-method的例子,结果总是报错,从网上也找不到类似的问题。
首先是抽象类
public abstract class LookupService {
private SubService subService = getSubService();
protected abstract SubService getSubService();
public int getHp() {
return subService.getHp();
}
}
然后是要注入的bean的接口
public interface SubService {
public void printOK();
public int getHp();
}
下面是实现类
public class SubServiceImpl implements SubService{
@Override
public void printOK() {
System.out.println("ok");
}
@Override
public int getHp() {
return (int) (Math.random()*100);
}
}
接下来是xml配置文件
<bean id="subService" class="com.guang.webtest.service.impl.SubServiceImpl"
scope="prototype">
</bean>
<bean id="lookupService" class="com.guang.webtest.service.LookupService">
<lookup-method name="getSubService" bean="subService" />
</bean>
这里用getSubService()方法作为lookup-method
最后就是测试,结果就报错了,错误信息如下
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lookupService' defined in class path resource [spring-mvc.xml]: Instantiation of bean failed; nested exception is java.lang.AbstractMethodError: com.guang.webtest.service.LookupService.getSubService()Lcom/guang/webtest/service/SubService;
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1099)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1044)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.guang.webtest.service.TestSystemService.main(TestSystemService.java:23)
Caused by: java.lang.AbstractMethodError: com.guang.webtest.service.LookupService.getSubService()Lcom/guang/webtest/service/SubService;
at com.guang.webtest.service.LookupService$$EnhancerBySpringCGLIB$$b2255b3a.getSubService(<generated>)
at com.guang.webtest.service.LookupService.<init>(LookupService.java:4)
at com.guang.webtest.service.LookupService$$EnhancerBySpringCGLIB$$b2255b3a.<init>(<generated>)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at java.lang.Class.newInstance(Class.java:374)
at org.springframework.beans.BeanUtils.instantiate(BeanUtils.java:78)
at org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy$CglibSubclassCreator.instantiate(CglibSubclassingInstantiationStrategy.java:115)
at org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy.instantiateWithMethodInjection(CglibSubclassingInstantiationStrategy.java:80)
at org.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy.instantiateWithMethodInjection(CglibSubclassingInstantiationStrategy.java:72)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:93)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1092)
... 13 more
麻烦大神帮忙看一下问题究竟出在哪?