spring 靜態注入


工具類中一般都是使用靜態方法,下面是靜態注入的例子:

@Component
public class BaseUtil {

private static UserService userService;
private static FileService fileService;

@Autowired
private void setUserService(UserService userService){
BaseUtil.userService = userService;
}
@Autowired
private void setFileService(FileService fileService){
BaseUtil.fileService = fileService;
}

    public static void populateInfo(User entity, UserDTO dto) {
fileService.findAll(null, entity.getId() + "");
}

}
注意:1、靜態成員變量上不需要再添加@Autowired注解。
2、靜態變量的setter方法的static關鍵字要去掉,不然會注入失敗。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM