MyBatis与Spring 整合后,创建Bean报奇葩错的解决

代理了MaBatis Generator自动生成的Mapper的类

package com.aliapp.wxxd.material.service;

import interfaces.EntityWrapperInterface;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.aliapp.wxxd.material.entity.db.Material;
import com.aliapp.wxxd.material.entity.db.MaterialCount;
import com.aliapp.wxxd.material.entity.db.MaterialCountExample;
import com.aliapp.wxxd.material.mapper.MaterialCountMapper;

import util.FileUtil;
import util.ienum.MessageTypeEnum;

/**
 *
 *
 * @author VanXD
 *
 */
@Service
public class MaterialCountMapperProxy implements MaterialCountMapper{ //注意这个接口是自动生成的,为了让Proxy与目标接口一致,所以我也实现了它<pre name="code" class="java">
	@Autowired
	MaterialCountMapper materialCountMapper;
//...

//..

}


启动TOMCAT 报错:

WARN [localhost-startStop-1] - Exception encountered during context initialization - cancelling refresh attemptorg.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘materialFacade‘: Injection of autowired dependencies failed;
nested exception isorg.springframework.beans.factory.BeanCreationException: Could not autowire field: com.aliapp.wxxd.material.service.MaterialCountMapperProxy facade.MaterialFacade.materialCountMapperProxy; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.aliapp.wxxd.material.service.MaterialCountMapperProxy] 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)}    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)  
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202)    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.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762)  
 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)    at
org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)  
 at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4772)    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5196)    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)  
 at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)    at java.util.concurrent.FutureTask.run(FutureTask.java:262)    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)  
 at java.lang.Thread.run(Thread.java:744)

。。。

。。

。。

Spring表示没找到 MaterialCountMapperProxy 这个类的Bean,注意我是加了@Service,并且配置文件进行了扫描的。

mapper由Spring 进行扫描配置

<!-- mapper 扫描配置 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
		<property name="basePackage" value="com.aliapp.wxxd.material.mapper"></property>
	</bean>

扫描出来后,肯定是在IOC容器中进行了Bean的装配,就怀疑是这里有关联,所以会出错。

<span style="font-size:24px;">implements MaterialCountMapper</span>

去掉,TOMCAT 正常启动。

但是这样,又不能保证我的代理类的接口和Mapper的一致。。。

而如果我事务不关联这个类,又可以实现这个接口,正常启动。。。真是郁闷

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-29 11:53:35

MyBatis与Spring 整合后,创建Bean报奇葩错的解决的相关文章

【MyBatis学习14】MyBatis和Spring整合

前面十几篇博文总结了mybatis在开发中的相关技术,但在实际中都是和spring整合开发的,所以这里总结一下mybatis和spring的整合方法,并在整合后进行测试. 1. 整合的环境 这都是老掉牙的问题了,不管是开发还是整合,首先环境肯定得有,环境嘛,除了Java环境和开发环境外,那就是jar包咯,关于mybatis和spring整合的jar包,我已经上传到下载频道了==>传送门  http://download.csdn.net/detail/eson_15/9549624 将这些jar

MyBatis入门第2天--MyBatis与Spring整合及逆向工程

文档版本 开发工具 测试平台 工程名字 日期 作者 备注 V1.0 2016.06.28 lutianfei none spring和mybatis整合 整合思路 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession.(spring和mybatis整合自动完成) 持久层的mapper都需要由spring进行管理. 整合环境 创建一个新的java工程(接近实际开发的工程结构

MyBatis学习(三)---MyBatis和Spring整合

想要了解MyBatis基础的朋友可以通过传送门: MyBatis学习(一)---配置文件,Mapper接口和动态SQL http://www.cnblogs.com/ghq120/p/8322302.html MyBatis学习(二)---数据表之间关联 http://www.cnblogs.com/ghq120/p/8323918.html 之前两篇文章都是单独介绍了MyBatis的用法,并没有和任何框架进行整合.使用MyBatis完成数据库的操作仍有一些模板化的代码,比如关闭SqlSessi

Mybatis和Spring整合&amp;逆向工程

Mybatis和Spring整合&逆向工程Mybatis和Spring整合mybatis整合Spring的思路目的就是将在SqlMapConfig.xml中的配置移植到Spring的applicationContext.xml文件中让spring管理SqlSessionFactory让spring管理mapper对象和dao.使用spring和mybatis整合开发mapper代理及原始dao接口.自动开启事务,自动关闭 sqlsession.让spring管理数据源( 数据库连接池)导入相关的

mybatis与spring整合时读取properties问题的解决

在学习mybatis与spring整合是,想从外部引用一个db.properties数据库配置文件,在配置文件中使用占位符进行引用,如下: 1 <context:property-placeholder location="classpath:db.properties" /> 2 <bean id="dataSource" 3 class="org.springframework.jdbc.datasource.DriverManage

Mybatis 与 spring 整合

这篇文章我们来学习一下Mybatis 与 spring 的整合MapperFactoryBean 我们知道在Mybatis的所有操作都是基于一个SqlSession的,而SqlSession是由SqlSessionFactory来产生的,SqlSessionFactory又是由SqlSessionFactoryBuilder来生成的.但是Mybatis-Spring是基于SqlSessionFactoryBean的.在使用Mybatis-Spring的时候,我们也需要SqlSession,而且这

Mybatis+struts2+spring整合

把student项目改造成ssm  struts2 +mybatis+spring 1,先添加spring支持:类库三个,applicationContext.xml写在webinf下四个命名空间,监听器 2,添加struts2支持  struts2与spring整合的jar包 3,添加mybatis2支持,把jar包导入,mybatis与spring整合的jar包,把原来在mybatis.cfg.xml中的大部分配置都写在applicationContext.xml,跟hibernate一样,

Mybatis与Spring整合

Mybatis与Spring整合无外乎要将数据源,以及事物管理的相关配置交给spring来管理,mybatis配置负责sqlmapper的相关配置也就是dao层到sql映射的相关配置. 一下以手机管理系统dao层实现所用到的Spring与MyBatis整合为例. 1.spring中beans.xml相关配置 <!--加载数据源基本配置文件--><context:property-placeholder location="classpath:conf/jdbc.properti

mybatis与spring整合(基于配置文件)(转)

mybatis与spring整合(基于配置文件) 本文主要介绍了如何将mybatis和spring整合在一起使用,本人使用的是mybatis3.05 + spring3.1.0M2 ,使用dbcp作为数据库连接池. 1.编写数据访问接口(UserDao.java) package com.mybatis; publicinterface UserDao { publicint countAll(); } 2.编写数据访问接口映射文件(UserDaoMapper.xml) <?xml versio