mmedeed 2017-02-08 03:18 采纳率: 0%
浏览 1066

Spring中使用lookup-method导致AbstractMethodError

最近在学习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

麻烦大神帮忙看一下问题究竟出在哪?

  • 写回答

1条回答 默认 最新

  • mmedeed 2017-02-08 05:52
    关注

    问题解决了,虽然不太清楚具体原理,希望有大神能给解释一下。
    修改如下:
    首先针对抽象类:

     public abstract class LookupService {
        private SubService subService;
        protected abstract SubService getSubService(); 
    
        public int getHp() {
            subService.printOK();
            return subService.getHp();
        }
    
        public void sayHello() {
            getSubService().printOK();
        }
    
        public void setSubService(SubService subService) {
            this.subService = subService;
        }
    }
    

    这里增加了subService的set方法,不再把抽象方法的返回值赋给私有对象。
    相应的在xml配置文件中添加了subService的注入

        <bean id="lookupService" class="com.guang.webtest.service.LookupService">
        <property name="subService" ref="subService"></property>
            <lookup-method name="getSubService" bean="subService" />
        </bean>
    

    这样就不会报错,而且在测试方法中多次调用getSubService()方法会返回不同的SubService对象

    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容