简体   繁体   English

如何优先考虑具有相同id的spring bean?

[英]How to give priority to spring bean with same id?

In our project we are using spring with Junit for Junit testing. 在我们的项目中,我们使用带有Junit spring进行Junit测试。 We have used @ContextConfiguration annotation for loading multiple file. 我们使用@ContextConfiguration批注来加载多个文件。 We have two classes AbstractContextJUnitTest and ContextJUnitTest and ContextJUnitTest extends AbstractContextJUnitTest . 我们有两个类AbstractContextJUnitTestContextJUnitTestContextJUnitTest扩展了AbstractContextJUnitTest

During code flow I have noticed that same bean Id in multiple files with different bean types . 在代码流期间,我注意到具有不同bean类型的多个文件中的相同bean ID When I am testing these Junits and getting the below error. 当我测试这些Junits并得到以下错误。

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] org.springframework.beans.factory.BeanNotOfRequiredTypeException:名为'voterId'的Bean应该是[com.spring.test2.Student]类型,但实际上是[com.spring.test2.Parent]类型

My requirement is Student bean should load with VoterId instead of Parent Bean. 我的要求是Student bean应该使用VoterId而不是Parent Bean加载。

Below are the java files and spring bean xml files: 下面是java文件和spring bean xml文件:

test.xml: 的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 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 AbstractContextJUnitTest.java

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


}

ContextJUnitTest.java 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);
    }
}

Did you tried @Primary ? 你试过@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. 您必须在任何需要的地方使用@Qualifier进行com.spring.test2.Parent

Or you can get the bean with type as: 或者您可以获取类型为bean的bean:

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

This is probably because you have extended classes in that order..and your test.xml doesn't have any bean with of Student. 这可能是因为你按照那个顺序扩展了类。而你的test.xml没有任何带有Student的bean。 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. 下面的行使它首先在test.xml中查找bean voterid并在那里找到它。 ContextConfiguration(locations={"classpath:/com/spring/test2/test.xml"}) public class ContextJUnitTest ContextConfiguration(locations = {“classpath:/com/spring/test2/test.xml”})公共类ContextJUnitTest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM