首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

一个异常org.springframework.beans.factory.BeanNotOfRequiredTypeException

2013-04-02 
一个错误org.springframework.beans.factory.BeanNotOfRequiredTypeException在做SSH整合的时候,把一个dao

一个错误org.springframework.beans.factory.BeanNotOfRequiredTypeException
在做SSH整合的时候,把一个dao注入到action中,但是从ApplicationContext中getBean的时候就报错了
代码如下:
Action

@Component("listAction")
@Scope("prototype")
public class ListAction extends ActionSupport {
    //@Autowired
    //@Qualifier("customerDAO")
    private CustomerDAO customerDAO;

    public String demo01() {
        HttpServletResponse response = ServletActionContext.getResponse();
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        response.setContentType("text/xml;charset=UTF-8");
        String str = "<root><success>true</success><totalCount>1158</totalCount><row><id>1</id><name>peter01</name><email>xxx@163.com</email></row><row><id>2</id><name>peter02</name><email>xxx@163.com</email></row></root>";

        out.write(str);
        List<Customer> list = customerDAO.listAll();
        System.out.println(list.size());

        return null;
    }


    public CustomerDAO getCustomerDAO() {
        return customerDAO;
    }

    @Resource(name = "customerDAO")
    public void setCustomerDAO(CustomerDAO customerDAO) {
        this.customerDAO = customerDAO;
    }
}


其中CustomerDAO 是一个接口,但是写如下的测试代码的时候报错了


 public void testSave01() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext_new.xml");
       
        ListAction listAction=ac.getBean("listAction",ListAction.class);

    }



错误信息:
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'listAction' must be of type [com.zq.demo.web.action.ListAction], but was actually of type [$Proxy16]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:361)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1121)
at com.zq.demo.test.Test02.testSave01(Test02.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)


at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.junit.runner.JUnitCore.run(JUnitCore.java:130)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:76)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)



请问大神们,这是什么原因呀?怎么解决。 spring struts
[解决办法]
你注入的是action,不是service?

listAction这个的bean配置看下。

错误是类型错误。
[解决办法]
 <action name="listAction" class="com.zq.demo.web.action.ListAction">大写,有包名,也要带上
[解决办法]
getbean()咋个变成 action咯。里面不是更bean 的id吗?求指教
[解决办法]
对应的action的bean的配置文件看下。

热点排行