关于IOC容器根据类名获取bean对象的说明

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);
    }
}

第一行报错
在这里插入图片描述
第二行报错
在这里插入图片描述
第三行正确:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值