MyBatis与Spring 整合后,创建Bean报奇葩错的解决
2015-07-30 13:56
609 查看
代理了MaBatis Generator自动生成的Mapper的类
}
启动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 进行扫描配置
扫描出来后,肯定是在IOC容器中进行了Bean的装配,就怀疑是这里有关联,所以会出错。
将
但是这样,又不能保证我的代理类的接口和Mapper的一致。。。
而如果我事务不关联这个类,又可以实现这个接口,正常启动。。。真是郁闷
/***************************** 2015-07-30 17:19:15 *****************************/
将@Service的扫描,从Spring 容器移动到Spring MVC容器
也就是这样:
这样就可以加上 implements MaterialCountMapper
保证接口一致了,猜测可能是Spring的事务导致容器没加载这个Bean?现在放在Spring MVC的容器中加载,再由Spring容器管理事务,就解决了。
/***************************** 2015年8月8日
12:58:13 *****************************/
猜测,应该是配置了AOP功能后,相应的Bean只能在子容器中获取,所以配置在父容器中获取不到Bean。
/***************************** 2015年8月20日
08:55:31 *****************************/
确认是因为父,子容器获取不到Bean而出错,我将扫描Service放在SpringIoC容器中,将事务管理放在了SpringMVC IoC中,问题解决
http://download.csdn.net/detail/tragedyxd/9026633
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的装配,就怀疑是这里有关联,所以会出错。
将
implements MaterialCountMapper去掉,TOMCAT 正常启动。
但是这样,又不能保证我的代理类的接口和Mapper的一致。。。
而如果我事务不关联这个类,又可以实现这个接口,正常启动。。。真是郁闷
/***************************** 2015-07-30 17:19:15 *****************************/
将@Service的扫描,从Spring 容器移动到Spring MVC容器
也就是这样:
<!-- 扫描 Service start --> <context:component-scan base-package="com.aliapp.wxxd.material.service"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan> <context:component-scan base-package="facade"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan> <!-- end 扫描 Service -->
这样就可以加上 implements MaterialCountMapper
保证接口一致了,猜测可能是Spring的事务导致容器没加载这个Bean?现在放在Spring MVC的容器中加载,再由Spring容器管理事务,就解决了。
/***************************** 2015年8月8日
12:58:13 *****************************/
猜测,应该是配置了AOP功能后,相应的Bean只能在子容器中获取,所以配置在父容器中获取不到Bean。
/***************************** 2015年8月20日
08:55:31 *****************************/
确认是因为父,子容器获取不到Bean而出错,我将扫描Service放在SpringIoC容器中,将事务管理放在了SpringMVC IoC中,问题解决
![](http://download.csdn.net/images/minetype/rar.gif)
SpringMVC+Mybatis+Spring+Shiro+ehcache整合配置文件
http://download.csdn.net/detail/tragedyxd/9026633
相关文章推荐
- 用Spring将Service注入到Servlet中
- 如何在eclipse中导入两个相同的工程
- spring,mybatis事务管理配置与@Transactional注解使用
- 学习认识Spring原理
- Java在的时候,类定义HashSet初始化方法
- eclipse调试yarn
- 二叉堆 之 Java的实现
- java基础知识之初识java之第二章
- Java语言编码规范(Java Code Conventions)
- 如何理解java回电话
- spring注解@ResponseBody处理ajax请求
- java命名规范
- JAVA中重载和重写的区别
- spring-hibernate 简单dao
- 使用JavaMail发送邮件
- SpringMVC与Struts2的对比
- Java File类学习笔记2:File类对象及常用操作方法
- JAVA 面向对象和集合知识点总结
- jdk的安装 打包jar 运行jar
- maven 指定工程的 jdk 版本及编译级别