title: Inject spring bean dynamically
body: <p>In a java-spring web-app I would like to be able to dynamically inject beans.
  For example I have an interface with 2 different implementations:</p>
  
  <p><a href="https://i.stack.imgur.com/H1rp6.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/H1rp6.jpg" alt="enter image description here"></a></p>
  
  <p>In my app I'm using some properties file to configure injections:</p>
  
  <pre><code>#Determines the interface type the app uses. Possible values: implA, implB
  myinterface.type=implA
  </code></pre>
  
  <p>My injections actually loaded conditionally relaying on the properties values in the properties file. For example in this case myinterface.type=implA wherever I inject MyInterface the implementation that will be injected will be ImplA (I accomplished that by extending the <a href="http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Conditional.html" rel="noreferrer">Conditional annotation</a>).</p>
  
  <p>I would like that during runtime - once the properties are changed the following will happen (without server restart):</p>
  
  <ol>
  <li>The right implementation will be injected. For example when setting <code>myinterface.type=implB</code> ImplB will be injected where-ever MyInterface is used</li>
  <li><a href="http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/env/Environment.html" rel="noreferrer">Spring Environment</a> should be refreshed with the new values and re-injected as well to beans.</li>
  </ol>
  
  <p>I thought of refreshing my context but that creates problems.
  I thought maybe to use setters for injection and re-use those setters once properties are re-configured. Is there a working practice for such a requirement?</p>
  
  <p>Any ideas?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>As some suggested I can use a factory/registry that holds both implementations (ImplA and ImplB) and returns the right one by querying the relevant property.
  If I do that I still have the second challenge - the environment. for example if my registry looks like this:</p>
  
  <pre><code>@Service
  public class MyRegistry {
  
  private String configurationValue;
  private final MyInterface implA;
  private final MyInterface implB;
  
  @Inject
  public MyRegistry(Environmant env, MyInterface implA, MyInterface ImplB) {
          this.implA = implA;
          this.implB = implB;
          this.configurationValue = env.getProperty("myinterface.type");
  }
  
  public MyInterface getMyInterface() {
          switch(configurationValue) {
          case "implA":
                  return implA;
          case "implB":
                  return implB;
          }
  }
  }
  </code></pre>
  
  <p>Once property has changed I should re-inject my environment. any suggestions for that?</p>
  
  <p>I know I can query that env inside the method instead of constructor but this is a performance reduction and also I would like to think of an ider for re-injecting environment (again, maybe using a setter injection?).</p>