title: Spring dynamic injection, factory-like pattern
body: <p>A continuation from <a href="https://stackoverflow.com/questions/10384473/dependency-injection-delayed-injection-praxis/10414747">Dependency injection, delayed injection praxis</a>. I have the Main class:</p>
  
  <pre><code>package test;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  import org.springframework.stereotype.Component;
  
  import java.util.List;
  import java.util.Scanner;
  
  @Component
  public class Main {
      @Autowired
      private StringValidator stringValidator;
  
      @Autowired
      private StringService stringService;
  
      @Autowired
      private ValidationService validationService;
  
      public void main() {
          scanKeyboardCreateLists();
  
          stringValidator.validate();
  
          final List&lt;String&gt; validatedList = stringValidator.getValidatedList();
          for (String currentValid : validatedList) {
              System.out.println(currentValid);
          }
      }
  
      private void scanKeyboardCreateLists() {
          //Let's presume the user interacts with the GUI, dynamically changing the object graph...
          //Needless to say, this is past container initialization...
          Scanner scanner = new Scanner(System.in);
          int choice = scanner.nextInt();
  
          //Delayed creation, dynamic
          if (choice == 0) {
              stringService.createList();
              validationService.createList();
          } else {
              stringService.createSecondList();
              validationService.createSecondList();
          }
      }
  
      public static void main(String[] args) {
          ApplicationContext container = new ClassPathXmlApplicationContext("/META-INF/spring/applicationContext.xml");
          container.getBean(Main.class).main();
      }
  }
  </code></pre>
  
  <p>And the object graph is dynamically created, depending on the user interaction. I solved the application coupling, allowing me to test this very simply. <strong>Also, since the lists are maintained by the container, the dynamic nature of this application(and every other) is irrelevant, since they can be requested any time the application needs them, maintaining their elements.</strong></p>
  
  <p>The rest of the code is here:</p>
  
  <pre><code>package test;
  
  import java.util.List;
  
  public interface Stringable {
      List&lt;String&gt; getStringList();
  }
  
  package test;
  
  import org.springframework.stereotype.Component;
  
  import java.util.ArrayList;
  
  @Component
  public class StringList extends ArrayList&lt;String&gt; {
  }
  
  package test;
  
  import org.springframework.stereotype.Component;
  
  import javax.inject.Inject;
  import java.util.ArrayList;
  import java.util.List;
  
  @Component
  public class StringService implements Stringable {
  
      private List&lt;String&gt; stringList;
  
      @Inject
      public StringService(final ArrayList&lt;String&gt; stringList) {
          this.stringList = stringList;
      }
  
      //Simplified
      public void createList() {
          stringList.add("FILE1.txt");
          stringList.add("FILE1.dat");
          stringList.add("FILE1.pdf");
          stringList.add("FILE1.rdf");
      }
  
      public void createSecondList() {
          stringList.add("FILE2.txt");
          stringList.add("FILE2.dat");
          stringList.add("FILE3.pdf");
          stringList.add("FILE3.rdf");
      }
  
      @Override
      public List&lt;String&gt; getStringList() {
          return stringList;
      }
  }
  
  package test;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Component;
  
  import java.util.ArrayList;
  import java.util.List;
  
  @Component
  public class StringValidator {
      private List&lt;String&gt; stringList;
      private List&lt;String&gt; validationList;
  
      private final List&lt;String&gt; validatedList = new ArrayList&lt;String&gt;();
  
      @Autowired
      public StringValidator(final ArrayList&lt;String&gt; stringList,
                             final ArrayList&lt;String&gt; validationList) {
          this.stringList = stringList;
          this.validationList = validationList;
      }
  
      public void validate() {
          for (String currentString : stringList) {
              for (String currentValidation : validationList) {
                  if (currentString.equalsIgnoreCase(currentValidation)) {
                      validatedList.add(currentString);
                  }
              }
          }
      }
  
      public List&lt;String&gt; getValidatedList() {
          return validatedList;
      }
  }
  
  package test;
  
  import java.util.List;
  
  public interface Validateable {
      List&lt;String&gt; getValidationList();
  }
  
  package test;
  
  import org.springframework.stereotype.Component;
  
  import java.util.ArrayList;
  
  @Component
  public class ValidationList extends ArrayList&lt;String&gt; {
  }
  
  package test;
  
  import org.springframework.stereotype.Component;
  
  import javax.inject.Inject;
  import java.util.ArrayList;
  import java.util.List;
  
  @Component
  public class ValidationService implements Validateable {
  
      private List&lt;String&gt; validationList;
  
      @Inject
      public ValidationService(final ArrayList&lt;String&gt; validationList) {
          this.validationList = validationList;
      }
  
      //Simplified...
      public void createList() {
          validationList.add("FILE1.txt");
          validationList.add("FILE2.txt");
          validationList.add("FILE3.txt");
          validationList.add("FILE4.txt");
      }
  
      public void createSecondList() {
          validationList.add("FILE5.txt");
          validationList.add("FILE6.txt");
          validationList.add("FILE7.txt");
          validationList.add("FILE8.txt");
      }
  
      @Override
      public List&lt;String&gt; getValidationList() {
          return validationList;
      }
  }
  </code></pre>
  
  <p>Does anyone know how would I solve the method call createList() or createSecondList() - without using the constructor which pretty much forces the design. I was thinking of a factory, but a factory for every class in a project of a bigger magnitude doesn't seem like a good idea.</p>
  
  <p>Something like:</p>
  
  <pre><code>&lt;bean ... factory-method="..." depends-on="..." lazy-init="..."/&gt;
  </code></pre>
  
  <p>And in the factory method instantiate the class and call the method createList().
  Or call it like this, from some method - which again looks bad, forcing the method to have the responsibility to instantiate the object graph.</p>
  
  <p>The picture of the runtime dependencies that I want to resolve in runtime is bellow:</p>
  
  <p><img src="https://i.stack.imgur.com/ja6i2.jpg" alt="enter image description here"></p>
  
  <p><strong>Is there some other way I could use the container to achive dynamic lazy initalization depending on the user interaction?</strong></p>
  
  <p>Thank you.</p>