Spring 源码阅读(一)环境搭建
注意事项:
- 使用 2024-03-14 发布的 Spring 5.3.33 版本
- IDE 工具使用了 Intellij IDEA,同时为了简化不必要的内容没单独配置 Gradle 环境
- 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 源码阅读(一)环境搭建的更多相关文章
- spring源码解析(一) 环境搭建(各种坑的解决办法)
上次搭建spring源码的环境还是两年前,依稀记得那时候也是一顿折腾,奈何当时没有记录,导致两年后的今天把坑重踩了一遍,还遇到了新的坑,真是欲哭无泪;为了以后类似的事情不再发生,这次写下这篇博文来必坑 ...
- Spring源码分析之环境搭建
写在最前面 最近突然心血来潮,想看看源码,看看大牛都怎么码代码,膜拜下.首选肯定是spring大法,于是说干就干,从GitHub上下载spring-framework源码编译拜读. 环境搭建 安装JD ...
- Spring 源码学习之环境搭建
一.下载Spring 源码 进入 https://github.com/spring-projects/spring-framework/tags 选择下载spring freamework的版本 h ...
- 构建后端第4篇之---spring 源码阅读构建环境
解决 IDEA 创建 Gradle 项目没有src目录问题 in new model named zyt-study root dir there are a build.gradle plug ...
- Spring源码分析——调试环境搭建(可能是最省事的构建方法)
1. 依赖工具 idea git jdk 1.8 + Gradle 2. 获取源码 从github https://github.com/spring-projects/spring-framewor ...
- Spring源码分析——(001)环境搭建
1.官方参考 spring-framework的github链接:https://github.com/spring-projects/spring-framework 源码环境搭建官方参考1:考如何 ...
- Sping学习笔记(一)----Spring源码阅读环境的搭建
idea搭建spring源码阅读环境 安装gradle Github下载Spring源码 新建学习spring源码的项目 idea搭建spring源码阅读环境 安装gradle 在官网中下载gradl ...
- 搭建 Spring 源码阅读环境
前言 有一个Spring源码阅读环境是学习Spring的基础.笔者借鉴了网上很多搭建环境的方法,也尝试了很多,接下来总结两种个人认为比较简便实用的方法.读者可根据自己的需要自行选择. 方法一:搭建基础 ...
- Spring源码阅读笔记01:源码阅读环境准备
1. 写在前面 对于做Java开发的同学来说,Spring就像是一条绕不过去的路,但是大多数也只是停留在对Spring的简单使用层面上,对于其背后的原理所知不多也不愿深究,关于这个问题,我在平时的生活 ...
- Spring源码阅读-ApplicationContext体系结构分析
目录 继承层次图概览 ConfigurableApplicationContext分析 AbstractApplicationContext GenericApplicationContext Gen ...
随机推荐
- chrome 快速执行 snippets 1. F12 2. Ctrl+Shift+P 3. show snippets 4. 上下选择 5. Ctrl + Enter
chrome 快速执行 snippets F12 Ctrl+Shift+P show snippets 上下选择 Ctrl + Enter
- 测试打包失败 已解决 分析过程 - 关键字 Jenkins nexus package-lock.json npm install build
Jenkins 打包失败 npm run build 总是失败,每次失败报错还不一样. 然后 npm install 每次安装 还总有包超时 MobaXterm1_CHS1 SSH 链接,手工安装也不 ...
- 13 种在 JavaScript 中删除/过滤数组的方法【转】
英文 | https://javascript.plainenglish.io/13-methods-to-remove-filter-an-item-in-an-array-and-array-of ...
- Leetcode 1161 最大层内元素和
一.题目 给你一个二叉树的根节点 root.设根节点位于二叉树的第1层,而根节点的子节点位于第2层,依此类推. 请返回层内元素之和 最大 的那几层(可能只有一层)的层号,并返回其中 最小 的那个. 示 ...
- windows10 使用gcc编译生成可执行文件exe实例解析
一 操作步骤 1.生成可执行程序 cd xxx # 先进入源程序所在的目录 gcc hello.cpp # 一次性编译,windows系统生成a.exe文件,Linux系统生成a.out文件 gcc ...
- python中bytes转int的实例(bytearray to short int in python)
python很多数据都是bytes格式的,经常需要转换成int或者short,笔者实际项目有需求,这里就做个笔记吧. 实例一: bytes转short:(无符号类型) import struct ba ...
- JavaFx 模拟键盘和鼠标事件
模拟键盘事件 可实现按键的模拟,包含快捷键 模拟按下ctrl+v示例代码: val robot = Robot() robot.keyPress(KeyEvent.VK_CONTROL) robot. ...
- 各种O总结及阿里代码规范总结
首先梳理下POJO POJO包括 DO/DTO/BO/VO(所有的POJO类属性必须使用包装数据类型.) 定义 DO/DTO/VO 等 POJO 类时,不要设定任何属性默认值. controller使 ...
- python高级技术(网络编程一)
一 socket是什么 链接socket前要熟悉计算机网络基础请看链接:https://www.cnblogs.com/coderxueshan/p/17344739.html Socket是应用层 ...
- golang 依赖控制反转(IoC) 改进版
最近在开发基于golang下的cqrs框架 https://github.com/berkaroad/squat (陆续开发中,最近断了半年,懒了...).这个框架依赖ioc框架,因为之前写了一个io ...