1.创建需要的实体类
public class Student { //学生实体类 private String name; //姓名 private Integer age; //年龄 private Grade grade; //年级 @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", grade=" + grade + "]"; } public Student() { super(); } public Student(String name, Integer age, Grade grade) { super(); this.name = name; this.age = age; this.grade = grade; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Grade getGrade() { return grade; } public void setGrade(Grade grade) { this.grade = grade; } }
public class Grade { //年级实体类 private String name; //年级名称 @Override public String toString() { return "Grade [name=" + name + "]"; } public Grade() { super(); } public Grade(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
public class SmallGrade extends Grade { //只是为了 验证 autowire="byType" }
2.创建对应的配置文件
<?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="grade" class="cn.bdqn.bean.Grade" p:name="2年级"/> <!--autowire="byName" 将域属性名称 作为ID 到容器中查询相同名称的Bean进行注入 --> <bean id="student" class="cn.bdqn.bean.Student" autowire="byName"/> </beans>
<?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="gradeaaaaa" class="cn.bdqn.bean.Grade" p:name="2年级"/> <!-- autowire="byType" 根据域属性的类型 到容器中查询 相同类型并且具有is-a关系的一个Bean进行注入 相同类型的bean不能多于一个 --> <bean id="student" class="cn.bdqn.bean.Student" autowire="byType"/> </beans>
3.创建测试类分类运行
public class StudentTest { @Test public void test01(){ ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml"); Student student=(Student) context.getBean("student"); System.out.println(student); } }
4.如果在autowire="byType" 的时候 在容器中在增加一个相同类型的bean或者有is-a的关系 会报错!
如下的代码 运行会报错
<?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="gradeaaa" class="cn.bdqn.bean.Grade" p:name="2年级"/> <bean id="smallgrade" class="cn.bdqn.bean.SmallGrade"/> <!-- autowire="byType" 根据域属性的类型 到容器中查询 相同类型并且具有is-a关系的一个Bean进行注入 相同类型的bean不能多于一个 --> <bean id="student" class="cn.bdqn.bean.Student" autowire="byType"/> </beans>
<?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:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="grade1" class="cn.bdqn.bean.Grade" p:name="1年级"/> <bean id="grade2" class="cn.bdqn.bean.Grade" p:name="2年级"/> <!-- autowire="byType" 根据域属性的类型 到容器中查询 相同类型并且具有is-a关系的一个Bean进行注入 相同类型的bean不能多于一个 --> <bean id="student" class="cn.bdqn.bean.Student" autowire="byType"/> </beans>