title: Creating multiple beans of same type based on external config
body: <pre><code>class SomeClass() implements Runnable
  {
      private SomeDevice someDevice;
      private SomeOtherDevice someOtherDevice;
      
      @Override
      public void run()
      {
          ...
          someDevice.doSomething();
          ...
          someOtherDevice.doSomething();
      }
  }
  </code></pre>
  <pre><code>@Configuration
  class Config
  {
      @Bean
      @Scope(&quot;prototype&quot;)
      public SomeDevice someDevice { return new SomeDevice() }
      @Bean
      @Scope(&quot;prototype&quot;)
      public SomeOtherDevice someOtherDevice { return new SomeOtherDevice() }
  }
  </code></pre>
  <p>I'm very, very new to Spring and have a bit of a complex thing to implement.</p>
  <p>I have an external configuration file that specifies how many of SomeDevice I will have, as well as what port each of SomeDevice will be listening on.
  An instance of SomeClass will be in charge of each SomeDevice. So I'll have SomeDevice1 running inside SomeClass1, SomeDevice2 running inside SomeClass2, etc.
  Each SomeClass will also need its own instance of a SomeOtherDevice.</p>
  <p>I wanted to be able to create these beans manually, so that I can:</p>
  <ol>
  <li>Read my external config file and create the proper number of SomeDevice(s)</li>
  <li>Specify each of their ports as per the external config, by calling someDevice.setPort()</li>
  <li>Put these inside their own instances of SomeClass.</li>
  <li>SomeClass will also need it's own instance of SomeOtherDevice (SomeOtherDevice does not need external config info)</li>
  </ol>
  <p>I've tried using bean factories to do this and I'm having trouble getting SomeClass to find the SomeDevice beans after I create them using bean factories. It can't find them by name, only by class. But because I'm going to have multiple SomeDevice.class beans, I want to be able to find them by name (and give them unique names when I create them). I'm also not even sure I'm approaching things in the &quot;best&quot; way. If anyone could point me in the right direction, I'd really appreciate it.</p>
  <p>EDIT: I forgot to mention that I would prefer not to change the source code of SomeDevice. So I can't add Spring annotations to that class unless it would be extremely necessary.</p>