IDEA 搭建Spring源码环境
Posted 破阵子2021
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IDEA 搭建Spring源码环境相关的知识,希望对你有一定的参考价值。
参考链接:Idea 搭建Spring源码环境
clone项目
- 项目地址:Spring-Framework;
- 切换分支:
git checkout -b 5.2.x origin/5.2.x
- 源码目录下有个import-into-idea.md,介绍关于导入idea的步骤以及注意事项;
IDEA导入
编译spring-oxm模块
源码目录下执行./gradlew :spring-oxm:compileTestJava
将源码导入IDEA
File -> New -> Project from Existing Sources -> build.gradle,项目将自动下载依赖包并编译:
导入后验证
注:引入自定义模块放入SpringFramework项目下
右击项目 -> New -> Module...,选择gradle项目,新建项目spring-demo,在dependencies中增加如下依赖
dependencies {
testImplementation \'org.junit.jupiter:junit-jupiter-api:5.6.0\'
testRuntimeOnly \'org.junit.jupiter:junit-jupiter-engine\'
compile(project(":spring-beans"))
compile(project(":spring-context"))
compile(project(":spring-core"))
compile(project(":spring-expression"))
}
然后检查settings.gradle是否有include \'spring-demo\',有的话说明新加模块被添加进来了。
最后编写测试代码验证:
// Persion.java
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 +
\'}\';
}
}
//Main.java
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Person person = context.getBean(Person.class);
System.out.println(person);
}
}
resource目录下添加配置文件spring.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 class="org.springframework.demo.Person" id="person">
<property name="name" value="johnny"/>
<property name="age" value="10"/>
</bean>
</beans>
输出如下内容表示环境搭建成功:
后面就可以在此基础上debug调试代码了:)
FAQ
-
Could not download javax.json-api.jar (javax.json:javax.json-api:1.1.4)
Could not download javax.json-api.jar (javax.json:javax.json-api:1.1.4)
Could not get resource \'https://repo.maven.apache.org/maven2/javax/json/javax.json-api/1.1.4/javax.json-api-1.1.4.jar\'.
Could not GET \'https://repo.maven.apache.org/maven2/javax/json/javax.json-api/1.1.4/javax.json-api-1.1.4.jar\'.
Connection reset
Connection reset原因可能是网络不好,重新构建(点击刷新按钮)就好了。
以上是关于IDEA 搭建Spring源码环境的主要内容,如果未能解决你的问题,请参考以下文章
Spring源码阅读:使用IDEA搭建Spring5.0.x源码阅读环境
SPRING01_基于gradle6.8.2和JDK15搭建Spring源码坏境