在做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)