一、下载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/spring/' }
}
复制代码
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/
下载下来后,我将其放到本地目录中 ~/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"))
testImplementation 'junit:junit:4.12'
}
复制代码
注意:一定要添加这些依赖,避免后面运行测试代码缺少依赖而报错。
- 如果你要用 web 相关功能,你加上 spring-web 相关依赖即可,根据自己的测试情况而定
(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.
复制代码
检查你的IDEA中是否按照Kotlin插件:
IntelliJ IDEA --> Preferences --> Plugins --> Kolint
复制代码
然后找到 build.gradle 文件里面如下内容:
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs = ["-Xjsr305=strict"]
apiVersion = "1.1"
languageVersion = "1.1"
}
}
复制代码
把里面的Kotlin版本改了
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs = ["-Xjsr305=strict"]
apiVersion = "1.3"
languageVersion = "1.3"
}
}
复制代码
四、写在最后
授人以鱼,不如授之以渔。调试源代码 Gitee