CTO 说,再用错@Autowired 和@Resource 就可以领盒饭了
作者:liuxuzxx
@Autowire
private JdbcTemplate jdbcTemplate;
提示的警告信息
Field injection is not recommended Inspection info: Spring Team recommends: "Always use constructor based dependency injection in your beans. Always use assertions for mandatory dependencies".
如图
基于构造函数的依赖注入
public class UserServiceImpl implents UserService{
private UserDao userDao;
public UserServiceImpl(UserDao userDao){
this.userDao = userDao;
}
}
基于Setter的依赖注入
public class UserServiceImpl implents UserService{
private UserDao userDao;
public serUserDao(UserDao userDao){
this.userDao = userDao;
}
}
基于字段的依赖注入
public class UserServiceImpl implents UserService{
private UserDao userDao;
}
如图
对于有final修饰的变量不好使
掩盖单一职责的设计思想
public class VerifyServiceImpl implents VerifyService{
private AccountService accountService;
private UserService userService;
private IDService idService;
private RoleService roleService;
private PermissionService permissionService;
private EnterpriseService enterpriseService;
private EmployeeService employService;
private TaskService taskService;
private RedisService redisService;
private MQService mqService;
public SystemLogDto(AccountService accountService,
UserService userService,
IDService idService,
RoleService roleService,
PermissionService permissionService,
EnterpriseService enterpriseService,
EmployeeService employService,
TaskService taskService,
RedisService redisService,
MQService mqService) {
this.accountService = accountService;
this.userService = userService;
this.idService = idService;
this.roleService = roleService;
this.permissionService = permissionService;
this.enterpriseService = enterpriseService;
this.employService = employService;
this.taskService = taskService;
this.redisService = redisService;
this.mqService = mqService;
}
}
与Spring的IOC机制紧密耦合
无法对注入的属性进行安检
感谢您的阅读,也欢迎您发表关于这篇文章的任何建议,关注我,技术不迷茫!小编到你上高速。
正文结束
2.深圳一普通中学老师工资单曝光,秒杀程序员,网友:敢问是哪个学校毕业的?
3.从零开始搭建创业公司后台技术栈
4.程序员一般可以从什么平台接私活?
5.清华大学:2021 元宇宙研究报告!
6.为什么国内 996 干不过国外的 955呢?
7.这封“领导痛批95后下属”的邮件,句句扎心!
8.15张图看懂瞎忙和高效的区别!
评论