天天看點

解決Bug=進步成長

Exception in thread "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.atguigu.spring.bean.Person] is defined: expected single matching bean but found 2: person1,person2

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:312)

    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:985)

    at com.atguigu.spring.bean.TestSpring.main(TestSpring.java:12)

錯誤原因:spring配置檔案定義了相同class的bean

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="person1" class="com.atguigu.spring.bean.Person">

        <property name="id" value="1"></property>

        <property name="name" value="張三"></property>

    </bean>

    <bean id="person2" class="com.atguigu.spring.bean.Person">

        <property name="id" value="2"></property>

        <property name="name" value="李四"></property>

    </bean>

</beans>

繼續閱讀