在學習Spring的過程當中遇到一個錯誤:在使用註解@resources的時候提示:The annotation @Resources is disallowed for this locationjava
後來來在學問Java網友的時候解決了。學習
原來的代碼是這樣的:this
1 package com.show.biz; 2 3 import javax.annotation.Resources; 4 5 import com.show.biz.UserBiz; 6 import com.show.dao.UserDAO; 7 8 public class UserBizImplByAnnotationOfResource implements UserBiz { 9 10 //將註解標註在userDAO上 11 @Resources(name="userDAO") //或者直接使用@Resources 12 UserDAO userDAO; 13 14 //將註解標註在userDAo的set方法上 15 @Resources(name="userDAO") 16 public void setUserDAO(UserDAO userDAo){ 17 this.userDAO = userDAo; 18 } 19 20 public boolean login(String username, String password) { 21 22 return userDAO.login(username, password); 23 } 24 25 }
網友提示:@Resources修改爲呢個@Resource,結果然的成了。修改後的代碼以下:spa
package com.show.biz; import javax.annotation.Resource; import javax.annotation.Resources; import com.show.biz.UserBiz; import com.show.dao.UserDAO; public class UserBizImplByAnnotationOfResource implements UserBiz { //將註解標註在userDAO上 @Resource(name="userDAO") //或者直接使用@Resources UserDAO userDAO; //將註解標註在userDAo的set方法上 @Resource(name="userDAO") public void setUserDAO(UserDAO userDAo){ this.userDAO = userDAo; } public boolean login(String username, String password) { return userDAO.login(username, password); } }