2025/2/25 中午《尚硅谷》——Spring——IOC容器创建对象基本步骤

目录

步骤一:编写pom.xml

步骤二:创建Student 的实体类(有参构造、无参构造、Getter和setter、toString)

步骤三:创建编写spring-ioc.xml

步骤四:编写测试文件IOCByXMLTest.java

步骤五:测试输出-成功


步骤一:编写pom.xml

<dependencies>

  • spring-context:Spring 核心依赖,包含了 Spring IOC 容器的实现。

  • junit:用于单元测试的依赖。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.atguigu.spring</groupId>
    <artifactId>spring_ioc_xml</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>


    <dependencies>
        <!-- 基于Maven依赖传递性,导入spring-context依赖即可导人当前所需所有jar包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.1</version>
        </dependency>

        <!-- junit测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

</project>

步骤二:创建Student 的实体类(有参构造、无参构造、Getter和setter、toString)

  1. 属性

    • sid:学生 ID。

    • sname:学生姓名。

    • age:学生年龄。

    • gender:学生性别。

  2. 构造方法

    • 提供了有参和无参构造方法,便于创建对象。

  3. Getter 和 Setter 方法

    • 用于访问和修改属性。

  4. toString 方法

    • 用于打印对象信息,便于调试。

package com.atguigu.spring.pojo;

public class Student {

    private Integer sid;

    private String sname;

    private Integer age;

    public String gender;

    public Student(Integer sid, String sname, Integer age, String gender) {
        this.sid = sid;
        this.sname = sname;
        this.age = age;
        this.gender = gender;
    }

    public Student() {
    }

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", sname='" + sname + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                '}';
    }
}

步骤三:创建编写spring-ioc.xml

  1. <beans>

    • 根元素,定义了 Spring 配置文件的命名空间和 Schema 位置。

  2. <bean>

    • 定义了一个 Bean,id 为 studentOneclass 为 com.atguigu.spring.pojo.Student

    • Spring 会根据这个配置创建 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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="studentOne" class="com.atguigu.spring.pojo.Student"></bean>

</beans>

步骤四:编写测试文件IOCByXMLTest.java

  1. ApplicationContext

    • Spring 的 IOC 容器接口,ClassPathXmlApplicationContext 是其实现类,用于从类路径加载配置文件。

  2. ioc.getBean("studentOne")

    • 从容器中获取 id 为 studentOne 的 Bean。

  3. System.out.println(studentOne)

    • 打印 Student 对象的信息。

package com.atguigu.spring.test;

import com.atguigu.spring.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class IOCByXMLTest {

    @Test
    public void testIOC(){
        //获取IOC容器
        ApplicationContext  ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
        //获取bean
        Student studentOne = (Student) ioc.getBean("studentOne");
        System.out.println(studentOne);
    }

}

步骤五:测试输出-成功

Student{sid=null, sname='null', age=null, gender='null'}

进程已结束,退出代码为 0