前言
本来想直接动手写一个spring源码系列,以前编译过5.0版本,打算重新编译一个最新版。编译也是spring学习的一个重要部分。
版本
操作系统:mac os
JDK:17(spring官方指定用java17)
Spring:5.3.22
gradle:6.0.0(spring自带)
IDEA:2021.2.3
步骤
1)终端克隆源码
git clone git@github.com:spring-projects/spring-framework.git
2)根据官方说明,预编译spring-oxm模块。终端进入spring-framework目录,执行
./gradlew :spring-oxm:compileTestJava
3)添加阿里镜像地址
2)现在可以用IDEA打开spring-framework
3)根据官方说明,排除 spring-aspects模块
4)修改java版本,有多处涉及到JDK版本的都改成java17
5)gradle设置,spring默认用自带的gradle编译,我这就不做改变。但是IDEA中要改个地方,因为后面要跑JUnit。
6)刷新gradle开始构建
7)初步构建成功
8)添加ioc测试模块,这里我用读取配置文件方式。
package com.cracker.bean;
public class Person {
private String lastName;
private Integer age;
private String gender;
public Person() {
}
public Person(String lastName, Integer age, String gender) {
this.lastName = lastName;
this.age = age;
this.gender = gender;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + ''' +
", age=" + age +
", gender='" + gender + ''' +
'}';
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person01" class="com.cracker.bean.Person">
<property name="lastName" value="张三"></property>
<property name="age" value="18"></property>
<property name="gender" value="男"></property>
</bean>
</beans>
package com.cracker;
import com.cracker.app.AppConfig;
import com.cracker.bean.IndexDao;
import com.cracker.bean.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.format.Printer;
public class IOCTest {
@Test
public void test() {
ApplicationContext ioc = new ClassPathXmlApplicationContext("ioc.xml");
System.out.println("容器启动完成....");
Person bean = (Person) ioc.getBean("person01");
System.out.print(bean);
}
}
下面这个是build.gradle
plugins {
id 'java'
}
group 'org.springframework'
version '1.0.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
```
compileOnly group: 'junit', name: 'junit', version: '4.12'
compileOnly(project(":spring-context"))
compileOnly(project(":spring-instrument"))
}
9)启动junit,开始编译。编译过程不出意外会报错。
第一个错误:Kotlin: warnings found and -Werror specified
解决: 到project structure里面,删掉后面的-Weeror
第二个错误: 找不到Instrumentation
解决: 在自己模块里面添加spring-instrument,这个上面我已经添加了。
第三个错误: 找不到xx类,根据官方文档说明需要预编译一些模块
解决: 命令行进入主目录,执行
./gradlew javapoetRepackJar
./gradlew objenesisRepackJar
10)再执行JUnit,编译成功
总结与参考
编译过程总会遇到各式各样的问题,要善于借助搜索引擎。
Kotlin: warnings found and -Werror specified blog.csdn.net/qq_39410381…
如果以前没有安装过aspectj插件的参考这篇文章 blog.csdn.net/u013837825/…