一,通过名字自动注入
1,复制spring40203 改名为spring40204,修改beans.xml,
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"
default-autowire="byName">
<bean id="dog3" class="com.cruise.entity.Dog">
<property name="name" value="Jack">property>
bean>
<bean id="dog" class="com.cruise.entity.Dog">
<property name="name" value="Tom">property>
bean>
<bean id="people1" class="com.cruise.entity.People">
<property name="id" value="1">property>
<property name="name" value="张三">property>
<property name="age" value="11">property>
bean>
beans>
2,修改People类,其中Dog的属性名决定装配哪一个bean,例如private Dog dog3(get set toString方法要重新生成);
package com.cruise.entity;
public class People {
private int id;
private String name;
private int age;
private Dog dog;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog.getName() + "]";
}
}
3,修改T类,运行-测试
package com.cruise.test;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cruise.entity.People;
public class T {
private ClassPathXmlApplicationContext CPXAC=null;
@Before
public void setUp() throws Exception {
CPXAC= new ClassPathXmlApplicationContext("beans.xml");
}
@Test
public void test1() {
People people1 = (People)CPXAC.getBean("people1");
System.out.println(people1);
}
}
二,通过类型自动注入
1,修改beans.xml文件,运行测试,
ps:需要类型唯一,例如:class相同的bean只能有一个,所以把id为dog3的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"
default-autowire="byType">
<bean id="dog" class="com.cruise.entity.Dog">
<property name="name" value="Tom">property>
bean>
<bean id="people1" class="com.cruise.entity.People">
<property name="id" value="1">property>
<property name="name" value="张三">property>
<property name="age" value="11">property>
bean>
beans>
2, 运行 测试(T类不用修改)
@Test
public void test1() {
People people1 = (People)CPXAC.getBean("people1");
System.out.println(people1);
}
三,通过构造方法自动注入,此方法与类型方法基本一直,只是需要在People中增加构造方法。
1,修改beans.xml,
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"
default-autowire="constructor">
<bean id="dog" class="com.cruise.entity.Dog">
<property name="name" value="Tom">property>
bean>
<bean id="people1" class="com.cruise.entity.People">
<property name="id" value="1">property>
<property name="name" value="张三">property>
<property name="age" value="11">property>
bean>
beans>
2,修改People类,添加构造方法,全参构造方法中加一个Dog即可,
public People(Dog dog) {
super();
this.dog = dog;
}
public People() {
super();
}
3,运行测试
@Test
public void test1() {
People people1 = (People)CPXAC.getBean("people1");
System.out.println(people1);
}