How to give priority to spring bean with same id?

10,821

Solution 1

Did you tried @Primary?

<bean id="voterId" class="com.spring.test2.Student" primary="true">
        <property name="name" value="hello"/>
        <property name="number" value="2080"/>
</bean>

You have to use @Qualifier for com.spring.test2.Parent wherever you need.

Or you can get the bean with type as:

applicationContext.getBeansOfType(Student.class).get("voterI‌​d")

Solution 2

This is probably because you have extended classes in that order..and your test.xml doesn't have any bean with of Student. So it is simply following inheritance and found parent. Below lines make it look for bean voterid in test.xml first and it found it there. ContextConfiguration(locations={"classpath:/com/spring/test2/test.xml"}) public class ContextJUnitTest

Share:
10,821
sidhartha pani
Author by

sidhartha pani

Updated on June 04, 2022

Comments

  • sidhartha pani
    sidhartha pani almost 2 years

    In our project we are using spring with Junit for Junit testing. We have used @ContextConfiguration annotation for loading multiple file. We have two classes AbstractContextJUnitTest and ContextJUnitTest and ContextJUnitTest extends AbstractContextJUnitTest.

    During code flow I have noticed that same bean Id in multiple files with different bean types. When I am testing these Junits and getting the below error.

    Error:

    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]

    My requirement is Student bean should load with VoterId instead of Parent Bean.

    Below are the java files and spring bean xml files:

    test.xml:

    <beans> 
        <context:annotation-config/>
        <bean id="voterId" class="com.spring.test2.Parent">
        <property name="Name" value="hai"/>
        </bean>
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
            <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
            <property name="username" value="system" />
            <property name="password" value="system" />
        </bean> 
        <bean id="transactionManager"
    
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    
            <property name="dataSource" ref="dataSource" />
        </bean> 
    </beans>
    

    test1.xml

    <beans>
    
        <context:annotation-config/>
        <bean id="voterId" class="com.spring.test2.Student">
            <property name="name" value="hello"/>
            <property name="number" value="2080"/>
         </bean>
    </beans>
    

    AbstractContextJUnitTest.java

    @ContextConfiguration(locations="classpath:/com/spring/test2/test1.xml")
    public class AbstractContextJUnitTest extends AbstractTransactionalJUnit4SpringContextTests{
    
    
    }
    

    ContextJUnitTest.java

    @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);
        }
    }
    
  • sidhartha pani
    sidhartha pani about 7 years
    Can i override the locations attribute in Junit class. I mean using any method?
  • LNP
    LNP about 7 years
    why would you do that...just exchange the parent and student beans in respective xml's...it should work fine.
  • sidhartha pani
    sidhartha pani about 7 years
    What I posted is sample example for my project. But in my project I should not change this.
  • Arpit Aggarwal
    Arpit Aggarwal about 7 years
    @sidharthapani try this - applicationContext.getBeansOfType(Student.class).get("voterI‌​d")