spring异常解决方法:org.springframework.beans.factory.BeanCreationNotAllowedException
来源:互联网 发布:sql注入检测网站 编辑:程序博客网 时间:2024/05/22 16:23
错误信息:
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'aisleService' must be of type [com.gdie.whlocation.service.impl.AisleService], but was actually of type [$Proxy38]
这个问题出现的原因:一般在使用annotation的方式注入spring的bean 出现的,具体是由于spring采用代理的机制导致的,看使用的代码:
- 1. 使用类注入:
- @Resource(name = "aisleService")
- private AisleService aisleService;
- 2. 使用接口注入:
- @Resource(name = "aisleService")
- private IAisleService aisleService;
代码1不能使用JDK的动态代理注入,原因是jdk的动态代理不支持类注入,只支持接口方式注入;
代码2可以使用jdk动态代理注入;
如果要使用代码1的方式,必须使用cglib代理;
当然了推荐使用代码2的方式,基于接口编程的方式!
关于spring动态代理的配置:
- 1.使用aop配置:
- <aop:config proxy-target-class="false"> </aop:config>
- 2. aspectj配置:
- <aop:aspectj-autoproxy proxy-target-class="true"/>
- 3. 事务annotation配置:
- <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
3种配置,只要使用一种即可,设置proxy-target-class为true即使用cglib的方式代理对象。
附:spring的aop代理判断逻辑:
- //org.springframework.aop.framework.DefaultAopProxyFactory
- //参数AdvisedSupport 是Spring AOP配置相关类
- public AopProxy createAopProxy(AdvisedSupport advisedSupport)
- throws AopConfigException {
- //在此判断使用JDK动态代理还是CGLIB代理
- if (advisedSupport.isOptimize() || advisedSupport.isProxyTargetClass()
- || hasNoUserSuppliedProxyInterfaces(advisedSupport)) {
- if (!cglibAvailable) {
- throw new AopConfigException(
- "Cannot proxy target class because CGLIB2 is not available. "
- + "Add CGLIB to the class path or specify proxy interfaces.");
- }
- return CglibProxyFactory.createCglibProxy(advisedSupport);
- } else {
- return new JdkDynamicAopProxy(advisedSupport);
- }
- }
1 0
- spring异常解决方法:org.springframework.beans.factory.BeanCreationNotAllowedException
- spring boot-部署时报org.springframework.beans.factory.BeanCreationNotAllowedException异常
- spring异常org.springframework.beans.factory.NoSuchBeanDefinitionException
- 关于Spring中org.springframework.beans.factory.UnsatisfiedDependencyException异常
- Spring中org.springframework.beans.factory.UnsatisfiedDependencyException异常
- Spring容器初始化异常:org.springframework.beans.factory.BeanDefinitionStoreException
- spring启动异常:org.springframework.beans.factory.BeanCreationException
- Spring中org.springframework.beans.factory.support.DefaultListableBeanFactory.<clinit>解决方法.
- 部署Spring AOP出现org.springframework.beans.factory.BeanCreationException解决方法
- 异常处理 org.springframework.beans.factory.BeanCreationException
- 异常:org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
- 异常org.springframework.beans.factory.NoSuchBeanDefinitionException
- org.springframework.beans.factory.BeanCreationException异常
- org.springframework.beans.factory.NoSuchBeanDefinitionException: 异常处理
- org.springframework.beans.factory.UnsatisfiedDependencyException异常
- org.springframework.beans.factory.NoUniqueBeanDefinitionException异常问题
- org.springframework.beans.factory.UnsatisfiedDependencyException异常
- 异常:org.springframework.beans.factory.BeanNotOfRequiredTypeException
- effective c++ 条款13,16(以对象管理资源,成对使用new delete的形式相同)
- 利用Hexo+github搭建博客,零成本、无需域名、服务器
- 获取客户端唯一标识码
- Android 5.0以上Button去掉阴影
- PHP判断来访是搜索引擎蜘蛛还是普通用户的代码小结
- spring异常解决方法:org.springframework.beans.factory.BeanCreationNotAllowedException
- App启动白屏问题
- Boost全平台编译方法
- 街区最短路径问题
- Yii2--创建多应用
- Jxl和POI的区别
- Redis 数据类型
- 收藏的学习网站
- 四、zedboard linux图形化界面和启动文件