当前位置: 首页 > article >正文

Spring的创建与使用

✏️作者:银河罐头
📋系列专栏:JavaEE

🌲“种一棵树最好的时间是十年前,其次是现在”

目录

  • 创建 Spring 项目
    • 创建maven项目
    • 添加 Spring 依赖
    • 创建启动类
  • 存储 Bean 对象
    • 创建 Bean
    • 把 Bean 对象存到 spring 中
  • 获取 Bean对象
    • 得到 Spring (上下文)对象
    • 从 spring 中获取 Bean
      • 通过名称获取
      • 通过类型获取
      • 通过名称 + 类型获取
    • 使用 Bean
    • ApplicationContext 和 BeanFactory 的区别

创建 Spring 项目

创建maven项目

image-20230430161731815

image-20230430162327513

image-20230430162954264

添加 Spring 依赖

配置Maven国内源。

1.idea 有 2 个 settings, 当前项目的配置源和新项目的配置源。这 2 个都要配置。

image-20230430164644315

image-20230430164717511

image-20230430165120136

image-20230430170111319

使用上面相同的方式去配置新项目的 国内源。

把仓库里的 jar 包全都删了。

重新下载 jar包

image-20230430171340780

image-20230430173657733

image-20230430173714511

选择 5 版本的(对应 jdk 8)

image-20230430173751523

image-20230430173807108

创建启动类

image-20230430174035230

存储 Bean 对象

在 Java 中,一个对象如果被使用多次,就可以称之为 Bean.

创建 Bean

public class Student {
    private void sayHi(){
        System.out.println("hi student");
    }
}

把 Bean 对象存到 spring 中

image-20230430192716247

<?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:content="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!--将 bean(com.spring.demo.Student) 存到 spring 当中,名字叫做 student-->
    <bean id="student" class="com.spring.demo.Student"></bean>
</beans>

只是"声明", 并没有真正存到 spring 当中.

获取 Bean对象

这里的 Bean 指的是 Spring Bean, 不是 Java Bean.

怎样从 Spring 容器中 获取 Bean 对象?

得到 Spring (上下文)对象

image-20230430194646471

image-20230430212645839

从 spring 中获取 Bean

通过名称获取

image-20230430195337167

这种获取方式不够优雅,如果 getBean()里传入的 id 在 xml 里查不到,此时将 null 强转类型就会报错。

通过类型获取

Student student = context.getBean(Student.class);

与通过名称获取相比通过类型获取不用进行类型强转了。

但是也会存在问题。

<bean id="student" class="com.spring.demo.Student"></bean>
<bean id="stu" class="com.spring.demo.Student"></bean>
<bean id="teacher" class="com.spring.demo.Teacher"></bean>

image-20230430210042881

当 spring 中存在一个 类的多个实例时,再根据类型来获取 Bean 就会报错!

那么这里 Student 类里的 student 和 stu 是同一个对象吗?

student == stu -> false

不是 同一个对象。

通过名称 + 类型获取

Student student = context.getBean("stu",Student.class);

获取 Bean 也就是 前面介绍的 “DI”, 依赖注入。

使用 Bean

public class App {
    public static void main(String[] args) {
        //1.获取 Spring 对象
        ApplicationContext context =
                new ClassPathXmlApplicationContext("spring-config.xml");
        //2.从 spring 容器中获取到 Bean 对象
        Student student = (Student) context.getBean("student");
        //3.使用 Bean (非必须)
        student.sayHi();
    }
}

image-20230430195503906

另一个获取 Spring 上下文对象的方式

public class App2 {
    public static void main(String[] args) {
        //1.得到 Spring 上下文对象
        BeanFactory beanFactory =
                new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
        //2.从 Spring 中获取 Bean 对象
        Student student = (Student) beanFactory.getBean("student");
        //3.使用 Bean
        student.sayHi();
    }
}

ApplicationContext 和 BeanFactory 的区别

那么这 2 种获取 Spring 上下文对象的方式有什么区别?

ApplicationContext:

public class Student {
    public Student(){
        System.out.println("do student init");
    }
    public void sayHi(){
        System.out.println("hi student");
    }
}
public class Teacher {
    public Teacher() {
        System.out.println("do teacher init");
    }
    public void sayHi(){
        System.out.println("hi teacher");
    }
}
<bean id="student" class="com.spring.demo.Student"></bean>
<bean id="teacher" class="com.spring.demo.Teacher"></bean>
public class App {
    public static void main(String[] args) {
        //1.获取 Spring 对象
        ApplicationContext context =
                new ClassPathXmlApplicationContext("spring-config.xml");
        //2.从 spring 容器中获取到 Bean 对象
        Student student = (Student) context.getBean("student");
        //3.使用 Bean (非必须)
        student.sayHi();
    }
}

image-20230430201422710

BeanFactory:

image-20230430201509496

区别:

image-20230430201836564

image-20230430201940601

  • ApplicationContext :在获取到 spring 上下文对象之后就会把 xml 里所有的 对象初始化加载到 spring 中。

  • BeanFactory :懒加载,在执行完 getBean() 之后,才会把 xml 中对应的 Bean 对象初始化加载到 spring 中。

性能角度:ApplicationContext 费内存,一次性加载,后续读取非常快。BeanFactory节省内存,调用时才会把 Bean 初始化加载到 Spring 中,效率不高。

image-20230430203550165

继承角度:BeanFactory 是 ApplicationContext 的父类。

功能角度: BeanFactory 和 ApplicationContext 是 Spring 的 2 个顶级的接口,BeanFactory 有基础功能访问容器,而 ApplicationContext 继承了 BeanFactory,除了有 BeanFactory 的功能之外,还扩展了其他的功能,比如国际化支持,资源访问支持,事件传播支持。


http://www.kler.cn/news/16767.html

相关文章:

  • 两分钟成为 ChatGPT 国内高手【不要再拿ChatGPT当百度用了】
  • 武忠祥老师每日一题||定积分基础训练(三)
  • markdown二元运算符
  • Msfconsole使用永恒之蓝入侵Win7并上传执行可执行文件
  • 【学习心得】Python多版本控制
  • 常用半导体器件
  • 数字孪生可视化平台开发 打通现实与虚拟世界
  • springboot第12集:DAO功能代码
  • vue基本语法
  • 通关MyBatis(上)
  • 二本做程序员有出路吗
  • python异常及其捕获
  • BM50-两数之和
  • ovs-vsctl 命令详解
  • js判断是否为null,undefined,NaN,空串或者空对象
  • 第一章--第一篇--了解 ChatGPT
  • 框架学习之KOCA框架简介
  • 【python基础语法八】正则表达式
  • MIT教授Tegmark:GPT-4敲响警钟,百年后人类何去何从丨智源大会嘉宾风采
  • 数据帧去掉VlanTag的代码(802.1Q)
  • go 语言环境安装(Windows 系统下安装)
  • ( 数组和矩阵) 566. 重塑矩阵 ——【Leetcode每日一题】
  • osg::Drawable类通过setDrawCallback函数设置回调函数的说明
  • 构建ChatGPT 镜像,并将其部署到 Docker 容器中。
  • 基于Matlab刻度盘识别角度计算
  • C++:计算机操作系统:多线程:高并发中的线程
  • ViveNAS - 一个基于LSM tree的文件存储实现 (一)
  • C++ srand()和rand()用法
  • hadoop伪分布式搭建教程
  • 【react从入门到精通】React JSX详解