package com.atChina.Test5;
public class Student {
private String name;
private int age;
private School school;
public Student(){
System.out.println("无参数构造方法...");
}
public void setSchool(School school) {
this.school = school;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", school=" + school
+ "]";
}
}
package com.atChina.Test5;
public class School {
private String address;
private String name;
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "School [address=" + address + ", name=" + name + "]";
}
public void setName(String name) {
this.name = name;
}
}
引用类型的自动注入--byName
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引用Spring的多个Schema空间的格式定义文件 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd ">
<!-- 引用类型的自动注入, 由框架给引用类型完成赋值,赋值的方式主要由byName,byType
1. byName(按名称注入): java类中引用类型的属性名和spring容器(xml配置文件)
中的<bean>的id名称一样,且数据类型是一样的,这样的bean对象能够赋值给引用类型
指定byName自动注入
<bean id="xx" class="yy" autowire="byName">
</bean>
-->
<bean id="student" class="com.atChina.Test5.Student" autowire="byName">
<property name="name" value="宋江"/>
<property name="age" value="20" />
<!-- <property name="school" ref="xuexiao"/> -->
</bean>
<!-- id的值要与java类中的属性名一致 -->
<bean id="school" class="com.atChina.Test5.School">
<property name="name" value="同济大学"/>
<property name="address" value="上海市" />
</bean>
</beans>
引用类型的自动注入--byType
package com.atChina.Test6;
public class PrimarySchool extends School {
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引用Spring的多个Schema空间的格式定义文件 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd ">
<!-- 引用类型的自动注入, 由框架给引用类型完成赋值,赋值的方式主要由byName,byType
1. byName(按名称注入): java类中引用类型的属性名和spring容器(xml配置文件)
中的<bean>的id名称一样,且数据类型是一样的,这样的bean对象能够赋值给引用类型
指定byName自动注入
<bean id="xx" class="yy" autowire="byName">
</bean>
2. byType(按类型注入):java类中引用类型的数据类型和spring容器(xml配置文件)中的
<bean>的class属性值是同源关系的,这样的bean可以赋值给引用类型
同源关系的:
1. java类中引用类型的数据类型和<bean>的class是一样的
2. java类中引用类型的数据类型和<bean>的class是父类和子类的关系
3 java类中引用类型的数据类型和<bean>的class是接口和实现类关系
指定byType自动注入
<bean id="xx" class="yy" autowire="byType">
</bean>
byType的自动注入注意的是,符合条件的对象只能有一个。
-->
<bean id="student" class="com.atChina.Test6.Student" autowire="byType">
<property name="name" value="吴用"/>
<property name="age" value="20" />
<!-- <property name="school" ref="xuexiao"/> -->
</bean>
<!--
<bean id="xuexiao" class="com.atChina.Test6.School">
<property name="name" value="同济大学"/>
<property name="address" value="上海市" />
</bean> -->
<bean id="priSchool" class="com.atChina.Test6.PrimarySchool">
<property name="name" value="实验小学"/>
<property name="address" value="上海市" />
</bean>
</beans>