1. 程式人生 > >Error creating bean with name 'userController': Injection of resource dependencies failed;

Error creating bean with name 'userController': Injection of resource dependencies failed;

Springmvc問題

今天,做開發時,剛配置好環境,測試一下環境是否正常,然後日誌報錯是:
  1. org.springframework.beans.factory.BeanCreationException: 
    Error creating bean with name 'userController': Injection of resource dependencies
     failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: 

先檢查是否配置了註解@Controller,@Service,@Repository,@Resource,發現程式碼沒問題

然後網上查詢解決方案,發現原來是在UserServiceImpl中沒有寫@Service("userService"),我以前是隻有@Service為什麼不會報錯???不理解,難道這次的配置文件是看別人的,導致不一樣配置,註解需要加上value值???不懂,記下來,以後研究,或者大佬幫忙解答一下

在控制層呼叫業務層,必須在service層先進行註解:

 c @Service("userService");  

然後在控制層注入業務層:

  1. @Resource
  2. private UserService userService;