一、下载Spring相关Jar包、文档
1. 访问Spring官网
Spring 的官方网站地址是:spring.io ,在官网中进行如下选择操作进入Spring 到项目主页
Project -> Spring Framework
本文学习回顾使用的是 5.0.20 版本(GA表示稳定版本)。
2. 如何下载官方jar包
选择 OVERVIEW 之后,点击如图片所示的右上角的github图标(🐱)
Spring的github的仓库地址:github.com/spring-proj… ,
在这个地址的页面上找到如下内容:
Access to Binaries
For access to artifacts or a distribution zip, see the Spring Framework Artifacts wiki page.
这段内容告诉你Spring的jar如下载,在该页面的底部你能找到Spring的仓库地址。
Downloading a Distribution
If for whatever reason you are not using a build system with dependency management capabilities, you can download Spring Framework distribution zips from the Spring repository at repo.spring.io. These distributions contain all source and binary jar files, as well as Javadoc and reference documentation, but do not contain external dependencies!
点击👆文字中的链接即可进入到仓库中去
复制图片上箭头最后所指向的地址,并在地址的后面拼接上spring就能直达仓库地址:
repo.spring.io/ui/native/r…
点击链接地址下载 spring-framework-5.0.20.RELEASE-dist.zip ,完成下载后本地解压!
docs :包含了官方文档和api文档
libs :包含了Spring的所有jar
二、Spring源码环境搭建
1. 环境配置说明
- Spring-5.0.20 版本
- Gradle 4.4.1
- JDK1.8
- IDEA 2021.3.2
- Mac OS
| Spring版本 | Gradle版本 | JDK版本 |
|---|---|---|
| Spring-5.0.x | Gradle-4.4.1 | JDK8+ |
| Spring-5.1.x | Gradle-4.10.3 | JDK9+ |
| Spring-5.2.x | Gradle-5.6.4 | JDK11+ |
为什么要选择版本?因为日常工作用的是JDK1.8,如果你选择了高版本的 Spring,则 Gradle 也是高版本,那 JDK也最好选择高版本,因为不同版本的 Gradle 编译时需要的JDK版本也不同。不然容易出现下面的错误:
java: 程序包jdk.jfr不存在
2. 下载源码
Spring项目地址: github.com/spring-proj…
点击下载源码包即可,下载到本地后进行解压重名文件目录
国内网络直接在github上下载比较慢,可以到我的 Gitee 项目地址里下载相关源码包。
3. 修改下载源
进入到 spring-framework-project 目录,找到 build.gradle 文件,找到文件中如下所示的内容
repositories {
mavenCentral()
maven { url "https://repo.spring.io/libs-spring-framework-build" }
}
将其替换成阿里云仓库地址,阿里云仓库使用说明
repositories {
//mavenCentral()
//maven { url "https://repo.spring.io/libs-spring-framework-build" }
maven { url 'https://maven.aliyun.com/repository/public/' }
maven{ url 'https://maven.aliyun.com/repository/gradle-plugin'}
maven{ url 'https://maven.aliyun.com/repository/spring-plugin'}
maven{ url 'https://maven.aliyun.com/repository/central'}
maven{ url 'https://maven.aliyun.com/repository/spring'}
maven{ url 'https://maven.aliyun.com/repository/google'}
}
4. Gradle 安装
Spring源码是基于Gradle来构建的,不同版本的Spring可能会使用不同版本的Gradle,所以本地安装也要对应好相应的Gradle的版本号,避免后期编译出现各种莫名奇妙的问题。
- 进入到如下目录
cd ~/spring-framework-project/gradle/wrapper
- 找到 gradle-wrapper.properties 文件并打开
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
该文件的作用是当我们用 IDEA 导入项目之后,项目会自动从我们配置的地址中去下载 Gradle,帮我们把 Gradle 环境搭建好。配置文件中 distributionUrl 就是这个下载的地址。但 IDEA 去下载可能会失败,并且速度不是很快。所以我们把这个地址改成本地的,让他从我们本地下载。
Gradle 的下载地址:gradle.org/releases/
在 Gradle 官网下载很慢,可以去阿里云上下载:mirrors.aliyun.com/macports/di…
下载下来后,我将其放到本地目录中 ~/Software 中
- 替换 distributionUrl 地址 本地的Gradle的文件位置:
/Users/shifeifei/Software/gradle-4.4.1-bin.zip
在Mac OS系统 distributionUrl 的配置如下:
distributionUrl=file://////Users/shifeifei/Software/gradle-4.4.1-bin.zipp
验证地址配置的正确性,可以将
file://////Users/shifeifei/Software/gradle-4.4.1-bin.zip
输入到浏览器中,如果能正常下载文件则说明配置的路径无误
5. 导入Spring源码项目
导入之前确保Git环境已搭建好,然后打开IDEA准备导入Spring源码项目
导入之前可以参考项目目录下的 import-into-idea.md 说明文件
## Steps
_Within your locally cloned spring-framework working directory:_
1. Precompile `spring-oxm` with `./gradlew :spring-oxm:compileTestJava`
2. Import into IntelliJ (File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle)
3. When prompted exclude the `spring-aspects` module (or after the import via File-> Project Structure -> Modules)
4. Code away
这段话的意思是让你先执行如下命令,然后再导入
cd ~/Projects/spring-framework-project
./gradlew :spring-oxm:compileTestJava
看到如下信息就表示执行成功
> Task :spring-oxm:genCastor
[ant:javac] : warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
> Task :spring-oxm:genJaxb
[ant:javac] : warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
Download https://maven.aliyun.com/repository/public/org/apache/bcel/bcel/6.0/bcel-6.0.pom
Download https://maven.aliyun.com/repository/public/org/jibx/jibx-bind/1.3.1/jibx-bind-1.3.1.pom
Download https://maven.aliyun.com/repository/public/org/apache/ant/ant/1.8.4/ant-1.8.4.pom
Download https://maven.aliyun.com/repository/public/org/apache/ant/ant-parent/1.8.4/ant-parent-1.8.4.pom
Download https://maven.aliyun.com/repository/public/org/apache/ant/ant-launcher/1.8.4/ant-launcher-1.8.4.pom
Download https://maven.aliyun.com/repository/public/org/jibx/jibx-bind/1.3.1/jibx-bind-1.3.1.jar
Download https://maven.aliyun.com/repository/public/org/apache/ant/ant/1.8.4/ant-1.8.4.jar
Download https://maven.aliyun.com/repository/public/org/apache/ant/ant-launcher/1.8.4/ant-launcher-1.8.4.jar
Download https://maven.aliyun.com/repository/public/org/apache/bcel/bcel/6.0/bcel-6.0.jar
BUILD SUCCESSFUL in 2m 29s
58 actionable tasks: 58 executed
正式用IDEA导入!!!
(1) 进入项目可以看到如下提示
fatal: not a git repository (or any of the parent directories): .git
这个是因为我们是下载的 zip 包,没有 git 文件,我们执行一下git初始化命令
git init
(2) IDEA中配置项目环境
- JDK 设置
File --> Project Structure
- Gradle 配置
- 编译源码
执行到这里就表示源码编译成功了!!✿✿✿
三、新建Module
- 在IDEA中按如下顺序创建一个 spring-demo-bean 的测试模块
File --> New Module --> Gradle
- 给 spring-demo-bean 模块添加依赖模块
在 spring-demo-bean 目录下的 build.gradle 文件中添加依赖
dependencies {
compile(project(":spring-aop"))
compile(project(":spring-beans"))
compile(project(":spring-context"))
compile(project(":spring-core"))
compile(project(":spring-context-support"))
compile(project(":spring-expression"))
compile(project(":spring-jdbc"))
compile(project(":spring-tx"))
compile("org.aspectj:aspectjweaver:1.9.6")
testCompile 'junit:junit:4.12'
compileOnly 'org.projectlombok:lombok:1.18.8'
}
注意:一定要添加这些依赖,避免后面运行测试代码缺少依赖而报错。
- 如果你要用 web 相关功能,你加上 spring-web 相关依赖即可,根据自己的测试情况而定
- 程序包org.junit不存在
/Users/sff/project/spring-5.0.20/spring-demo-bean/src/main/java/com/sff/demo/TestBean.java:4: 错误: 程序包org.junit不存在 import org.junit.Test;
在 gradle 中有严格的约定:
存放项目源码:src/main/java
存放测试源码:src/test/java
存放资源文件:src/main/resources
所以不要试图在 src/main/java 目录下试图使用 junit 单元测试,我的粗暴方案是直接写main单元测试吧:
public class TestBean {
public static void main(String[] args) {
test();
}
}
(1) 编写测试代码
- 定义实体对象
public class User {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
- resources 目录创建:application-bean.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-3.0.xsd">
<bean id="user" class="com.sff.demo.bean.User">
<property name="username" value="Tom"/>
</bean>
</beans>
- 编写main方法
public class BeanTest {
public static void main(String[] args) {
ApplicationContext cx = new ClassPathXmlApplicationContext("application-bean.xml");
User user = cx.getBean("user", User.class);
System.out.printf(user.getUsername());
}
}
- 运行结果
objc[43921]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/bin/java (0x10e7634c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10f7884e0). One of the two will be used. Which one is undefined.
三月 06, 2022 10:19:04 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5caf905d: startup date [Sun Mar 06 10:19:04 CST 2022]; root of context hierarchy
三月 06, 2022 10:19:04 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [application-bean.xml]
Tom
Process finished with exit code 0
(2) 可能存在的问题
- Kotlin 的版本不对的几个错误如下
第一种情况
Kotlin: Language version 1.1 is no longer supported; please, use version 1.3 or greater.
第二种情况
e: Incompatible classes were found in dependencies. Remove them from the classpath or use '-Xskip-metadata-version-check' to suppress errors
e: /Users/shifeifei/Projects/spring-framework-project/spring-core/src/test/kotlin/org/springframework/core/env/PropertyResolverExtensionsTests.kt: (40, 14): Class 'org.springframework.core.env.PropertyResolverExtensionsKt' is compiled by a pre-release version of Kotlin and cannot be loaded by this version of the compiler
第三中情况
Caused by: org.gradle.api.internal.tasks.compile.CompilationFailedException: Compilation failed; see the compiler error output for details.
- 解决方式 检查你的IDEA中是否按照Kotlin插件:
IntelliJ IDEA --> Preferences --> Plugins --> Kolint
然后找到 build.gradle 文件里面下面内容并修改
id "org.jetbrains.kotlin.jvm" version "1.3.50" apply false
kotlinVersion = "1.3.50"
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs = ["-Xjsr305=strict"]
apiVersion = "1.3"
languageVersion = "1.3"
}
}
PS:这里为啥要用 kotlin 1.3 的版本?因为我们的 gradle 用的是4.4的版本,它对kotlin的版本也是有要求!!!
四、写在最后
授人以鱼,不如授之以渔。编译后的源代码 Gitee