实体类Teacher
public class Teacher {
private long id;
private String name;
private Student student;
public Teacher() {}
public Teacher(Student student) {
this.student = student;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
autowired.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 手动注入 -->
<bean name="stu" class="com.briup.bean.Student">
<property name="id" value="1"></property>
<property name="name" value="tom"></property>
<property name="age" value="20"></property>
</bean>
<bean name="t" class="com.briup.bean.Teacher">
<property name="id" value="1"></property>
<property name="name" value="jack"></property>
<!--在teacher中手动注入stu 注释掉拿到的是null-->
<!-- <property name="student" ref="stu"></property> -->
</bean>
</beans>
测试类
@Test
public void ioc_autowired() {
try {
String path = "com/briup/ioc/autowired/autowired.xml";
ApplicationContext container =
new ClassPathXmlApplicationContext(path);
Teacher t = (Teacher) container.getBean("t");
System.out.println(t);
System.out.println(t.getId());
System.out.println(t.getName());
System.out.println(t.getStudent());
} catch (Exception e) {
e.printStackTrace();
}
}
结果getStudent是空的
自动注入的方式
1.autowire=“byName”:
spring容器会到当前的类中找property的名字,然后再根据这个名
字去spring容器中找有没有和这个property名字相同的对象,有的话,
就把这个对象当做参数放到setXxxx这个方法里面注入进来
bean name="student"是固定与实体类中的setStudent()相对应的,
不能是其他的
public void setStudent(Student student) {
this.student = student;
}
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 与setStudent对应 -->
<bean name="student" class="com.briup.bean.Student">
<property name="id" value="1"></property>
<property name="name" value="tom"></property>
<property name="age" value="20"></property>
</bean>
<!-- 自动注入student -->
<bean name="t" class="com.briup.bean.Teacher" autowire="byName">
<property name="id" value="1"></property>
<property name="name" value="jack"></property>
</bean>
</beans>
结果:
2.autowire=“byType”:
spring容器会根据当前类中的set方法里面参数的类型,
去容器中找相匹配的对象,如果没找到就算了,如果找到一个就注入进来,
如果找到多个,那么就会报错了
bean name="stu",不管name="名称",只要匹配到名称是Student类型就行了
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean name="stu" class="com.briup.bean.Student">
<property name="id" value="1"></property>
<property name="name" value="tom"></property>
<property name="age" value="20"></property>
</bean>
<bean name="t" class="com.briup.bean.Teacher" autowire="byType">
<property name="id" value="1"></property>
<property name="name" value="jack"></property>
</bean>
</beans>
3.default-autowire=“byName”,default-autowire=“byType”
了解即可,不常用
default-autowire="byName"
在根元素beans中加入这个属性,那么下面所有的bean都会使用byName的方式进行
自动注入,
如果在下面的某一个bean里面想使用其他的方式进行注入,可以用autowire=""
属性进行说明,或者某一个bean不想使用任何自动注入就使用autowire="no"
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="byName">
<bean name="stu" class="com.briup.bean.Student">
</bean>
</beans>