spring+mybatis之使用dao接口实现类的相关测试。

用过注解的朋友应该都知道,dao接口不用写实现类,spring会自动给生成实现类,今天我突然想到写上实现类,项目还能不能运行,经过一上午的测试,发现还是没有解决,为了记录,写下了此篇博文。

先贴出正常的配置:

spring-mvc.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:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"
	default-lazy-init="true">

	<description>Spring Configuration</description>
	
    <!-- 加载配置属性文件 -->
	<context:property-placeholder ignore-unresolvable="true" location="classpath:crawler.properties" />
	
	<!-- 使用Annotation自动注册Bean,解决事物失效问题:在主容器中不扫描@Controller注解,在SpringMvc中只扫描@Controller注解。  -->
	<context:component-scan base-package="com.crawler"><!-- base-package 如果多个,用“,”分隔 -->
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
 	<!-- MyBatis begin -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.crawler"/>
        <property name="mapperLocations" value="classpath:/mappings/*.xml"/>
		<property name="configLocation" value="classpath:/mybatis-config.xml"></property>
    </bean>
    
    <!-- 扫描basePackage下所有以@Repository注解的Dao接口,注入sqlSessionFactory -->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <property name="basePackage" value="com.crawler"/>
        <property name="annotationClass" value="org.springframework.stereotype.Repository"/>
    </bean>

    <!-- 定义事务 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 配置 Annotation 驱动,扫描@Transactional注解的类定义事务  -->
	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
    <!-- MyBatis end -->
    
	<!-- 数据源配置, 使用 BoneCP 数据库连接池 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 
	    <!-- 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass -->
	    <property name="driverClassName" value="${jdbc.driver}" />
	    
		<!-- 基本属性 url、user、password -->
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		
		<!-- 配置初始化大小、最小、最大 -->
		<property name="initialSize" value="${jdbc.pool.init}" />
		<property name="minIdle" value="${jdbc.pool.minIdle}" /> 
		<property name="maxActive" value="${jdbc.pool.maxActive}" />
		
		<!-- 配置获取连接等待超时的时间 -->
		<property name="maxWait" value="60000" />
		
		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		
		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
		<property name="minEvictableIdleTimeMillis" value="300000" />
		
		<property name="validationQuery" value="${jdbc.testSql}" />
		<property name="testWhileIdle" value="true" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		
		<!-- 配置监控统计拦截的filters -->
	    <property name="filters" value="stat" /> 
	</bean>
	
	<!-- 使用Annotation自动注册Bean,只扫描@Controller -->
	<context:component-scan base-package="com.crawler" use-default-filters="false"><!-- base-package 如果多个,用“,”分隔 -->
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- 定义视图文件解析 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="${web.view.prefix}"/>
		<property name="suffix" value="${web.view.suffix}"/>
	</bean>
	<!-- 静态资源映射 -->
    <mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"/>
    
    <mvc:annotation-driven/>
</beans>

controller就写贴出来一点,业务逻辑太多,也没什么用处

@Controller
@RequestMapping("/crawler")
public class CrawlerController {
	@Resource(name="crawlerService")
	private ICrawlerService crawlerService;

service接口就不贴了,有实现类接口就知道是什么了

package com.crawler.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

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

import com.crawler.common.utils.StringUtils;
import com.crawler.dao.iface.ICrawlerDao;
import com.crawler.service.iface.ICrawlerService;

/**
 * 爬虫service接口的实现类
 * @author siqiangming 2018年2月27日 下午3:42:32
 */
@Service("crawlerService")
@Transactional
public class CrawlerServiceImpl implements ICrawlerService {
	@Autowired
	private ICrawlerDao crawlerDao;
	
	public void createTable(String tableName, List<String> data) {
		List<String> filedsPinYin = new ArrayList<String>();
		for (int i = 0; i < data.size(); i++) {
			String filed = "";
			try {
				filed = StringUtils.toPinYin(data.get(i));
			} catch (BadHanyuPinyinOutputFormatCombination e) {
				e.printStackTrace();
			}
			filedsPinYin.add(filed);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tableName", tableName);
		map.put("data", filedsPinYin);
		crawlerDao.createTable(map);
	}
	
	public void insert(String tableName, List<List<String>> data) {
		for (int i = 1; i < data.size(); i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			if(data.get(i).size() == data.get(0).size()){
				map.put("tableName", tableName);
				map.put("data", data.get(i));
				crawlerDao.insert(map);
			}
		}
	}

}

dao接口

package com.crawler.dao.iface;

import java.util.Map;
import org.springframework.stereotype.Repository;

/**
 * 爬虫dao接口
 * @author siqiangming 2018年2月27日 下午3:39:48
 */
@Repository("crawlerDao")
public interface ICrawlerDao {
	
	/**
	 * 创建表的dao接口
	 * @author siqiangming 2018年3月1日 上午10:31:37
	 * @param map数据集合
	 */
	void createTable(Map<String, Object> map);
	
	/**
	 * 添加数据的dao接口
	 * @author siqiangming 2018年3月1日 上午10:31:40
	 * @param map数据集合
	 */
	void insert(Map<String, Object> map);
}

mapper.xml(这里要注意,mapper的名称要和dao接口的路径名称一样)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.crawler.dao.iface.ICrawlerDao">

<update id="createTable" parameterType="HashMap">
	create table if not exists ${tableName} 
	<foreach collection="data" item="field" open="(" close=" varchar(50))" separator=" varchar(50),">
		${field}
	</foreach>
</update>

<insert id="insert" parameterType="HashMap">
	insert into ${tableName} values
	<foreach collection="data" item="field" open="(" close=")" separator=",">
		#{field}
	</foreach>
</insert>

</mapper>

运行程序一切正常,操作数据库等没有问题。

接下来就是写dao接口的实现类了。

首先修改dao接口(去掉了注解)

package com.crawler.dao.iface;

import java.util.Map;

/**
 * 爬虫dao接口
 * @author siqiangming 2018年2月27日 下午3:39:48
 */
public interface ICrawlerDao {
	
	/**
	 * 创建表的dao接口
	 * @author siqiangming 2018年3月1日 上午10:31:37
	 * @param map数据集合
	 */
	void createTable(Map<String, Object> map);
	
	/**
	 * 添加数据的dao接口
	 * @author siqiangming 2018年3月1日 上午10:31:40
	 * @param map数据集合
	 */
	void insert(Map<String, Object> map);
}

dao接口实现类,其他的一切没动,

package com.crawler.dao;

import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.crawler.dao.iface.ICrawlerDao;

/**
 * 
 * @author siqiangming 2018年4月18日 上午9:22:00
 */
@Repository("crawlerDao")
public class CrawlerDao extends SqlSessionDaoSupport implements ICrawlerDao {
	
	public void createTable(Map<String, Object> map) {
		getSqlSession().update("com.crawler.dao.iface.ICrawlerDao.createTable", map);
	}

	public void insert(Map<String, Object> map) {
		getSqlSession().insert("com.crawler.dao.iface.ICrawlerDao.insert", map);
	}

}
项目启动还是正常,但是运行起来程序,执行业务之时就会报错
15:31:40.704 [http-nio-8070-exec-5] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'springServlet' processing POST request for [/crawler/crawler/seleniumCrawling]
15:31:40.712 [http-nio-8070-exec-5] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /crawler/seleniumCrawling
15:31:40.718 [http-nio-8070-exec-5] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Returning handler method [public void com.crawler.controller.CrawlerController.seleniumCrawling(javax.servlet.http.HttpServletResponse,com.crawler.pojo.CrawlerInfo) throws java.lang.InterruptedException]
15:31:40.719 [http-nio-8070-exec-5] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'crawlerController'
15:31:40.719 [http-nio-8070-exec-5] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'crawlerController'
15:31:40.728 [http-nio-8070-exec-5] DEBUG org.springframework.beans.factory.annotation.InjectionMetadata - Registered injected element on class [com.crawler.controller.CrawlerController]: ResourceElement for private com.crawler.service.iface.ICrawlerService com.crawler.controller.CrawlerController.crawlerService
15:31:40.728 [http-nio-8070-exec-5] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'crawlerController' to allow for resolving potential circular references
15:31:40.732 [http-nio-8070-exec-5] DEBUG org.springframework.beans.factory.annotation.InjectionMetadata - Processing injected element of bean 'crawlerController': ResourceElement for private com.crawler.service.iface.ICrawlerService com.crawler.controller.CrawlerController.crawlerService
15:31:40.732 [http-nio-8070-exec-5] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'crawlerService'
15:31:40.732 [http-nio-8070-exec-5] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'crawlerService'
15:31:40.737 [http-nio-8070-exec-5] DEBUG org.springframework.beans.factory.annotation.InjectionMetadata - Registered injected element on class [com.crawler.service.CrawlerServiceImpl]: AutowiredFieldElement for private com.crawler.dao.iface.ICrawlerDao com.crawler.service.CrawlerServiceImpl.crawlerDao
15:31:40.737 [http-nio-8070-exec-5] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'crawlerService' to allow for resolving potential circular references
15:31:40.743 [http-nio-8070-exec-5] DEBUG org.springframework.beans.factory.annotation.InjectionMetadata - Processing injected element of bean 'crawlerService': AutowiredFieldElement for private com.crawler.dao.iface.ICrawlerDao com.crawler.service.CrawlerServiceImpl.crawlerDao
15:31:40.749 [http-nio-8070-exec-5] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'crawlerDao'
15:31:40.749 [http-nio-8070-exec-5] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'crawlerDao'
15:31:40.750 [http-nio-8070-exec-5] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'crawlerDao' to allow for resolving potential circular references
15:31:40.760 [http-nio-8070-exec-5] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'crawlerDao'
15:31:40.763 [http-nio-8070-exec-5] DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Resolving exception from handler [null]: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'crawlerController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'crawlerService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.crawler.dao.iface.ICrawlerDao com.crawler.service.CrawlerServiceImpl.crawlerDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'crawlerDao' defined in file [D:\project\taohuadaoinfo\workspace_weiqiao\apache-tomcat-8.0.46-crawler\webapps\crawler\WEB-INF\classes\com\crawler\dao\CrawlerDao.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
15:31:40.763 [http-nio-8070-exec-5] DEBUG org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - Resolving exception from handler [null]: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'crawlerController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'crawlerService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.crawler.dao.iface.ICrawlerDao com.crawler.service.CrawlerServiceImpl.crawlerDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'crawlerDao' defined in file [D:\project\taohuadaoinfo\workspace_weiqiao\apache-tomcat-8.0.46-crawler\webapps\crawler\WEB-INF\classes\com\crawler\dao\CrawlerDao.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
15:31:40.763 [http-nio-8070-exec-5] DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [null]: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'crawlerController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'crawlerService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.crawler.dao.iface.ICrawlerDao com.crawler.service.CrawlerServiceImpl.crawlerDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'crawlerDao' defined in file [D:\project\taohuadaoinfo\workspace_weiqiao\apache-tomcat-8.0.46-crawler\webapps\crawler\WEB-INF\classes\com\crawler\dao\CrawlerDao.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
15:31:40.769 [http-nio-8070-exec-5] DEBUG org.springframework.web.servlet.DispatcherServlet - Could not complete request
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'crawlerController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'crawlerService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.crawler.dao.iface.ICrawlerDao com.crawler.service.CrawlerServiceImpl.crawlerDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'crawlerDao' defined in file [D:\project\taohuadaoinfo\workspace_weiqiao\apache-tomcat-8.0.46-crawler\webapps\crawler\WEB-INF\classes\com\crawler\dao\CrawlerDao.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:308)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1208)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
	at org.springframework.web.method.HandlerMethod.createWithResolvedBean(HandlerMethod.java:224)
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:306)
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:56)
	at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:299)
	at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1120)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:932)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:965)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:867)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:841)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'crawlerService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.crawler.dao.iface.ICrawlerDao com.crawler.service.CrawlerServiceImpl.crawlerDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'crawlerDao' defined in file [D:\project\taohuadaoinfo\workspace_weiqiao\apache-tomcat-8.0.46-crawler\webapps\crawler\WEB-INF\classes\com\crawler\dao\CrawlerDao.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1208)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:461)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:435)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:559)
	at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:305)
	... 44 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.crawler.dao.iface.ICrawlerDao com.crawler.service.CrawlerServiceImpl.crawlerDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'crawlerDao' defined in file [D:\project\taohuadaoinfo\workspace_weiqiao\apache-tomcat-8.0.46-crawler\webapps\crawler\WEB-INF\classes\com\crawler\dao\CrawlerDao.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
	... 57 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'crawlerDao' defined in file [D:\project\taohuadaoinfo\workspace_weiqiao\apache-tomcat-8.0.46-crawler\webapps\crawler\WEB-INF\classes\com\crawler\dao\CrawlerDao.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1572)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
	... 59 common frames omitted
Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
	at org.springframework.util.Assert.notNull(Assert.java:112)
	at org.mybatis.spring.support.SqlSessionDaoSupport.checkDaoConfig(SqlSessionDaoSupport.java:75)
	at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1631)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568)
	... 69 common frames omitted
四月 18, 2018 3:31:40 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [springServlet] in context with path [/crawler] threw exception [Request processing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'crawlerController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'crawlerService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.crawler.dao.iface.ICrawlerDao com.crawler.service.CrawlerServiceImpl.crawlerDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'crawlerDao' defined in file [D:\project\taohuadaoinfo\workspace_weiqiao\apache-tomcat-8.0.46-crawler\webapps\crawler\WEB-INF\classes\com\crawler\dao\CrawlerDao.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required] with root cause
java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
	at org.springframework.util.Assert.notNull(Assert.java:112)
	at org.mybatis.spring.support.SqlSessionDaoSupport.checkDaoConfig(SqlSessionDaoSupport.java:75)
	at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1631)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1208)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:461)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:435)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:559)
	at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
	at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:305)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1208)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
	at org.springframework.web.method.HandlerMethod.createWithResolvedBean(HandlerMethod.java:224)
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:306)
	at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:56)
	at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:299)
	at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1120)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:932)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:965)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:867)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:841)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Unknown Source)

15:31:40.770 [http-nio-8070-exec-5] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'sqlSessionFactory'

上文也说过其他配置没有改动,现在怀疑2个方面,

一、配置方面还需要其他改动或者添加的。

二、使用mapperScannerConfigurer配置造成的,先看这段配置在spring-mvc.xml中

    <!-- 扫描basePackage下所有以@Repository注解的Dao接口,注入sqlSessionFactory -->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <property name="basePackage" value="com.crawler"/>
        <property name="annotationClass" value="org.springframework.stereotype.Repository"/>
    </bean>

按理来说这个配置会将sqlSessionFactory注入到在扫描路径下,不应该报

Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
需要参数这个错误的。

然后将mapperScannerConfigurer配置屏蔽掉

package com.crawler.dao;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Repository;

import com.crawler.dao.iface.ICrawlerDao;

/**
 * 
 * @author siqiangming 2018年4月18日 上午9:22:00
 */
@Repository("crawlerDao")
public class CrawlerDao implements ICrawlerDao {
	@Resource
	private SqlSessionFactory sqlSessionFactory;
	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
		this.sqlSessionFactory = sqlSessionFactory;
	}
	public void createTable(Map<String, Object> map) {
		sqlSessionFactory.openSession().update("com.crawler.dao.iface.ICrawlerDao.createTable", map);
	}

	public void insert(Map<String, Object> map) {
		sqlSessionFactory.openSession().insert("com.crawler.dao.iface.ICrawlerDao.insert", map);
	}

}

测试发现解决问题,运行正常。

如果需要继承SQLSessionDaoSupport类,改为下面需要设置sqlSessionFactory

package com.crawler.dao;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.crawler.dao.iface.ICrawlerDao;

/**
 * 
 * @author siqiangming 2018年4月18日 上午9:22:00
 */
@Repository("crawlerDao")
public class CrawlerDao extends SqlSessionDaoSupport implements ICrawlerDao {
	@Resource
	private SqlSessionFactory sqlSessionFactory;
	@Resource
	public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
		super.setSqlSessionFactory(sqlSessionFactory);
	}
	public void createTable(Map<String, Object> map) {
		getSqlSession().update("com.crawler.dao.iface.ICrawlerDao.createTable", map);
	}

	public void insert(Map<String, Object> map) {
		getSqlSession().insert("com.crawler.dao.iface.ICrawlerDao.insert", map);
	}

}
OK,解决,最后发现是蛋疼了,还是不写dao实现类的好。




没有更多推荐了,返回首页