<legend id='neAWQ'><style id='neAWQ'><dir id='neAWQ'><q id='neAWQ'></q></dir></style></legend>
  • <tfoot id='neAWQ'></tfoot>
  • <small id='neAWQ'></small><noframes id='neAWQ'>

    <i id='neAWQ'><tr id='neAWQ'><dt id='neAWQ'><q id='neAWQ'><span id='neAWQ'><b id='neAWQ'><form id='neAWQ'><ins id='neAWQ'></ins><ul id='neAWQ'></ul><sub id='neAWQ'></sub></form><legend id='neAWQ'></legend><bdo id='neAWQ'><pre id='neAWQ'><center id='neAWQ'></center></pre></bdo></b><th id='neAWQ'></th></span></q></dt></tr></i><div id='neAWQ'><tfoot id='neAWQ'></tfoot><dl id='neAWQ'><fieldset id='neAWQ'></fieldset></dl></div>

        <bdo id='neAWQ'></bdo><ul id='neAWQ'></ul>

        构造函数注入与字段注入

        Constructor injection vs Field injection(构造函数注入与字段注入)
        <tfoot id='lMTiy'></tfoot>
          <bdo id='lMTiy'></bdo><ul id='lMTiy'></ul>
            • <small id='lMTiy'></small><noframes id='lMTiy'>

                <i id='lMTiy'><tr id='lMTiy'><dt id='lMTiy'><q id='lMTiy'><span id='lMTiy'><b id='lMTiy'><form id='lMTiy'><ins id='lMTiy'></ins><ul id='lMTiy'></ul><sub id='lMTiy'></sub></form><legend id='lMTiy'></legend><bdo id='lMTiy'><pre id='lMTiy'><center id='lMTiy'></center></pre></bdo></b><th id='lMTiy'></th></span></q></dt></tr></i><div id='lMTiy'><tfoot id='lMTiy'></tfoot><dl id='lMTiy'><fieldset id='lMTiy'></fieldset></dl></div>
                  <tbody id='lMTiy'></tbody>

              • <legend id='lMTiy'><style id='lMTiy'><dir id='lMTiy'><q id='lMTiy'></q></dir></style></legend>

                  本文介绍了构造函数注入与字段注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  注入任何服务时,我有两种选择:

                  When injecting any services, I have two choices :

                  字段注入:

                   @Inject 
                   private MyService myService;
                  

                  构造函数注入:

                  private MyService myService; 
                  
                  @Inject
                  public ClassWhereIWantToInject(MyService mySerivce){
                      this.myService = myService;
                  }
                  

                  为什么构造函数注入优于字段注入?

                  推荐答案

                  我发现字段注入只有两个缺点.

                  I found only two disadvantages in the field injection.

                  • 在测试对象时很难注入模拟.(可以使用来自 Mockito 的 @InjectMocks 解决)

                  循环依赖.如果 bean A 依赖于 bean B 并且 bean B 需要 bean A.如果你有构造函数注入很容易找到它.

                  Circle dependencies. If bean A depends on bean B and bean B needs bean A. If you have the constructor injection it easy to find it.

                  这篇关于构造函数注入与字段注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                  1. <small id='6Sq08'></small><noframes id='6Sq08'>

                      <bdo id='6Sq08'></bdo><ul id='6Sq08'></ul>
                      <i id='6Sq08'><tr id='6Sq08'><dt id='6Sq08'><q id='6Sq08'><span id='6Sq08'><b id='6Sq08'><form id='6Sq08'><ins id='6Sq08'></ins><ul id='6Sq08'></ul><sub id='6Sq08'></sub></form><legend id='6Sq08'></legend><bdo id='6Sq08'><pre id='6Sq08'><center id='6Sq08'></center></pre></bdo></b><th id='6Sq08'></th></span></q></dt></tr></i><div id='6Sq08'><tfoot id='6Sq08'></tfoot><dl id='6Sq08'><fieldset id='6Sq08'></fieldset></dl></div>
                      <tfoot id='6Sq08'></tfoot>

                        <tbody id='6Sq08'></tbody>
                        1. <legend id='6Sq08'><style id='6Sq08'><dir id='6Sq08'><q id='6Sq08'></q></dir></style></legend>