Java 学习【框架篇(一)】Spring(二)学框架前的说明 & 前期准备 & 一个 hellospring 程序

学框架前的说明

思想的学习不同于代码的学习,思想的学习要多去练习,练习的时候要带入问题去练习,不然很难成长。
在这里插入图片描述

前期准备

下载

  1. 官网:https://spring.io/projects/spring-framework#overview

  2. 官方下载地址:https://repo.spring.io/ui/native/release/org/springframework/spring
    在这里插入图片描述
    在这里插入图片描述

  3. Github: https://github.com/spring-projects/spring-framework/releases

  4. Maven: 导入 MVC
    在这里插入图片描述

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.0.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.2.0.RELEASE</version>
    </dependency>
    
    

官方文档

英文文档
中文文档

Spring 七大模块

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3. hellospring

3.1 导入依赖

在这里插入图片描述

新建 Maven 模块
在这里插入图片描述
此时由于父依赖已导入包 -> 此项目无需再导

3.2 编写相关代码

3.2.1 编写一个 Hello POJO 类

在这里插入图片描述

3.2.2 编写 spring 文件

便于理解先命名为 beans.xml(官方为 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>

在这里插入图片描述
配置文件上下文 -> 此后才会有小叶子
在这里插入图片描述
在这里插入图片描述


补充:
ref:引用 Spring 容器中创建好的对象
value:具体的值,基本数据类型
在这里插入图片描述
在这里插入图片描述

3.2.3 测试

在这里插入图片描述
new CPX -> 拿到 Spring 的容器
工厂模式
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
POJO 类被 spring 托管后 左边会有叶子图标,点击可以跳转到 bean 配置容器中
若没有图标说明还未被托管,不可用
在这里插入图片描述


至此, 用户只需修改配置文件即可;

猜你喜欢

转载自blog.csdn.net/weixin_46644403/article/details/121613616