搭建环境
本次使用的源码Spring4.3.x,JDK8,gradle4.10.2,idea2018.2.5
Spring源码使用的是Gradle
进行构建的,所以需要下载 Gradle
以及搭建 java开发环境
Java环境使用的是 JDK1.8
Gradle版本4.10.2
Gradle下载
spring-framework-4.3.x.zip 包:查看build.gradle文件 查询gradlew 有推荐匹配的gradle版本号
Spring源码下载地址:Spring源码下载
开始编译
- 进入Spring源码目录
cd spring-framework-4.3.x
- 执行脚本:gradlew.bat cleanIdea :spring-oxm:compileTestJava
导入idea
File-->new-->Project from Existing Sources... 选中解压的目录(spring-framework-4.3.x)
如果build报错,一般都是引用的jar无法下载到
修改配置build.gradle 添加maven仓库
注意:有2个地方需要添加,我这边应该是加多了,但是以防万一都加上了
可以参考:阿里仓库服务
buildscript {
repositories {
maven { url "https://maven.aliyun.com/repository/public" }
gradlePluginPortal()
maven { url "https://repo.spring.io/plugins-release" }
}
repositories {
maven{ url 'https://maven.aliyun.com/repository/public'}
maven{ url 'https://maven.aliyun.com/repository/apache-snapshots'}
maven{ url 'https://maven.aliyun.com/repository/central'}
maven{ url 'https://maven.aliyun.com/repository/spring-plugin'}
maven{ url 'https://maven.aliyun.com/nexus/content/groups/public'}
maven{ url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
mavenCentral()
maven { url "https://repo.spring.io/libs-spring-framework-build" }
}
然后基本上加载就没有问题了
代码build
build会报各种问题,首先build去除test模块的代码,用 build -x test 执行
创建module:test-spring
package com.ywm;
/**
* @Description
* @Author YuanWeiMin
* @Date 2022-05-24 17:57
*/
public class TestBean {
public TestBean() {
System.out.println("init.......");
}
public void test() {
System.out.println("test.......");
}
}
package com.ywm;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Description
* @Author YuanWeiMin
* @Date 2022-05-24 17:56
*/
public class TestSpring {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext("beans.xml");
System.out.println(ctx.getId());
TestBean tb = ctx.getBean(TestBean.class);
tb.test();
}
}
<?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="com.ywm.TestBean"/>
</beans>
启动报错:ClassPathXmlApplicationContext 无法加载
解决办法:子模块的build.gradle配置添加
如果spring-context:main里面无法加载aop文件
解决办法:模块的build.gradle配置添加
遇到的问题到此结束
启动test
遇到特殊情况,这个里面的main方法的jdk默认选中6,需要特殊注意一下,应该是我电脑同时安装了jdk6和jdk8
记录:
零零总总搞了好几天,主要是各种编译问题