org.springframework.beans.factory.BeanCreationException: Error creating bean with name:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'practiceWebConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.example.practice.interceptor.PassportInceptor com.example.practice.configuration.PracticeWebConfiguration.passportInceptor; nested exception is java.lang.IllegalArgumentException: Can not set com.example.practice.interceptor.PassportInceptor field com.example.practice.configuration.PracticeWebConfiguration.passportInceptor to com.sun.proxy.$Proxy72

以上是出錯內容:@Autowired   註冊對於接口的實現類的時候要使用接口類來注入才行

 

原因參看:https://blog.csdn.net/qq_31963719/article/details/79458002

 

// 告訴spring mvc這是一個控制器類
@Controller
@RequestMapping("")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    @RequestMapping("listCategory")
    public ModelAndView listCategory(){
        ModelAndView mav = new ModelAndView();
        List<Category> cs= categoryService.list();
 @Autowired
    CategoryService categoryService;

 

爲什麼在CategoryController類中 @Auto明明註解在CategoryService 這個接口上 而注入的卻是CategoryServiceImpl這個實現類
因爲: (自動裝配實現了CategoryService接口的的實例,只有CategoryServiceImpl實現了CategoryService接口,所以就會注入CategoryServiceImpl)

這種自動裝配 @Autowired 還是@Resource在裝配或者注入的時候都是先是實例化後再進行的 第一步都是先實例化

這裏他要實例化一個接口是不可能的 所以要找到他的實現類 實例化他的實現類
 

發佈了32 篇原創文章 · 獲贊 17 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章