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

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

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

                  相关文档推荐

                  Slf4j LoggerFactory.getLogger and sonarqube(Slf4j LoggerFactory.getLogger 和 sonarqube)
                  Security - Array is stored directly(安全性 - 数组直接存储)
                  SonarQube quot;Class Not Foundquot; during Main AST Scan(SonarQube“找不到类在主 AST 扫描期间)
                  Integrate Spock#39;s test with Sonar(将 Spock 的测试与声纳集成)
                  How do I make Hudson/Jenkins fail if Sonar thresholds are breached?(如果违反声纳阈值,我如何让 Hudson/Jenkins 失败?)
                  automatically add curly brackets to all if/else/for/while etc. in a java code-base(自动将大括号添加到 java 代码库中的所有 if/else/for/while 等)

                  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>