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

Why isn't @Autowired working in my Spring MVC @Controller?

我有一个 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... */
    }

}

您的 MyAppServiceImpl class 没有实现 MyAppService 接口。

您没有在您的服务中实现接口class- 更改如下-

public class MyAppServiceImpl implements MyAppService {
}

服务 class 最好使用 @Service 注释而不是 @Component