Creature--------------->People-------------------->Student
第一部分
一、代码bean准备:
package com.shugen.ioc.test;
/**
* @ClassName Creatur
* @Description TOOD
* @Author shungen
* @Date 2022/2/27 16:23
* @Version 1.0
*/
public class Creature {
private Integer id;
public Creature() {
}
public Creature(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public String toString() {
return "Creature{" +
"id=" + id +
'}';
}
}
package com.shugen.ioc.test;
/**
* @ClassName People
* @Description TOOD
* @Author shungen
* @Date 2022/2/27 16:21
* @Version 1.0
*/
public class People extends Creature{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
'}';
}
}
package com.shugen.ioc.test;
/**
* @ClassName Student
* @Description TOOD
* @Author shungen
* @Date 2022/2/27 16:24
* @Version 1.0
*/
public class Student extends People{
private Integer StudentId;
public Integer getStudentId() {
return StudentId;
}
public void setStudentId(Integer studentId) {
StudentId = studentId;
}
@Override
public String toString() {
return "Student{" +
"StudentId=" + StudentId +
'}';
}
}
spring配置文件通过xml方式配置bean,将bean对象加入IOC容器
<?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">
<bean id="user" class="com.shugen.ioc.test.User"/>
<bean id="creature" class="com.shugen.ioc.test.Creature"/>
<bean id="people" class="com.shugen.ioc.test.People"/>
<bean id="student" class="com.shugen.ioc.test.Student"/>
</beans>
二、测试代码
/**
* @ClassName TestIOC
* @Description TOOD
* @Author shungen
* @Date 2022/2/27 16:16
* @Version 1.0
*/
public class TestIOC {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void test01(){
Creature creature = applicationContext.getBean(Creature.class);
System.out.println(creature);//产生错误
}
}
/**
* @ClassName TestIOC
* @Description TOOD
* @Author shungen
* @Date 2022/2/27 16:16
* @Version 1.0
*/
public class TestIOC {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void test01(){
People people = applicationContext.getBean(People.class);
System.out.println(people);
}
}
/**
* @ClassName TestIOC
* @Description TOOD
* @Author shungen
* @Date 2022/2/27 16:16
* @Version 1.0
*/
public class TestIOC {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void test01(){
Student student = applicationContext.getBean(Student.class);
System.out.println(student);
}
}
综上很明显,确实如此:
根据类型来获取bean时,在满足bean唯一性的前提下,其实只是看:『对象 instanceof 指定的类型』的返回结果,只要返回的是true就可以认定为和类型匹配,能够获取到。
接下来考虑有接口存在的情况是否如此呢?
第二部分
一、bean、接口等的准备
public interface Fly {
}
public class Bird implements Fly{
}
public class BigBird extends Bird{
}
<!--第二部分-->
<bean id="bird" class="com.shugen.ioc.test.Bird"/>
<bean id="bigBird" class="com.shugen.ioc.test.BigBird"/>
二、测试
public class TestIOC {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void test01(){
// Fly fly = applicationContext.getBean(Fly.class);
// Bird bird = applicationContext.getBean(Bird.class);
BigBird bigBird = applicationContext.getBean(BigBird.class);
}
}
第一行报错
第二行报错
第三行正确: