1. 项目构架
Address.java
public class Address {
private String city;
private String street;
省略了 getter 和setter
}
Phone.java
public class Phone {
private String brand;
private double price;
省略了 getter 和setter
}
Person.java
public class Person {
private String name;
private int age;
private Phone phone;
private Address address;
省略了 getter 和setter
}
2. 配置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"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<bean id="address" class="com.baidu.entity.Address"
p:city="ShangHai" p:street="LongGang"></bean>
<bean id="phone" class="com.baidu.entity.Phone"
p:brand="HuaWei" p:price="4899"></bean>
<!-- 手动装配 -->
<bean id="person1" class="com.baidu.entity.Person"
p:name="Dave" p:age="45" p:phone-ref="phone" p:address-ref="address">
</bean>
<!-- 自动装配 byName :可以使用autowire 属性指定自动装配的方式
byName 根据bean 的名字 和当前 bean 的setter 风格的属性名进行自动装配。
若有匹配的,则进行自动装配,若没有,则不装配
-->
<bean id="person2" class="com.baidu.entity.Person"
p:name="Jim" p:age="20" autowire="byName" ></bean>
<bean id="phone2" class="com.baidu.entity.Phone"
p:brand="Mi" p:price="1999"></bean>
<!-- 自动装配 byType :可以使用autowire 属性指定自动装配的方式
byType 根据bean 的名字 和当前 bean 的属性的类型进行自动装配,若IOC 容器中有1 个以上的和类型匹配的bean,则抛异常
-->
<bean id="person3" class="com.baidu.entity.Person"
p:name="Jim" p:age="20" autowire="byType" ></bean>
</beans>
3. 测试方法
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpringPerson {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person1 = (Person) ac.getBean("person");
System.out.println(person1);
System.out.println();
Person person2 = (Person) ac.getBean("person");
System.out.println(person2);
System.out.println();
Person person3 = (Person) ac.getBean("person");
System.out.println(person3);
}
}
4. 测试结果
person1: Person [name=Dave, age=45, phone=Phone [brand=HuaWei, price=4899.0], address=Address [city=ShangHai, street=LongGang]]
person2: Person [name=Jim, age=20, phone=Phone [brand=HuaWei, price=4899.0], address=Address [city=ShangHai, street=LongGang]]
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'person3' defined in class path resource [applicationContext.xml]: Unsatisfied dependency expressed through bean property 'phone': : No qualifying bean of type [com.baidu.entity.Phone] is defined: expected single matching bean but found 2: phone,phone2; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.baidu.entity.Phone] is defined: expected single matching bean but found 2: phone,phone2
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1278)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1170)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.baidu.entity.TestSpringPerson.main(TestSpringPerson.java:9)
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.baidu.entity.Phone] is defined: expected single matching bean but found 2: phone,phone2
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:967)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1263)
... 13 more