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

        构造函数注入与字段注入

        时间:2024-05-10
        <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.

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

                  上一篇:IntStream 何时真正关闭?SonarQube S2095 是 IntStream 的误报吗? 下一篇:如何让 Sonar 忽略 codeCoverage 指标的某些类?

                  相关文章

                  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>

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