使用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的更多相关文章

  1. 【已解决】Field injection is not recommended和Could not autowired. No beans of 'xxx' type found.

    目录 问题 解决办法 备注 问题 在项目中,我们使用Spring的@Autowired注解去引入其他类时有时候阿里的编码规约插件就会提示:"Field injection is not re ...

  2. @Autowired注解警告Field injection is not recommended

    在使用spring框架中的依赖注入注解@Autowired时,idea报了一个警告 大部分被警告的代码都是不严谨的地方,所以我深入了解了一下. 被警告的代码如下: @Autowired UserDao ...

  3. @Autowired 警告 Field injection is not recommended Spring @Autowired注入

    问题: 一. 在IDEA升级2017版后,发现以前使用的 @Autowired 出现了个警告 Field injection is not recommended. @Autowired的三种使用方式 ...

  4. 开发软件-IntelliJ IDEA:百科

    ylbtech-开发软件-IntelliJ IDEA:百科 IDEA 全称 IntelliJ IDEA,是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智 ...

  5. 错误号: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 ...

  6. 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 ...

  7. 错误号: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 ...

  8. 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 ...

  9. Portswigger web security academy:SQL injection

    Portswigger web security academy:SQL injection 目录 Portswigger web security academy:SQL injection SQL ...

随机推荐

  1. (笔记)如何安装Arm-linux-gcc

      如何安装Arm-linux-gcc   安装交叉编译工具链: 1.首先以root用户登入 2.复制arm-linux-gcc-4.3.2.tgz到根目录下tmp文件夹里 3.解压命令tar xvz ...

  2. 了解VIM的寄存器

    VIM下的删除:delete; 复制:yank; 粘帖:put; 都会用到VIM下的相关寄存器,今天就说说这个寄存器的问题: VIM中有多种寄存器:包括: 有名寄存器,用名字("a-&quo ...

  3. e826. 获得和设置JSplitPane分开的位置

    The location of a divider is measured in pixels from either the left edge (in the case of a horizont ...

  4. Python property,属性

    參考资料 http://www.ibm.com/developerworks/library/os-pythondescriptors/ 顾名思义,property用于生成一个属性.通过操作这个属性. ...

  5. CentOS 7系统查看系统版本和机器位数

    前言 由于不经常使用linux,每当使用的时候就是安装软件,安装软件的时候就要选择安装包平台,是32位的还是64位的.这时候突然发现不知道怎么查,于是百度.虽然轻而易举百度出来,但仍旧没有自己的笔记看 ...

  6. c# 正则实践

    Regex reg = new Regex(@"<img[\s]+src[\s]*=[\s]*['""](?<picPath>.*)['"&q ...

  7. xml根据属性去重。如csprj去重

    public static void distinct(string filePath) { //1.创建XML文档对象 XmlDocument doc = new XmlDocument(); // ...

  8. fedora26在编译s3c2440内核时make menuconfig *** Unable to find the ncurses libraries

    [root@fedora-26 linux-2.6.32.2]# make menuconfig *** Unable to find the ncurses libraries or the *** ...

  9. session没保存,登录失败

    今天发现做的项目,登录不上 查了下原因,原来是session没保存上 查看php.ini文件,session.save_path="D:\php\tmp\tmp" 查看了下对应的目 ...

  10. 如何用BarTender将日期变量和序列号变量放一起打印成条码?

    刚接触BarTender 2016的小伙伴们可能对条码的数据源还不太搞的定,例如有时需要将日期变量和序列号变量放一起打印成条码,那如何简单达到目的呢?下面,小编教大家解决这一问题的三大步骤. 1.在B ...