Spring+SpringMVC+mybatis框架整合时报错我是这样排查的

我最近在使用ssm框架进行系统搭建,ssm整合系统框架基本上构建ok,我测试去跑通整个流程,看看框架整合是否有问题,

结果在我运行Tomcat之后,控制台报错以下信息

 Error creating bean with name 'testController': Injection of autowired dependencies failed;
 nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ssm.SSM.manager.service.TestService com.ssm.SSM.manager.controller.TestController.testService; 

nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ssm.SSM.manager.service.TestService] 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)}

大致的意思就是没有bean,依赖注入失败,我一开始去直接进行百度,发现信息太多太杂,我都去试了,有加@Service("类名"),有去改@Autowired为@Resource,还有说类名不匹配的,还有包冲突的,依赖传递有问题的,很多解决的方法,但是这个就像一团肉丸子,无从下口。我思考了下,出错误应该在4个地方,代码,依赖关系,spring与springmvc配置文件,web配置文件决定按步骤来进行排错误,首先先解决代码错误排查,因为是一个小流程,代码很简单

TestDao接口

package com.ssm.SSM.manager.dao;

import java.util.Map;


public interface TestDao {
	public void insert(Map map);
}

TestService接口

package com.ssm.SSM.manager.service;


public interface TestService {
	public void insert();
	
}

 TestController控制类

package com.ssm.SSM.manager.controller;

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


import com.ssm.SSM.manager.service.TestService;

@Controller
public class TestController {
	@Autowired
	private TestService testService;
	@RequestMapping("/test")
	public String test() {
		System.out.println("TestController");
		testService.insert();
		return "success";
	}
}

TestServiceImpl接口实现类 

package com.ssm.SSM.manager.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ssm.SSM.manager.dao.TestDao;
import com.ssm.SSM.manager.service.TestService;


@Service
public class TestServiceImpl implements TestService {
	@Autowired
	private TestDao testDao;
	@Override
	public void insert() {
		// TODO Auto-generated method stub
		Map map=new HashMap();
		map.put("name", "zhang3");
		testDao.insert(map);
	}

}

确认过代码没有错误的情况下,我然后去排查了依赖传递关系,检查依赖是否与我的依赖关系图一样,然后发现没有问题,接着我去进行排查spring与springMVC的配置文件,发现了自己改的时候,少改了一个包名,然后配置有问题。附上我改完的配置文件

SpringMVC配置文件

<?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:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		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.0.xsd">

       <context:component-scan base-package="com.ssm.SSM.*" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!-- 启用注解功能 -->
    <context:annotation-config />
    
    <!-- 字符串字符编码转换 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
        <property name="messageConverters">   
	         <list>
	             <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">   
	                <property name="supportedMediaTypes">
	                      <list>
	                          <value>application/json;charset=UTF-8</value>
	                     </list>   
	                </property>   
	             </bean>   
	         </list>   
	    </property>
    </bean>
	
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!--显示数据库异常现象-->
    <!-- <bean id="exceptionResolver"  
        class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
        <property name="exceptionMappings">  
            <props>
                <prop key="java.lang.Exception">error/error</prop>
            </props>  
        </property>  
    </bean> -->

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="UTF-8">
        <property name="maxUploadSize" value="2097152"/>
        <property name="resolveLazily" value="true"/>
    </bean>
</beans>

 Spring配置文件

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

	<context:component-scan base-package="com.ssm.SSM.*" >
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>

	<!-- 加载外部属性配置文件 -->
	<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations" value="classpath:config/jdbc.properties" />
	</bean>
	
	<!-- 配置C3P0数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver}"/>
		<property name="jdbcUrl" value="${jdbc.url}"/>
		<property name="user" value="${jdbc.user}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>
	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	    <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
	    <property name="dataSource" ref="dataSource"></property>
	    <property name="mapperLocations">
	        <list>
	            <value>classpath*:mybatis/mapper-*.xml</value>
	        </list>
	    </property>
	</bean>
	
	<!-- 扫描Mapper映射配置 -->
	<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!--<property name="basePackage" value="com.ssm.SSM.manager.dao,com.ssm.SSM.potal.dao"/>-->
		<property name="basePackage" value="com.ssm.SSM.*.dao"/>
	</bean>

	<!-- 事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception"/>
			<tx:method name="query*" read-only="true"/>
			<tx:method name="get*" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	
	<aop:config>
		<aop:advisor advice-ref="transactionAdvice" pointcut="execution(*  com.ssm.SSM..*Service.*(..))"/>
	</aop:config>		

</beans>

WEB配置文件

<?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_2_5.xsd" id="WebApp_ID" version="2.5">
 <display-name>SSM</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/spring-*.xml</param-value>
    </context-param>
    
    <!-- 监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 字符编码过滤器 -->
    <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

	    
	<filter>  
	    <filter-name>HiddenHttpMethodFilter</filter-name>  
	    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>  
	</filter>
	<filter-mapping>  
	    <filter-name>HiddenHttpMethodFilter</filter-name>  
	    <servlet-name>springmvc</servlet-name>  
	</filter-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:spring/springmvc-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.htm</url-pattern>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    
    <!-- 会话超时时间 -->
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>
    
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值