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