使用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. c指针 --笔记2返回指针值的函数

    返回指针值的函数 一般带回指针值的函数,定义形式为: int *a (int x, int y); 看这个经典案例: #include <stdio.h> int main(int arg ...

  2. C#客户端(WinForm)开机自动启动实现

    /// <summary> /// 修改程序在注册表中的键值 /// </summary> /// <param name="isAuto">t ...

  3. android http json请求3种不同写法

    第一种: public static String invoke() {        String result = null;        try {             final Str ...

  4. Kilo 版 Keystone 数据库结构

    在安装完keystone并利用keystone-manage命令同步数据库后,mysql(我使用的存储后端)中新加了如下表: +------------------------+ | Tables_i ...

  5. B/S模式实现批量打包apk

    界面流程 界面例如以下: 这是一个使用html编写的界面,界面分为两半.两个frame.左边为操作栏,右边为控制台输出. 打包流程: 选择须要打包的渠道后,点击打包,等待server打包,并把日志输出 ...

  6. Oracle RAC集群资料收集

    RAC优势和劣势 http://tech.it168.com/a2012/0814/1384/000001384756_all.shtml 双机热备与RAC并行模式对比 WAS数据源设置 http:/ ...

  7. workerman定时器使用

    From: http://doc3.workerman.net/worker-development/add.html add int \Workerman\Lib\Timer::add(float ...

  8. com.alibaba.com.caucho.hessian.io.SerializerFactory getDeserializer

    2016-1-27 14:40:25 com.alibaba.com.caucho.hessian.io.SerializerFactory getDeserializer 警告: Hessian/B ...

  9. MariaDB安装 Apache安装

     

  10. categorys

    //spark-shell --driver-class-path /home/hadoop/test/mysqljdbc.jarimport java.sql.DriverManager// --q ...