org.springframework.beans.factory.BeanNotOfRequiredTypeException的解决办法
2016-10-13 00:00
706 查看
摘要: Spring Hibernate整合出现的问题:一般在使用annotation的方式注入spring的bean 出现的,具体是由于spring采用代理的机制导致的,看使用的代码。
Spring Hibernate整合出现的问题:一般在使用annotation的方式注入spring的bean 出现的,具体是由于spring采用代理的机制导致的,看使用的代码。
先看一段代码
默认情况下,代码2运行会报出文章开始的错误。
造成这种错误的原因是
jdk的动态代理不支持类注入,只支持接口方式注入;
如果要使用类方式的注入,必须使用cglib代理;
关于spring动态代理的配置:
上面3种配置,只要任选一种即可,将proxy-target-class设置为true即使用cglib的方式代理对象。
附:spring的aop代理判断逻辑:
Spring Hibernate整合出现的问题:一般在使用annotation的方式注入spring的bean 出现的,具体是由于spring采用代理的机制导致的,看使用的代码。
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.myssh.dao.impl.AccountDao] 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.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) ... 35 more
先看一段代码
1、基于接口注入方式(Spring推荐的方式) @Autowired private IUserDao userDao; 2、基于类注入的方式 @Autowired private AccountDao accountDao;
默认情况下,代码2运行会报出文章开始的错误。
造成这种错误的原因是
jdk的动态代理不支持类注入,只支持接口方式注入;
如果要使用类方式的注入,必须使用cglib代理;
关于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); } }
相关文章推荐
- 解决: org.springframework.beans.factory.BeanNotOfRequiredTypeException
- 异常:org.springframework.beans.factory.BeanNotOfRequiredTypeException
- 代理及注入之org.springframework.beans.factory.BeanNotOfRequiredTypeException
- Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'dao' is exp
- org.springframework.beans.factory.BeanNotOfRequiredTypeException:
- org.springframework.beans.factory.BeanNotOfRequiredTypeException:
- org.springframework.beans.factory.BeanNotOfRequiredTypeException
- 【异常篇-spring】org.springframework.beans.factory.BeanNotOfRequiredTypeException
- Aurora-hec4.0_scaninvoice_异常_04_org.springframework.beans.factory.BeanNotOfRequiredTypeException
- 处理异常:org.springframework.beans.factory.BeanNotOfRequiredTypeException
- org.springframework.beans.factory.BeanNotOfRequiredTypeException
- 运用spring时出现org.springframework.beans.factory.BeanNotOfRequiredTypeException: 错误
- 在使用Spring AOP 时,启动服务抛出异常: org.springframework.beans.factory.BeanNotOfRequiredTypeException
- org.springframework.beans.factory.BeanNotOfRequiredTypeException 错误,
- Spring-org.springframework.beans.factory.BeanNotOfRequiredTypeException
- org.springframework.beans.factory.BeanNotOfRequiredTypeException must be of type
- "org.springframework.beans.factory.BeanNotOfRequiredTypeException……" 错误解决方案
- org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'faceAction' must be of
- org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named "" must be of type [org.apache.struts.action.Action
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.oskyhang.gbd.service.UserService] found for dependency: expected at least 1 bean which qualifies as aut