啓動報錯:beans.factory.BeanNotOfRequiredTypeException: Bean named 'xxxService' is expected to be of t

啓動報錯:beans.factory.BeanNotOfRequiredTypeException: Bean named 'xxService' is expected to be of type 'com.inter.IxxService' but was actually of type 'com.service.xxService$$EnhancerBySpringCGLIB$$8e6810a7'

spring不但支持自己定義的@Autowired註解,還支持幾個由JSR-250規範定義的註解,它們分別是@Resource、@PostConstruct以及@PreDestroy。

  @Resource的作用相當於@Autowired,只不過@Autowired按byType自動注入,而@Resource默認按 byName自動注入罷了

也就是說:

 

使用@Resource  我們想注入的是ISysCouponService,隨便定義了一個變量名稱爲 couponService , 而恰巧代碼裏又出現以couponService命名的類,並不是我們想要注入的bean,此時啓動會報錯,也就是說代碼中如果沒有這個命名的類什麼事都沒有,這只是一個巧合,所以說命名儘量規範。

 

使用 @Autowired 不會出現以上問題

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章