无法为HomeController注入服务

时间:2015-06-29 11:59:49

标签: java spring spring-mvc dependency-injection controller

我正在尝试向homeController注入一个服务,我收到此错误: servlet mvc-dispatcher的Servlet.init()引发了异常。

我猜appContext.xml没有加载?

这是我的文件

HomeController中:

package com.springapp.mvc;
import interfaces.SaleRequestService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class HelloController {

private SaleRequestService _saleRequestService;

public HelloController(SaleRequestService saleRequestService){
    _saleRequestService = saleRequestService;
}

@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
    model.addAttribute("message", "Hello world!");
    return "hello";
}
}

的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.xsd   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

   <bean id="saleRequestService" class="services.SaleRequestServiceImpl"/>

   <bean class="com.springapp.mvc.HelloController">
          <constructor-arg index="0" ref="saleRequestService"/>
   </bean>       

的web.xml

<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring MVC Application</display-name>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

堆栈跟踪:

  

org.springframework.beans.factory.BeanCreationException:创建名为&#39; helloController&#39;的bean时出错在文件[C:\ Dev \ hera \ hera.web \ target \ hera.web \ WEB-INF \ classes \ com \ springapp \ mvc \ HelloController.class]中定义:bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[com.springapp.mvc.HelloController]:找不到默认构造函数;嵌套异常是java.lang.NoSuchMethodException:com.springapp.mvc.HelloController。()

0 个答案:

没有答案