title: Using Hk2 to generate an instance with the specified constructor args
body: <p>I am using HK2 and the service locator pattern in my code. The following is the Interface:</p>
  
  <pre><code>package org.swx.nursing.tools.sqlfinder.gui;
  
  import org.jvnet.hk2.annotations.Contract;
  import org.swx.nursing.tools.sqlfinder.servicelocator.ServiceLocatorFactory;
  
  @Contract
  public interface GuiTemplate {
  
      public void createAndShowGUI(GuiTemplateCriteria criteria);
  
      public static final class Factory{
  
          //Disable public instantiation
          private Factory (){
              throw new AssertionError();
          }
  
          public static GuiTemplate create(){
              return ServiceLocatorFactory.getInstance().getService(GuiTemplate.class);
          };
      }
  }
  </code></pre>
  
  <p>the following is the Implementing class for the above interface:</p>
  
  <pre><code>package org.swx.nursing.tools.sqlfinder.gui;
  
  import org.jvnet.hk2.annotations.Service;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  @Service
  public final class GuiTemplateImpl implements GuiTemplate{
  
  
      private static final long serialVersionUID = 7583852594722977867L;
      private static final Logger LOGGER      = LoggerFactory.getLogger(GuiTemplateImpl.class);
  
      private static GuiTemplateCriteria  criteria;
  
      public GuiTemplateImpl(GuiTemplateCriteria  criteria) {
          this.criteria = criteria;
      }
  </code></pre>
  
  <p>If I want to get an instance of the above class so that i can pass in the arguments for the constructor, how do i do that?</p>
  
  <p>I know that the following would call the no-argument constructor &amp; return an object with the Implementing class:</p>
  
  <pre><code>ServiceLocatorUtilities.createAndPopulateServiceLocator().getService(GuiTemplate.class);
  </code></pre>
  
  <p>The API is here: <a href="https://hk2.java.net/hk2-api/apidocs/org/glassfish/hk2/api/ServiceLocator.html" rel="nofollow">https://hk2.java.net/hk2-api/apidocs/org/glassfish/hk2/api/ServiceLocator.html</a></p>
  
  <p>Please advise,</p>
  
  <p>Thanks!</p>