spring异常解决方法:org.springframework.beans.factory.BeanCreationNotAllowedException
2017-04-14 11:50
746 查看
错误信息:
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采用代理的机制导致的,看使用的代码:
Java代码
1. 使用类注入:
@Resource(name = "aisleService")
private AisleService aisleService;
2. 使用接口注入:
@Resource(name = "aisleService")
private IAisleService aisleService;
代码1不能使用JDK的动态代理注入,原因是jdk的动态代理不支持类注入,只支持接口方式注入;
代码2可以使用jdk动态代理注入;
如果要使用代码1的方式,必须使用cglib代理;
当然了推荐使用代码2的方式,基于接口编程的方式!
关于spring动态代理的配置:
Xml代码
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代理判断逻辑:
Java代码
//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);
}
}
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采用代理的机制导致的,看使用的代码:
Java代码
1. 使用类注入:
@Resource(name = "aisleService")
private AisleService aisleService;
2. 使用接口注入:
@Resource(name = "aisleService")
private IAisleService aisleService;
代码1不能使用JDK的动态代理注入,原因是jdk的动态代理不支持类注入,只支持接口方式注入;
代码2可以使用jdk动态代理注入;
如果要使用代码1的方式,必须使用cglib代理;
当然了推荐使用代码2的方式,基于接口编程的方式!
关于spring动态代理的配置:
Xml代码
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代理判断逻辑:
Java代码
//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);
}
}
相关文章推荐
- 出现 org.springframework.beans.factory.BeanCreationException 异常的原因及解决方法
- 关于SpringMVC中org.springframework.beans.factory.BeanCreationException: Could not autowire field:的问题的解决
- 【问题解决】 web项目启动不了 org.springframework.beans.factory.BeanCreationException: Could not autowire field:
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name的解决方法
- org.springframework.beans.factory.BeanCreationException异常原因及解决办法
- 异常:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletCo
- org.springframework.beans.factory.BeanCreationException: Scope 'request' is not active for the current thread
- tomcat启动异常--org.springframework.beans.factory.BeanCreationException
- 解决org.springframework.beans.factory.BeanCreationException: Error creating bean with name
- 异常处理 org.springframework.beans.factory.BeanCreationException
- org.springframework.beans.factory.BeanCreationException 解决办法
- Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field
- 解决org.springframework.beans.factory.BeanCreationException: Error creating bean with name
- org.springframework.beans.factory.BeanCreationException---这个错误怎么解决,求助!!!
- 解决org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.spring
- 个人笔记 org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.
- SH初步整合的异常:org.springframework.beans.factory.BeanCreationException
- 题目:org.springframework.beans.factory.BeanCreationException(hibernate中的jar包和spring中的jar包冲突)异常产生的原因和解决方案。
- mybatis配置通用dao和service时出现org.springframework.beans.factory.BeanCreationException异常
- Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field