【SSM】No qualifying bean of type

one_two_three_ 2017-10-20 02:59:00
异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.how2java.service.CsHomeworkService com.how2java.controller.TestController.csHomeworkService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.how2java.service.CsHomeworkService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
代码:

package com.how2java.controller;

import java.util.List;

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


import com.how2java.pojo.CsHomework;
import com.how2java.service.CsHomeworkService;

@Controller
@RequestMapping("")
public class TestController {

@Autowired
CsHomeworkService csHomeworkService;

@RequestMapping("dachuang")
public ModelAndView show(){
ModelAndView mav = new ModelAndView();
List<CsHomework> cs= csHomeworkService.list();

// 鏀惧叆杞彂鍙傛暟
mav.addObject("cs", cs);
// 鏀惧叆jsp璺緞
mav.setViewName("listCsHomework");
return mav;

}
}

applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:annotation-config/>
<context:component-scan base-package="com.how2java.service"/>

<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/python?characterEncoding=UTF-8</value>

</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>***********</value>
</property>
</bean>

<!-- 扫描存放SQL语句的CsHomework.xml -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.how2java.pojo" />
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:com/how2java/mapper/*.xml"/>
</bean>

<!-- 扫描Mapper,并将其生命周期纳入Spring的管理 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.how2java.mapper"/>
</bean>

</beans>


springMVC.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<!-- 扫描Controller,并将其生命周期纳入Spring管理 -->
<context:annotation-config/>

<context:component-scan base-package="com.how2java.controller">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<!-- 注解驱动,以使得访问路径与方法的匹配可以通过注解的方式配置 -->
<mvc:annotation-driven />
<!-- 静态页面,如html,css,js,images可以访问 -->
<mvc:default-servlet-handler />

<!-- 视图定位到/WEB/INF/jsp 这个目录下 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 在WEB-INF目录下新增加web.xml,这个web.xml有两个作用:
1. 通过ContextLoaderListener在web app启动的时候,获取contextConfigLocation配置文件的文件名applicationContext.xml,并进行Spring相关初始化工作

2. 有任何访问,都被DispatcherServlet所拦截,这就是Spring MVC那套工作机制了。 -->
<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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

<!-- spring的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- spring mvc核心:分发servlet -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- spring mvc的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>



@Service
public class CsHomeworkServiceImpl implements CsHomeworkService{

@Autowired
CsHomeworkMapper cs;

@Override
public List<CsHomework> list() {
return cs.list();
}

}


@Controller
@RequestMapping("")
public class TestController {

@Autowired
CsHomeworkService csHomeworkService;

@RequestMapping("dachuang")
public ModelAndView show(){
ModelAndView mav = new ModelAndView();
List<CsHomework> cs= csHomeworkService.list();

// 鏀惧叆杞彂鍙傛暟
mav.addObject("cs", cs);
// 鏀惧叆jsp璺緞
mav.setViewName("listCsHomework");
return mav;

}
}


...全文
2619 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
one_two_three_ 2017-10-21
  • 打赏
  • 举报
回复
oo 我发现了,因为我的service接口的包名是com.how2java.service 而实现类所在的包名是com.how2java.serviceimpl 所以无法实现 @Autowired CsHomeworkService cHomeworkService; 正确的做法是,将实现类的报名改成com.how2java.service.impl 实现类和接口都在com.how2java.service文件下,才能自动注入(应该是吧。
one_two_three_ 2017-10-21
  • 打赏
  • 举报
回复
求大神帮我看下是哪里出错了!!!感激!!

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧