【问题标题】:How to give priority to spring bean with same id?如何优先使用相同id的spring bean?
【发布时间】:2017-02-04 09:35:12
【问题描述】:

在我们的项目中,我们使用带有Junit 的spring 进行Junit 测试。我们使用@ContextConfiguration 注释来加载多个文件。我们有两个类AbstractContextJUnitTest 和ContextJUnitTest 和ContextJUnitTest 扩展AbstractContextJUnitTest。

在代码流中,我注意到在具有不同 bean 类型的多个文件中存在相同的 bean Id。当我测试这些 Junit 并收到以下错误时。

错误:

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean 名为“voterId”的预计类型为 [com.spring.test2.Student] 但实际上是 [com.spring.test2.Parent]

类型

我的要求是 Student bean 应该加载 VoterId 而不是 Parent Bean。

下面是java文件和spring bean 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

<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);
    }
}

【问题讨论】:

    标签: java spring unit-testing spring-mvc junit4


    【解决方案1】:

    你试过@Primary吗?

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

    您必须在任何需要的地方使用@Qualifier 代替com.spring.test2.Parent。

    或者你可以得到类型为的bean:

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

    【讨论】:

    • @sidharthapani 试试这个 - applicationContext.getBeansOfType(Student.class).get("voterId")
    【解决方案2】:

    这可能是因为您按该顺序扩展了类..并且您的 test.xml 没有 Student 的任何 bean。所以它只是遵循继承并找到父级。 下面的几行使它首先在 test.xml 中查找 bean voterid 并在那里找到它。 ContextConfiguration(locations={"classpath:/com/spring/test2/test.xml"}) 公共类 ContextJUnitTest

    【讨论】:

    • 我可以覆盖 Junit 类中的位置属性吗?我的意思是使用任何方法?
    • 你为什么要这样做...只需在各自的 xml 中交换父和学生 bean...它应该可以正常工作。
    • 我发布的是我的项目的示例。但在我的项目中,我不应该改变这一点。
    猜你喜欢
    • 2018-12-17
    • 1970-01-01
    • 2016-11-21
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多