java中BeanNotOfRequiredTypeException的問(wèn)題解決(@Autowired和@Resource注解的不同)
1. 錯(cuò)誤信息
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named ‘aisleService’ must be of type [com.gdie.whlocation.service.impl.AisleService], but was actually of type [$Proxy38]
2. 問(wèn)題原因
一般在使用annotation的方式注入spring的bean 出現(xiàn)的,具體是由于spring采用代理的機(jī)制導(dǎo)致的,看使用的代碼:
3. @Autowired和@Resource注解的區(qū)別
1. 使用類注入:
@Resource(name = "aisleService") private AisleService aisleService;
2. 使用接口注入:
@Resource(name = "aisleService") private IAisleService aisleService;
代碼1不能使用JDK的動(dòng)態(tài)代理注入,原因是JDK的動(dòng)態(tài)代理不支持類注入,只支持接口方式注入;
代碼2可以使用JDK動(dòng)態(tài)代理注入;
如果要使用代碼1的方式,必須使用cglib代理;當(dāng)然了推薦使用代碼2的方式,基于接口編程的方式!
使用1的方式也是可以的,建議試一下@Autowired這個(gè)注解:
- @Autowired這個(gè)注解和@Resource注解不同的是方法類型不同;
- @Autowired是按類的類型注入的 ,是不用區(qū)分注入名字的;
- @Resource是按照類的名字注入的,區(qū)分注入名字的大小寫(xiě)的寫(xiě)錯(cuò)了也會(huì)報(bào)BeanNotOfRequiredTypeException異常;
4. 關(guān)于spring動(dòng)態(tài)代理的配置:
1.使用aop配置:
<aop:config proxy-target-class="false"> </aop:config>
2. aspectj配置:
<aop:aspectj-autoproxy proxy-target-class="true"/>
3. 事務(wù)annotation配置:
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
3種配置,只要使用一種即可,設(shè)置proxy-target-class為true即使用cglib的方式代理對(duì)象。
附:spring的aop代理判斷邏輯:
//org.springframework.aop.framework.DefaultAopProxyFactory //參數(shù)AdvisedSupport 是Spring AOP配置相關(guān)類 public AopProxy createAopProxy(AdvisedSupport advisedSupport) throws AopConfigException { //在此判斷使用JDK動(dòng)態(tài)代理還是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); } }
到此這篇關(guān)于java中BeanNotOfRequiredTypeException的問(wèn)題解決(@Autowired和@Resource注解的不同)的文章就介紹到這了,更多相關(guān)java BeanNotOfRequiredTypeException內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
探索Java中private方法添加@Transactional事務(wù)未生效原因
你又遇到過(guò)明明給private方法添加了@Transactional但是事務(wù)依然沒(méi)有生效的情況嗎,具體原因本篇文章將詳細(xì)告訴你,有需要的朋友跟著小編往下看吧2021-11-11Windows下將JAVA?jar注冊(cè)成windows服務(wù)的方法
這篇文章主要介紹了Windows下將JAVA?jar注冊(cè)成windows服務(wù)的方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07Java中的線程池如何實(shí)現(xiàn)線程復(fù)用
這篇文章主要介紹了Java中的線程池如何實(shí)現(xiàn)線程復(fù)用問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03Mybatis中的PageHelper的執(zhí)行流程分析
這篇文章主要介紹了Mybatis的PageHelper執(zhí)行流程,本文給大家介紹介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02SpringBoot+MyBatis實(shí)現(xiàn)MD5加密數(shù)據(jù)庫(kù)用戶密碼的方法
MD5技術(shù)主要用于對(duì)用戶密碼加密,增加賬戶的安全性,他具有不可逆的特性,不會(huì)被輕易解密,這篇文章給大家介紹SpringBoot+MyBatis實(shí)現(xiàn)MD5加密數(shù)據(jù)庫(kù)用戶密碼的方法,感興趣的朋友跟隨小編一起看看吧2024-03-03