为什么@Autowired不能在我的Spring MVC @Controller中工作?

时间:2018-04-04 04:42:28

标签: spring spring-mvc spring-ioc

我有一个Spring MVC项目。

我有一个具有@Autowired服务的WebController。

当我在服务器上运行Web应用程序时,会创建WebController,但它不会自动装配该服务。

真的把我的头碰到墙上试图解决这个问题......任何想法是什么?

这是我得到的错误......

org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'webController': 
Unsatisfied dependency expressed through field 'myAppService'; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 'com.mycompany.myapp.controller.MyAppService'     
available: expected at least 1 bean which qualifies as 
autowire candidate. Dependency annotations:     
{@org.springframework.beans.factory.annotation.Autowired(required=true)}

这是我的web.xml ...

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>Archetype Created Web Application</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

这是我的调度员-servlet.xml ......

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    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
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.mycompany.myapp" />

    <mvc:annotation-driven />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

我有一个像这样的WebController ......

package com.mycompany.myapp.controller;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.mycompany.myapp.model.TableRecord;

@Controller
public class WebController {

    @Autowired
    MyAppService myAppService;

    @RequestMapping(value = "/showprojects", method = RequestMethod.GET)
    public ModelAndView showProjects(Model model) {
        ModelAndView mav = new ModelAndView();

        try {
            Set<TableRecord> recProjects = myAppService.getProjects();
            model.addAttribute("projects", recProjects);
        } catch (Exception x) {
            x.printStackTrace();
        }

        mav.setViewName("projects");
        return mav;

    }

这是我的服务界面......

package com.mycompany.myapp.controller;

import java.util.Set;
import org.springframework.stereotype.Component;
import com.mycompany.myapp.model.TableRecord;

@Component
public interface MyAppService {

    public Set<TableRecord> getProjects() throws Exception;

}

这是我的服务实施......

package com.mycompany.myapp.controller;

import java.util.Set;
import org.springframework.stereotype.Component;
/* ... other import statements ... */

@Component
public class MyAppServiceImpl {

    public MyAppServiceImpl() throws Exception {
        /* ... some initialization of member variables 
        and db connection ... */
    }

    public Set<TableRecord> getProjects() throws Exception {
        /* ... implementation that returns a Set... */
    }

}

2 个答案:

答案 0 :(得分:3)

您没有在服务类中实现该接口 - 请按以下方式更改它 -

public class MyAppServiceImpl implements MyAppService {
}

最好将@Service annoation用于服务类而不是@Component

答案 1 :(得分:2)

您的MyAppServiceImpl课程没有实现MyAppService界面。