abc280479107 2017-08-08 05:24 采纳率: 0%
浏览 2581

关于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条回答

  • Tsui丶 2017-08-08 05:29
    关注

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

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

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)