Spring 源码阅读(一)环境搭建

注意事项:

  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地址: github.com/spring-proj...

配置 Gradle 环境

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

properties 复制代码
distributionUrl=https\://mirrors.cloud.tencent.com/gradle/gradle-7.5.1-bin.zip

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

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 命令进行编译。

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

css 复制代码
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

properties 复制代码
-Dfile.encoding=UTF-8

新建测试 Module

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

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

php 复制代码
include 'spring-research'

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

java 复制代码
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 中添加过滤规则:

xml 复制代码
<module name="BeforeExecutionExclusionFileFilter">  
    <property name="fileNamePattern" value="^.*\\spring\-research\\.*$"/>  
</module>

新建实体类 Person

java 复制代码
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 复制代码
<?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 的实例

java 复制代码
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);
	}
}

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

相关推荐
真实的菜2 分钟前
消息队列处理模式:流式与批处理的艺术
java
噼里啪啦啦.17 分钟前
Spring事务和事务传播机制
数据库·sql·spring
盖世英雄酱5813618 分钟前
Java 内存管理技巧(新手必看集合篇)
java
码农小灰20 分钟前
Java 8 Stream API 入门到实践详解
java·java案例
步、步、为营25 分钟前
.NET 事件模式举例介绍
java·开发语言·.net
cui_hao_nan28 分钟前
设计模式——模板方法
java·设计模式
小吕学编程28 分钟前
HttpServletRequest常用方法
java·http
在未来等你29 分钟前
Java并发编程实战 Day 11:并发设计模式
java·设计模式·多线程·并发编程·threadlocal·生产者消费者·读写锁
李少兄41 分钟前
解决 idea提示`SQL dialect is not configured` 问题
java·sql·intellij-idea
BreezeDove1 小时前
IDEA安装&迁移IDEA配置数据位置
java·ide·intellij-idea