速读摘要文章源自JAVA秀-https://www.javaxiu.com/10652.html
本身Spring的目的就是解藕和依赖反转,结果通过再次与类注入器(在本例中为Spring)耦合,失去了通过自动装配类字段而实现的对类的解耦,从而使类在Spring容器之外无效。基本都是private形式的,private把属性都给封印到class当中了,无法对注入的属性进行安检。你在程序启动的时候无法拿到这个类,只有在真正的业务使用的时候才会拿到,一般情况下,这个注入的都是非null的,万一要是null怎么办,在业务处理的时候错误才爆出来,时间有点晚了,如果在启动的时候就暴露出来,那么bug就可以很快得到修复(当然你可以加注解校验)。文章源自JAVA秀-https://www.javaxiu.com/10652.html
原文约 1713 字 | 图片 7 张 | 建议阅读 4 分钟 | 评价反馈文章源自JAVA秀-https://www.javaxiu.com/10652.html
@Autowire 和 @Resource 注解使用的正确姿势,别再用错的了!
点击关注 ? Java面试那些事儿 文章源自JAVA秀-https://www.javaxiu.com/10652.html
收录于话题文章源自JAVA秀-https://www.javaxiu.com/10652.html
#Java面试那些事儿文章源自JAVA秀-https://www.javaxiu.com/10652.html
41个文章源自JAVA秀-https://www.javaxiu.com/10652.html
大家好,我是D哥文章源自JAVA秀-https://www.javaxiu.com/10652.html
点击关注下方公众号,Java面试资料 都在这里
文章源自JAVA秀-https://www.javaxiu.com/10652.html
来源:juejin.cn/post/6844904064212271117文章源自JAVA秀-https://www.javaxiu.com/10652.html
# 介绍
今天使用Idea写代码的时候,看到之前的项目中显示有warning的提示,去看了下,是如下代码?文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.html@Autowire
private JdbcTemplate jdbcTemplate;
提示的警告信息文章源自JAVA秀-https://www.javaxiu.com/10652.html
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".文章源自JAVA秀-https://www.javaxiu.com/10652.html
这段是Spring工作组的建议,大致翻译一下:文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.html
属性字段注入的方式不推荐,检查到的问题是:Spring团队建议:"始终在bean中使用基于构造函数的依赖项注入,始终对强制性依赖项使用断言"文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.html
Field注入警告文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.html
# 注入方式
文章源自JAVA秀-https://www.javaxiu.com/10652.html
虽然当前有关Spring Framework(5.0.3)的文档仅定义了两种主要的注入类型,但实际上有三种:文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.html
基于构造函数的依赖注入文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.htmlpublicclassUserServiceImplimplentsUserService{
private UserDao userDao;
@Autowire
publicUserServiceImpl(UserDao userDao){
this.userDao = userDao;
}
}
基于Setter的依赖注入文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.htmlpublicclassUserServiceImplimplentsUserService{
private UserDao userDao;
@Autowire
publicserUserDao(UserDao userDao){
this.userDao = userDao;
}
}
基于字段的依赖注入文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.htmlpublicclassUserServiceImplimplentsUserService{
@Autowire
private UserDao userDao;
}
基于字段的依赖注入方式会在Idea当中吃到黄牌警告,但是这种使用方式使用的也最广泛,因为简洁方便,您甚至可以在一些Spring指南中看到这种注入方法,尽管在文档中不建议这样做(有点执法犯法的感觉)文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.html
Spring自己的文档文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.html
# 基于字段的依赖注入缺点
文章源自JAVA秀-https://www.javaxiu.com/10652.html
对于有final修饰的变量不好使文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.html
Spring的IOC对待属性的注入使用的是set形式,但是final类型的变量在调用class的构造函数的这个过程当中就得初始化完成,这个是基于字段的依赖注入做不到的地方,只能使用基于构造函数的依赖注入的方式文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.html
掩盖单一职责的设计思想文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.html
我们都知道在OOP的设计当中有一个单一职责思想,如果你采用的是基于构造函数的依赖注入的方式来使用Spring的IOC的时候,当你注入的太多的时候,这个构造方法的参数就会很庞大,类似于下面文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.html
当你看到这个类的构造方法那么多参数的时候,你自然而然的会想一下:这个类是不是违反了单一职责思想? 但是使用基于字段的依赖注入 不会让你察觉,你会很沉浸在@Autowire当中文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.htmlpublicclassVerifyServiceImplimplentsVerifyService{
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机制紧密耦合文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.html
当你使用基于字段的依赖注入方式的时候,确实可以省略构造方法和setter这些个模板类型的方法,但是,你把控制权全给Spring的IOC了,别的类想重新设置下你的某个注入属性,没法处理(当然反射可以做到)。文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.html
本身Spring的目的就是解藕和依赖反转,结果通过再次与类注入器(在本例中为Spring)耦合,失去了通过自动装配类字段而实现的对类的解耦,从而使类在Spring容器之外无效。文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.html
隐藏依赖性文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.html
当你使用Spring的IOC的时候,被注入的类应当使用一些public类型(构造方法,和setter类型方法)的方法来向外界表达:我需要什么依赖,但是基于字段的依赖注入 的方式,基本都是private形式的,private把属性都给封印到class当中了,文章源自JAVA秀-https://www.javaxiu.com/10652.html
无法对注入的属性进行安检。文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.html
基于字段的依赖注入 方式,你在程序启动的时候无法拿到这个类,只有在真正的业务使用的时候才会拿到,一般情况下,这个注入的都是非null的,万一要是null怎么办,在业务处理的时候错误才爆出来,时间有点晚了,如果在启动的时候就暴露出来,那么bug就可以很快得到修复(当然你可以加注解校验)。文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.html
如果你想在属性注入的时候,想根据这个注入的对象操作点东西,你无法办到。我碰到过的例子:一些配置信息啊,有些人总是会配错误,等到了自己测试业务阶段才知道配错了,例如线程初始个数不小心配置成了3000,机器真的是狂叫啊!这个时候就需要再某些Value注入的时候做一个检测机制文章源自JAVA秀-https://www.javaxiu.com/10652.html
文章源自JAVA秀-https://www.javaxiu.com/10652.html
# 结论
文章源自JAVA秀-https://www.javaxiu.com/10652.html
通过上面,我们可以看到,基于字段的依赖注入 方式有很多缺点,我们应当避免使用基于字段的依赖注入,推荐的方法是使用基于构造函数和基于setter的依赖注入,对于必需的依赖项,建议使用基于构造函数 的注入,以使它们成为不可变的,并防止它们为null。对于可选的依赖项,建议使用基于Setter的注入。文章源自JAVA秀-https://www.javaxiu.com/10652.html
热门推荐:TikTok二面:“聊聊二维码扫码登录的原理”。太天真!这简历一看就是包装过的!阿里被罚182.28亿!回应来了~文章源自JAVA秀-https://www.javaxiu.com/10652.html

评论