错误信息:
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);
- }
- }
Java,Spring,BeanNotOfRequiredTypeException
问题描述:
今天在使用annotation或Resource的方式注入spring的bean 的时候出现了 org.springframework.beans.factory.BeanNotOfRequiredTypeException:错误
后来看了下spring的一些解释,是未正确使用spring采用的代理机制导致了这个错误。
代码
@Resource
private FormDefMappingService formDefMappingService;
代码
@Autowired
private FormDefMappingService formDefMappingService;
上述的代码在只有在项目 类命名唯一否则会出现上述错误,避免上述错误在
1、检查在FormDefMappingServiceImpl是否添加@service的注解(项目中仅有一个FormDefMappingServiceImpl时可省略2、3步骤)
2、FormDefMappingServiceImpl中@service中添加name(name在项目中必须唯一)
3、在使用时@Resource或@Autowired(添加name需与@Qualifier结合)中,添加name(name与FormDefMappingServiceImpl中name相同)
代码
@Resource(name="formDefMappingService")
private FormDefMappingService formDefMappingService;
代码
@Autowired(required=false)
@Qualifier("formDefMappingService")
private FormDefMappingService formDefMappingService;
spring为什么要注入接口,而不是实现类
首先说明,注入的对象确实为实现类的对象。(并不是实现类的代理对象,注入并不涉及代理)
如果只是单纯注入是可以用实现类接收注入对象的,但是往往开发中会对实现类做增强,如事务,日志等,实现增强的AOP技术是通过动态代理实现的,而spring默认是JDK动态代理,对实现类对象做增强得到的增强类与实现类是兄弟关系,所以不能用实现类接收增强类对象,只能用接口接收。如:
//接口:IA
//实现类:AImpl
//增强类:AImplProxy
AImpl aImpl = new AImpl();
//通过JDKProxyFactory创建代理对象
JDKProxyFactory factory = new JDKProxyFactory(aImpl);
AImplProxy aImplProxy = factory.createProxy();//这个增强类对象aImplProxy 只能强转为IA,而不能转为AImpl,因为JDK代理得到的AImplProxy类与AImpl是兄弟关系而非父子
由于以上原因,如果将对象注入给实现类而非接口的话,在代理时就会报错。
解决方法,让Spring强制使用Cglib代理:
<aop:aspectj-autoproxy proxy-target-class="true"/>
cglib代理类和实现类之间是父子关系,自然可以用父类(实现类)去接收子类对象(代理类对象即增强类对象)。
不过应该不会需要这么做,使用接口本来就是解耦的,你直接用实现类接收注入对象岂不是失去了注入的意义。(为什么不直接new一个呢?)