注解都写了,没发现哪有遗漏,个人认为是环境配置的问题,求解答
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)}