title: How to give priority to spring bean with same id?
body: <p>In our project we are using spring with <code>Junit</code> for Junit testing. We have used <code>@ContextConfiguration</code> annotation for loading multiple file. We have two classes <code>AbstractContextJUnitTest</code> and <code>ContextJUnitTest</code> and <code>ContextJUnitTest</code> extends <code>AbstractContextJUnitTest</code>.</p>
  
  <p><strong><em>During code flow I have noticed that same bean Id in multiple files with different bean types</em></strong>. When I am testing these Junits and getting the below error.</p>
  
  <p><strong>Error:</strong></p>
  
  <blockquote>
    <p>org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean
    named 'voterId' is expected to be of type [com.spring.test2.Student]
    but was actually of type [com.spring.test2.Parent]</p>
  </blockquote>
  
  <p>My requirement is Student bean should load with VoterId instead of Parent Bean.</p>
  
  <p>Below are the java files and spring bean xml files:</p>
  
  <p><strong>test.xml:</strong></p>
  
  <pre><code>&lt;beans&gt; 
      &lt;context:annotation-config/&gt;
      &lt;bean id="voterId" class="com.spring.test2.Parent"&gt;
      &lt;property name="Name" value="hai"/&gt;
      &lt;/bean&gt;
      &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /&gt;
          &lt;property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" /&gt;
          &lt;property name="username" value="system" /&gt;
          &lt;property name="password" value="system" /&gt;
      &lt;/bean&gt; 
      &lt;bean id="transactionManager"
  
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
  
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt; 
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>test1.xml</strong></p>
  
  <pre><code>&lt;beans&gt;
  
      &lt;context:annotation-config/&gt;
      &lt;bean id="voterId" class="com.spring.test2.Student"&gt;
          &lt;property name="name" value="hello"/&gt;
          &lt;property name="number" value="2080"/&gt;
       &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>AbstractContextJUnitTest.java</strong></p>
  
  <pre><code>@ContextConfiguration(locations="classpath:/com/spring/test2/test1.xml")
  public class AbstractContextJUnitTest extends AbstractTransactionalJUnit4SpringContextTests{
  
  
  }
  </code></pre>
  
  <p><strong>ContextJUnitTest.java</strong></p>
  
  <pre><code>@ContextConfiguration(locations={"classpath:/com/spring/test2/test.xml"})
  public class ContextJUnitTest extends AbstractContextJUnitTest{
  
  
      @Test
      public void testStudent(){
          Student stud=applicationContext.getBean("voterId",Student.class);
          assertEquals(stud.getNumber(), 2080);
      }
  }
  </code></pre>