最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java中BeanNotOfRequiredTypeException的問題解決(@Autowired和@Resource注解的不同)

 更新時間:2023年07月18日 10:19:01   作者:自知自省  
本文主要介紹了java中BeanNotOfRequiredTypeException的問題解決(@Autowired和@Resource注解的不同),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1. 錯誤信息

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. 問題原因

一般在使用annotation的方式注入spring的bean 出現(xiàn)的,具體是由于spring采用代理的機制導(dǎo)致的,看使用的代碼:

3. @Autowired和@Resource注解的區(qū)別

1. 使用類注入:  

@Resource(name = "aisleService")  
private AisleService aisleService;  

2. 使用接口注入:  

@Resource(name = "aisleService")  
private IAisleService aisleService;  

代碼1不能使用JDK的動態(tài)代理注入,原因是JDK的動態(tài)代理不支持類注入,只支持接口方式注入;

代碼2可以使用JDK動態(tài)代理注入;

如果要使用代碼1的方式,必須使用cglib代理;當(dāng)然了推薦使用代碼2的方式,基于接口編程的方式!

使用1的方式也是可以的,建議試一下@Autowired這個注解:

  • @Autowired這個注解和@Resource注解不同的是方法類型不同;
  • @Autowired是按類的類型注入的 ,是不用區(qū)分注入名字的;
  • @Resource是按照類的名字注入的,區(qū)分注入名字的大小寫的寫錯了也會報BeanNotOfRequiredTypeException異常;

4. 關(guān)于spring動態(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的方式代理對象。

附:spring的aop代理判斷邏輯:

//org.springframework.aop.framework.DefaultAopProxyFactory     
//參數(shù)AdvisedSupport 是Spring AOP配置相關(guān)類     
public AopProxy createAopProxy(AdvisedSupport advisedSupport)     
        throws AopConfigException {     
    //在此判斷使用JDK動態(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的問題解決(@Autowired和@Resource注解的不同)的文章就介紹到這了,更多相關(guān)java BeanNotOfRequiredTypeException內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

鄂托克前旗| 东台市| 扶绥县| 涟源市| 敖汉旗| 房山区| 竹山县| 偃师市| 明星| 商丘市| 如皋市| 乌兰浩特市| 威宁| 桂林市| 乡宁县| 鄂伦春自治旗| 宣化县| 仁寿县| 衢州市| 陇川县| 赤峰市| 朝阳区| 石楼县| 泾源县| 萨迦县| 自贡市| 定襄县| 石城县| 东山县| 彭水| 庆阳市| 东乌| 商丘市| 永新县| 甘谷县| 兰考县| 穆棱市| 迭部县| 乌拉特后旗| 崇仁县| 青铜峡市|