使用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. SAP HR模块的基础数据表和增强配置

    信息类型是SAP HR模块数据单元,用于对人员数据的记录和维护,是HR的基础.信息类型按照其创建方式的不同可以分为:人事信息类型.组织信息类型.信息类型数据的维护主要在事物码PA30.PA40.po1 ...

  2. 今日Q群:QQ群众群友反馈问题的归纳总结

    今日Q群:QQ群群友反馈问题的归纳总结     今天Q群里还算比较活跃,归纳总结后主要有以下几类问题: 一.如何在Excel中按指定规则对有颜色的单元格进行过滤删选 具体的解决办法,请参照今天发布微信 ...

  3. android中清空一个表---类似truncate table 表名 这样的功能 android sqlite 清空数据库的某个表

    public void clearFeedTable(){ String sql = "DELETE FROM " + FEED_TABLE_NAME +";" ...

  4. ubuntu安装phpVirtualBox web服务

    首先确保已经安装好了 virtualbox,打开了 vboxweb-service $ sudo ls /etc/init.d | grep vboxweb-service     然后, Insta ...

  5. php判断文件存在是用file_exists 还是 is_file

    From: http://www.php100.com/html/php/hanshu/2013/0905/4672.html [导读] 在写程序时发现在判断文件是否存在时,有两种写法,有的人用了is ...

  6. Vue中计算属性与class,style绑定

    var vm=new Vue({ el:'#app', data:{ a:2, }, computed:{ //这里的b是计算属性:默认getter b:{ get:function(){ retur ...

  7. Docker命令之 run

    docker run :创建一个新的容器并运行一个命令 语法 docker run [OPTIONS] IMAGE [COMMAND] [ARG...] OPTIONS说明: -a stdin: 指定 ...

  8. 第一个jquery程序

    <html> <head></head> <script type="text/javascript" src="jquery- ...

  9. ngx-bootstrap学习笔记(一)-popover

    前言 这月做了个ng2模块,其中有个校验功能,当校验不通过时给出提示,项目中使用jQuery实现,今天才发现ngx-bootstrap已经有现成功能了,且可封装成通用组件放入shareModule,使 ...

  10. Linux 查看系统硬件信息

    linux查看系统的硬件信息,并不像windows那么直观,这里我罗列了查看系统信息的实用命令,并做了分类,实例解说. cpu lscpu命令,查看的是cpu的统计信息. blue@blue-pc:~ ...