title: SpringFramework: set bean name programmatically
body: <p>For springframwork based application, when using xml to declare beans, bean id can be configured by passing a unique value and even a parameter and then solve the value at runtime.</p>
  
  <p>Now I hope to replace all xml configuration to java annotation.</p>
  
  <p>Say I want to create two database beans with different id.</p>
  
  <pre><code>bean.xml
  &lt;bean id="A.database" class="org.apache.commons.dbcp.BasicDataSource"&gt;
      &lt;!-- collaborators and configuration for this bean go here --&gt;
  &lt;/bean&gt;   
  
  &lt;bean id="B.database" class="org.apache.commons.dbcp.BasicDataSource"&gt;
      &lt;!-- collaborators and configuration for this bean go here --&gt;
  &lt;/bean&gt; 
  </code></pre>
  
  <p>Then I optimize the upper code to one bean with two property file for two application</p>
  
  <pre><code>bean.xml
  &lt;bean id="${appName.database}"   class="org.apache.commons.dbcp.BasicDataSource"&gt;
      &lt;!-- collaborators and configuration for this bean go here --&gt;
  &lt;/bean&gt; 
  
  applicationA.properties    
  appName.database=A.database
  
  applicationB.properties
  appName.database=B.database
  </code></pre>
  
  <p>The whole application is composed of "framework" module which provides beans common for each application, like database bean, jdbcTemplate bean, and "application" module which provides property value for placeholder and initializes beans with unique id. So even if I start multiple application at the same time, they will find corresponding bean from the context.</p>
  
  <p>Generally speaking, I hope to do    </p>
  
  <pre><code>@Bean(name = "${beanName}")
  public ABean getBean() {}
  </code></pre>
  
  <p>and resolve ${beanName} at application level.</p>