注意事项:

  1. 使用 2024-03-14 发布的 Spring 5.3.33 版本
  2. IDE 工具使用了 Intellij IDEA,同时为了简化不必要的内容没单独配置 Gradle 环境
  3. JDK 版本采用 Eclipse Temurin 1.8/11 均可

下载源码

下载 SpringFramework 源码,本次选择 5.3.33 版本,发布日期 2024-03-14,通过 Intellij IDEA 打开。

Gihub地址: https://github.com/spring-projects/spring-framework/releases/tag/v5.3.33

配置 Gradle 环境

由于国内下载 gradle 比较慢可以考虑使用腾讯云镜像源,在 /gradle/wrapper/gradle-wrapper.properties 中修改 distributionUrl

distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-7.5.1-bin.zip

同时仓库地址可修改为阿里云,在 build.gradle 中配置阿里云镜像

allprojects {
repositories {
maven { url 'https://maven.aliyun.com/repository/public/' }
maven { url 'https://maven.aliyun.com/repository/spring/'}
maven { url 'https://maven.aliyun.com/repository/jcenter/'}
maven { url 'https://maven.aliyun.com/repository/gradle-plugin/'}
mavenLocal()
mavenCentral()
}
}

在 Gradle 先 Reload All Gradle Projects,再执行 build 命令进行编译。

期间碰到报错信息,报错详情:

spring-core\src\main\java\org\springframework\core\CoroutinesUtils.java:74: 警告: [deprecation] AccessibleObject中的isAccessible()已过时
if (method.isAccessible() && !KCallablesJvm.isAccessible(function)) {

解决方法:在 org.springframework.core.CoroutinesUtils#invokeSuspendingFunction 加上 @SuppressWarnings("deprecation")

若控制台输出中文乱码,Intellij IDEA 中设置 vm.properties

-Dfile.encoding=UTF-8

新建测试 Module

通过 gradle 新建一个 module,取名为 spring-research

module 新增完成后会自动在 settings.gradle 中引用 spring-research 模块,若没有自动引入可以手动加入

include 'spring-research'

在 spring-research 中的 build.gradle 中引入 spring-context

dependencies {
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
// 引入 spring-context 依赖
api(project(":spring-context"))
}

可选项:此外由于 Spring 源码工程配置了 checkStyle,在做测试类的时候有些方法不能够满足 Spring 的规范要求(由于使用 Intellij IDEA 自动生成或者格式化的代码不满足要求,比如 tab indent,实体类 this 指向,包括 import 隔行等规则),可以通过在 src/checkstyle/checkstyle.xml 中添加过滤规则:

<module name="BeforeExecutionExclusionFileFilter">
<property name="fileNamePattern" value="^.*\\spring\-research\\.*$"/>
</module>

新建实体类 Person

package io.github.linweiwang.bean;

public class Person {
private String name;
private int age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} @Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

在 resources.properties 中新建 spring-config.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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person" class="io.github.linweiwang.bean.Person">
<property name="name" value="王"/>
<property name="age" value="18"/>
</bean>
</beans>

在 Main 中调用 SpringContext 获取 Bean 的实例

package io.github.linweiwang;

import io.github.linweiwang.bean.Person;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
Person person = context.getBean(Person.class);
System.out.println(person);
}
}

运行成功即环境搭建成功!

Spring 源码阅读(一)环境搭建的更多相关文章

  1. spring源码解析(一) 环境搭建(各种坑的解决办法)

    上次搭建spring源码的环境还是两年前,依稀记得那时候也是一顿折腾,奈何当时没有记录,导致两年后的今天把坑重踩了一遍,还遇到了新的坑,真是欲哭无泪;为了以后类似的事情不再发生,这次写下这篇博文来必坑 ...

  2. Spring源码分析之环境搭建

    写在最前面 最近突然心血来潮,想看看源码,看看大牛都怎么码代码,膜拜下.首选肯定是spring大法,于是说干就干,从GitHub上下载spring-framework源码编译拜读. 环境搭建 安装JD ...

  3. Spring 源码学习之环境搭建

    一.下载Spring 源码 进入 https://github.com/spring-projects/spring-framework/tags 选择下载spring freamework的版本 h ...

  4. 构建后端第4篇之---spring 源码阅读构建环境

    解决 IDEA 创建 Gradle 项目没有src目录问题 in new model named zyt-study   root dir there are  a build.gradle plug ...

  5. Spring源码分析——调试环境搭建(可能是最省事的构建方法)

    1. 依赖工具 idea git jdk 1.8 + Gradle 2. 获取源码 从github https://github.com/spring-projects/spring-framewor ...

  6. Spring源码分析——(001)环境搭建

    1.官方参考 spring-framework的github链接:https://github.com/spring-projects/spring-framework 源码环境搭建官方参考1:考如何 ...

  7. Sping学习笔记(一)----Spring源码阅读环境的搭建

    idea搭建spring源码阅读环境 安装gradle Github下载Spring源码 新建学习spring源码的项目 idea搭建spring源码阅读环境 安装gradle 在官网中下载gradl ...

  8. 搭建 Spring 源码阅读环境

    前言 有一个Spring源码阅读环境是学习Spring的基础.笔者借鉴了网上很多搭建环境的方法,也尝试了很多,接下来总结两种个人认为比较简便实用的方法.读者可根据自己的需要自行选择. 方法一:搭建基础 ...

  9. Spring源码阅读笔记01:源码阅读环境准备

    1. 写在前面 对于做Java开发的同学来说,Spring就像是一条绕不过去的路,但是大多数也只是停留在对Spring的简单使用层面上,对于其背后的原理所知不多也不愿深究,关于这个问题,我在平时的生活 ...

  10. Spring源码阅读-ApplicationContext体系结构分析

    目录 继承层次图概览 ConfigurableApplicationContext分析 AbstractApplicationContext GenericApplicationContext Gen ...

随机推荐

  1. ECharts 中国地图 vue

    <template> <div> <div id="china_map_box"> <div id="china_map&quo ...

  2. C++追根究底(持续更新)

    目录 头文件 " " 与 <>的区别 带 .h 和不带 .h 的区别 带 c前缀 和不带 c前缀 的区别 宏定义 特殊符号:#,##,#@ 防止一个头文件被重复包含 d ...

  3. Ubuntu 22.04 源码安装ST-Link V2过程详解

    一 首先安装依赖工具: A 安装预编译库: sudo apt-get install git make cmake libusb-1.0-0-dev B 安装gcc库: sudo apt-get in ...

  4. python下进行10进制转16进制不带0x并且将16进制转成小端序

    前记   python涉及到和硬件互交的部分,一般是需要发送十六进制的帧长的.所以,python这个转换还是经常使用的.笔者在这里遇到了一个问题.就做一个记录吧. 基本方法:  假如你熟悉python ...

  5. Linux下编译成静态库和动态库,引入到项目中

    目录 配置全局变量 编译动态库和静态库: 动态库编译 静态库编译 AS mk 方式加载静态库和动态库 配置build.gradle 加载静态库方法 将libget.a考入到项目中 配置Android. ...

  6. Ubuntu如何进救援模式

    linux的救援模式-1 详解在 Ubuntu 中引导到救援模式或紧急模式 这篇教程将介绍如何在 Ubuntu 22.04.20.04 和 18.04 LTS 版本中引导到 救援Rescue 模式或 ...

  7. 【福利】JetBrains 全家桶永久免费使用

    Jetbrains系列的IDE公认是最好的集成开发工具,但是收费且挺贵.我们以PhpStorm为例,新用户第一年需要199$,注意是$,还不是人民币,这个价格一上来肯定筛选掉一大批用户.确实好用,所以 ...

  8. 【论文阅读】NIDS对抗性机器学习综述

    基本信息 题目:Adversarial Machine Learning for Network Intrusion Detection Systems: A Comprehensive Survey ...

  9. C# OpenCvSharp 轮廓检测

    效果 代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data ...

  10. MySQL命令创建只读权限用户

    查询用户 select user,authentication_string,host,Select_priv,Delete_priv from mysql.user; 创建用户 CREATE USE ...