关于springMVC注解注入失败的问题

注解都写了,没发现哪有遗漏,个人认为是环境配置的问题,求解答

dao

package dao;

import java.util.List;

import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import entity.Food;

@Repository
public class FoodDao extends HibernateDaoSupport{ 

service

 package Logic;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.FoodDao;
import entity.Food;

@Service
public class FoodLogic {

controller

 import entity.Food;


@Controller
@RequestMapping("")
public class AddFoodController extends AbstractController{

    @RequestMapping("/list")
    public ModelAndView getList(HttpServletRequest request, HttpServletResponse response) {
        List<Food> foodlist = this.foodLogic.selectAllFood();
        response.setCharacterEncoding("UTF-8");
        try {
            PrintWriter write = response.getWriter();
            write.write(getJson(foodlist));
            write.flush();
            write.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return this.listView();
    }

    @ResponseBody
    public String getJson(List list) {

        JSONObject jsonDate = new JSONObject();
        jsonDate.put("list", list);
        return jsonDate.toString();

    }
}


package Controller.foodController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import Logic.FoodLogic;

public class AbstractController {
    @Autowired
    public FoodLogic foodLogic;

    public ModelAndView listView() {
        return new ModelAndView("food/list");
    }

    public ModelAndView editView() {
        return new ModelAndView("food/edit");
    }

    public ModelAndView viewView() {
        return new ModelAndView("food/view");
    }

}


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"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">                    
    <!-- 加载Spring的全局配置文件 -->

    <!-- scan the package and the sub package -->
    <context:component-scan base-package="Controller"/>


    <!-- don't handle the static resource -->
    <mvc:default-servlet-handler />

    <!-- if you use annotation you must configure following setting -->
    <mvc:annotation-driven />

    <!-- configure the InternalResourceViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
            id="internalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/htm/" />
        <!-- 后缀 -->
        <property name="suffix" value=".html" />
    </bean>

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>OrderMealManager</display-name>
<!--   <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list> -->

  <!-- 监听spring上下文容器 -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

   <!--configure the setting of springmvcDispatcherServlet and configure the mapping-->
  <servlet>
      <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <!-- <load-on-startup>1</load-on-startup> -->
  </servlet>

  <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>

错误信息

 八月 08, 2017 12:59:18 下午 org.springframework.context.support.AbstractApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'addFoodController': Unsatisfied dependency expressed through field 'foodLogic': No qualifying bean of type [Logic.FoodLogic] found for dependency [Logic.FoodLogic]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [Logic.FoodLogic] found for dependency [Logic.FoodLogic]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
八月 08, 2017 12:59:18 下午 org.springframework.web.servlet.FrameworkServlet initServletBean
严重: Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'addFoodController': Unsatisfied dependency expressed through field 'foodLogic': No qualifying bean of type [Logic.FoodLogic] found for dependency [Logic.FoodLogic]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [Logic.FoodLogic] found for dependency [Logic.FoodLogic]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

5个回答

Error creating bean with name 'addFoodController'
spring反向注入创建bean实例失败。
看下spring配置文件里面这个bean的路径以及类名是不是完全正确的。
你是采用注解的方式的么?那么这个bean是不是有声明注册成springbean。

还有其他可能就是这个java类没编译成功,所以应用启动的时候classloader没有load到class。


这一行,包名写错了

<context:component-scan base-package="Controller"/>
    package指定到具体的包名,包名写全

尽量controller调service,service调dao

在配置文件中 我看到你只是把Controller的包里的注解注册成了bean, 你的dao包 和Logic包怎么没有扫描? 是我看错了还是楼主没贴? 难道楼主真没扫?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐