Spring 源码编译
心血来潮突然想看下spring的源码,我用的是idea2021,spring版本是5.2.x,但是网上基本上都是以前的版本,所以想记录一下。
1.下载spring源码
在码云上通过git克隆源码https://gitee.com/mirrors/Spring-Framework.git
2.下载gradle
查看Spring-Framework/gradle/wrapper/gradle-wrapper.properties文件对应的gradle的版本号。
并自行去gradle官网下载,找到对应的版本号。
3.配置gradle环境变量
vim ~/.bash_profile
我是mac,配置环境变量类似jdk,不会可百度。
重新加载配置
source ~/.bash_profile
验证安装
gradle -v
3.编译源码
./gradlew :spring-oxm:compileTestJava
4.导入源码
5.新建测试module
引入依赖
我是将spring-test的依赖直接拿过来的
description = "Spring DebugContext Framework"
apply plugin: "kotlin"
dependencies {
compile(project(":spring-core"))
optional(project(":spring-aop"))
optional(project(":spring-beans"))
optional(project(":spring-context"))
optional(project(":spring-jdbc"))
optional(project(":spring-orm"))
optional(project(":spring-tx"))
optional(project(":spring-web"))
optional(project(":spring-webflux"))
optional(project(":spring-webmvc"))
optional(project(":spring-websocket"))
optional(project(":spring-instrument"))
optional("javax.activation:javax.activation-api")
optional("javax.el:javax.el-api")
optional("javax.inject:javax.inject")
optional("javax.servlet:javax.servlet-api")
optional("javax.servlet.jsp:javax.servlet.jsp-api")
optional("javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api")
optional("javax.xml.bind:jaxb-api")
optional("javax.websocket:javax.websocket-api")
optional("junit:junit")
optional("org.junit.jupiter:junit-jupiter-api")
optional("org.testng:testng")
optional("org.aspectj:aspectjweaver")
optional("org.codehaus.groovy:groovy")
optional("org.hamcrest:hamcrest")
optional("org.apache.taglibs:taglibs-standard-jstlel")
optional("net.sourceforge.htmlunit:htmlunit")
optional("org.seleniumhq.selenium:htmlunit-driver")
optional("org.seleniumhq.selenium:selenium-java")
optional("org.xmlunit:xmlunit-matchers")
optional("org.skyscreamer:jsonassert")
optional("com.jayway.jsonpath:json-path")
optional("org.jetbrains.kotlin:kotlin-reflect")
optional("org.jetbrains.kotlin:kotlin-stdlib")
optional("io.projectreactor:reactor-test")
optional("org.jetbrains.kotlinx:kotlinx-coroutines-core")
optional("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
testCompile(project(":spring-context-support"))
testCompile(project(":spring-oxm"))
testCompile(testFixtures(project(":spring-beans")))
testCompile(testFixtures(project(":spring-context")))
testCompile(testFixtures(project(":spring-core")))
testCompile(testFixtures(project(":spring-tx")))
testCompile(testFixtures(project(":spring-web")))
testCompile("javax.annotation:javax.annotation-api")
testCompile("javax.cache:cache-api")
testCompile("javax.ejb:javax.ejb-api")
testCompile("javax.interceptor:javax.interceptor-api")
testCompile("javax.mail:javax.mail-api")
testCompile("org.hibernate:hibernate-core")
testCompile("org.hibernate:hibernate-validator")
testCompile("javax.validation:validation-api")
testCompile("org.junit.platform:junit-platform-runner") {
exclude group: "junit", module: "junit"
}
testCompile("org.junit.platform:junit-platform-testkit")
testCompile("com.fasterxml.jackson.core:jackson-databind")
testCompile("com.thoughtworks.xstream:xstream")
testCompile("com.rometools:rome")
testCompile("org.apache.tiles:tiles-api")
testCompile("org.apache.tiles:tiles-core")
testCompile("org.apache.tiles:tiles-servlet")
testCompile("org.hsqldb:hsqldb")
testCompile("org.apache.httpcomponents:httpclient")
testCompile("io.projectreactor.netty:reactor-netty")
testCompile("de.bechte.junit:junit-hierarchicalcontextrunner")
testRuntime("org.junit.vintage:junit-vintage-engine") {
exclude group: "junit", module: "junit"
}
testRuntime("org.glassfish:javax.el")
testRuntime("com.sun.xml.bind:jaxb-core")
testRuntime("com.sun.xml.bind:jaxb-impl")
}
添加测试类
package org.springframework.bean;
/**
* @author fangjianfeng
* @date 2021-07-20.
*/
public class MyBean {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
<?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="myBean" class="org.springframework.bean.MyBean"></bean>
</beans>
运行成功
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author fangjianfeng {@literal <jianfeng.fang@leyantech.com>}
* @date 2021-07-20.
*/
public class TestBean {
@Test
void testSimpleLoad() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
Object myBean = applicationContext.getBean("myBean");
System.out.println(myBean);
}
}
6.注意事项
建完测试类运行的时候每次启动都会编译一次,试了好多方法都不管用,最后尝试下面方式得以解决。
勾选完idea编译后再次运行demo时可能会报各种类找不到的错误,我们只要找到报错的项目进行编译就好了。
我就是按这样的方法解决的~