IntelliJ IDEA:Field injection is not recommended
使用IntelliJ IDEA进行开发的时候,code analyze的时候会出现提示“Field injection is not recommended”。
stackoverflow上有篇回答:http://stackoverflow.com/questions/39890849/what-exactly-is-field-injection-and-how-to-avoid-it
国外有篇文章:http://vojtechruzicka.com/field-dependency-injection-considered-harmful/
spring官方文档:http://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/beans.html
依赖注入方式:
1、通过构造方法
2、通过setters或者其它方法
3、通过反射直接注入到fields
一般的@Autowired就是通过第三种方式
注入的指导方针:
1、为了强制依赖,或者为了易变性,使用构造方法注入
2、为了可选的或者可变的依赖,使用setter注入
3、尽量避免使用直接在属性上注入
属性注入的坏处:
1、你不能使用属性注入的方式构建不可变对象。
2、你的类和依赖容器强耦合,不能再容器外使用。
3、你的类不能绕过反射(例如单元测试的时候)进行实例化,必须通过依赖容器才能实例化,这更像是集成测试。
4、实际的依赖被隐藏在外面,不是在构造方法或者其它方法里面反射的。
5、一个类经常会有超过10个的依赖。如果使用构造方法的方式注入的话,构造方法会有10个参数,明显有点蠢。但是如果使用属性注入的话就没有这样的限制。但是一个类有很多的依赖,是一个危险的标志,因为很有可能这个类完成了超过一件事,违背了单一职责原则。
总结:
尽量避免使用属性注入,虽然非常方便。
IntelliJ IDEA:Field injection is not recommended的更多相关文章
- 【已解决】Field injection is not recommended和Could not autowired. No beans of 'xxx' type found.
目录 问题 解决办法 备注 问题 在项目中,我们使用Spring的@Autowired注解去引入其他类时有时候阿里的编码规约插件就会提示:"Field injection is not re ...
- @Autowired注解警告Field injection is not recommended
在使用spring框架中的依赖注入注解@Autowired时,idea报了一个警告 大部分被警告的代码都是不严谨的地方,所以我深入了解了一下. 被警告的代码如下: @Autowired UserDao ...
- @Autowired 警告 Field injection is not recommended Spring @Autowired注入
问题: 一. 在IDEA升级2017版后,发现以前使用的 @Autowired 出现了个警告 Field injection is not recommended. @Autowired的三种使用方式 ...
- 开发软件-IntelliJ IDEA:百科
ylbtech-开发软件-IntelliJ IDEA:百科 IDEA 全称 IntelliJ IDEA,是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智 ...
- 错误号:1364 错误信息:Field 'platId' doesn't have a default value
1. 错误描述 错误号:1364 错误信息:Field 'platId' doesn't have a default value insert into `use`.`t_platform_scal ...
- SpringCloud报错: "Field discoveryClient in com.controller.DcController required a bean of type 'com.netflix.discovery.DiscoveryClient' that could not be found."
SpringCloud报错: "Field discoveryClient in com.controller.DcController required a bean of type 'c ...
- 错误号:1364 错误信息:Field 'platId' doesn't have a default value
1. 错误描写叙述 错误号:1364 错误信息:Field 'platId' doesn't have a default value insert into `use`.`t_platform_sc ...
- mybatis 一次执行多条SQL MySql+Mybatis+Druid之SqlException:sql injection violation, multi-statement not allow
如果用JDBC jdbc.jdbcUrl=jdbc:mysql://127.0.0.1:3306/database?useUnicode=true&characterEncoding=utf8 ...
- Portswigger web security academy:SQL injection
Portswigger web security academy:SQL injection 目录 Portswigger web security academy:SQL injection SQL ...
随机推荐
- SAP DBDI 网银接口实现案例
在财务共享中心SSC实施中,为了提高AP和对账的效率,不可避免的需要实现和网上银行的集成.笔者为各位分析该案例如下: 为什么要上网银? 2).和SAP的无缝集成 3).直接在SAP中和银联对接 4). ...
- 使用3ds Max制作简单卧室
一.介绍 学习目标:熟练使用“标准基本体”和“扩展基本体”内的按钮来创建对象. 软件环境:3ds Max2015 二.实验步骤 1,启动3ds Max,使用“长方体”工具在场景中创建一个长方体作为空间 ...
- struts2系列(一):struts2入门(struts2的产生、struts2的工作流程、搭建struts2开发环境)
一. struts2的产生 struts1的缺点: 1. ActionForm过多,而且这个ActionForm在很大程度上又和VO(POJO)重复 ...
- unity---各种资源对应的格式
我们打包AssetBundle后,Unity3D会根据文件的后缀名将文件转换为特定的类型对象存储起来,我们后期获取时需要根据这些类型取出打包的数据,这里记录下不同后缀文件打包后的类型. 文本格式 支持 ...
- Hadoop(HA)分布式集群部署
Hadoop(HA)分布式集群部署和单节点namenode部署其实一样,只是配置文件的不同罢了. 这篇就讲解hadoop双namenode的部署,实现高可用. 系统环境: OS: CentOS 6.8 ...
- asp.net 截屏
public class HomeController : Controller { // // GET: /Home/ static System.Windows.Forms.WebBrowser ...
- C# 窗体间传值(使用委托与自定义事件)
using System; using System.Drawing; using System.Windows.Forms; namespace 跨窗体调用控件 { public partial c ...
- TensorFlow:tf.train.Saver()模型保存与恢复
1.保存 将训练好的模型参数保存起来,以便以后进行验证或测试.tf里面提供模型保存的是tf.train.Saver()模块. 模型保存,先要创建一个Saver对象:如 saver=tf.train.S ...
- oracle装载表是什么?
oracle装载表即通过sqlloader的方式导入数据. Oracle 的SQL*LOADER可以将外部数据加载到数据库表中.下面是SQL*LOADER的基本特点: 1)能装入不同数据类型文件及多个 ...
- 理解端口与IP
理解IP和端口 端口,端口号,服务器端口------百科