【关于Spring那些事】——与君初相识

导读:本篇文章讲解 【关于Spring那些事】——与君初相识,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

第一个Spring程序

Spring是一个轻量级开源框架,由一个叫Rod Johnson的程序员在 2002 年提出并创建。

1.创建maven工程

在这里插入图片描述

2.添加Spring依赖

在maven项目的pom.xml文件中添加一下代码

<dependency>
   <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.18</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.3.18</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>5.3.18</version>
</dependency>

3.创建Spring配置文件

在Resources目录下创建applicationContext.xml文件

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="..." class="...">  
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <bean id="..." class="...">
        <!-- collaborators and configuration for this bean go here -->
    </bean>

    <!-- more bean definitions go here -->

</beans>

4.创建实体类

public class Student {

    private String id;
    private String name;
    private String sex;
    private int age;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                '}';
    }
}

5.配置applicationContext.xml文件

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--将指定类配置给Spring,创建Bean实例-->
    <bean id="student" class="com.gyh.ioc.pojo.Student"></bean>

</beans>

6.测试

public class Test {
    public static void main(String[] args) {
        //初始化Spring容器并加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //通过getBean方法获取指定的Bean,获取之后需要进行强制类型转换
        Student student = (Student) context.getBean("student");
        System.out.println(student);
    }
}

运行结果:
在这里插入图片描述
这里可以看到,我们没有使用new关键字来创建对象,而是通过Spring成功获取了Student的实现类对象,这就是SpringIoC容器的工作机制。

通过配置文件给对象的属性赋值

<bean id="student" class="com.gyh.ioc.pojo.Student">
        <property name="id" value="1001"/>
        <property name="name" value="Spring"/>
        <property name="sex" value=""/>
        <property name="age" value="18"/>
    </bean>
  • 运行结果
    在这里插入图片描述

因此我们不仅可以通过配置文件创建实现类的对象,还可以给对象的属性赋值。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/11491.html

(0)
小半的头像小半

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!