亲手给你搭建spring源码环境

一、源码下载

GitHub - spring-projects/spring-framework: Spring Framework

二、构建工具

1、下载

Gradle Distributions

注意:版本还是很重要,特别是跟你的jdk版本要对应,我试过很多版本,我选择的是jdk11+gradle5.6.4

2、环境变量配置

 3、检查是否安装成功

4、idea插件安装

三、设置镜像

repositories {
			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" }
		}

四、修改配置

1、idea中——>setting——>Gradle

2、File——>Project Setting——>project

3、File——>Project Setting——>SDKS

五、源码构建

1、用idea打开已经来取的源码,gradle就会自己进行构建,构建成功后如下图所示:

2、网络是个很大的问题,网络不好,很多依赖下载不下来,因为 Gradle 支持使用 Maven 依赖,所以我们可以使用阿里云的 Maven 镜像 https://maven.aliyun.com/nexus/content/groups/public/**。修改 build.gradle 文件

		repositories {
			mavenCentral()
			maven { url "http://maven.aliyun.com/nexus/content/groups/public/"}
			maven { url "https://repo.spring.io/libs-spring-framework-build" }
			maven { url "https://repo.spring.io/snapshot" } // Reactor
		}

3、报错:每个人可能都不一样,环境不同,网速不同等很多其它因素,有问题正常,我只列举自己遇到的些问题

  • 下载plugin没有权限, 打开build.gradle--->plugins节点:
plugins {
	id 'io.spring.dependency-management' version '1.0.9.RELEASE' apply false
//	id 'io.spring.gradle-enterprise-conventions' version '0.0.2'
	id 'io.spring.nohttp' version '0.0.5.RELEASE'
	id 'org.jetbrains.kotlin.jvm' version '1.3.72' apply false
	id 'org.jetbrains.dokka' version '0.10.1' apply false
	id 'org.asciidoctor.jvm.convert' version '2.4.0'
	id 'de.undercouch.download' version '4.1.1'
	id "io.freefair.aspectj" version '4.1.6' apply false
	id 'com.gradle.build-scan' version '3.2'
	id "com.jfrog.artifactory" version '4.12.0' apply false
	id "com.github.ben-manes.versions" version '0.24.0'
}

  • Gradle build的过程中checkstyle校验不过,出现这个问题我们可以把去掉checkstyle,打开build.gradle--->dependencies
	dependencies {
		testCompile("org.junit.jupiter:junit-jupiter-api")
		testCompile("org.junit.jupiter:junit-jupiter-params")
		testCompile("org.mockito:mockito-core")
		testCompile("org.mockito:mockito-junit-jupiter")
		testCompile("io.mockk:mockk")
		testCompile("org.assertj:assertj-core")
		// Pull in the latest JUnit 5 Launcher API to ensure proper support in IDEs.
		testRuntime("org.junit.platform:junit-platform-launcher")
		testRuntime("org.junit.jupiter:junit-jupiter-engine")
		testRuntime("org.apache.logging.log4j:log4j-core")
		testRuntime("org.apache.logging.log4j:log4j-slf4j-impl")
		testRuntime("org.apache.logging.log4j:log4j-jul")
		// JSR-305 only used for non-required meta-annotations
		compileOnly("com.google.code.findbugs:jsr305")
		testCompileOnly("com.google.code.findbugs:jsr305")
//		checkstyle("io.spring.javaformat:spring-javaformat-checkstyle:0.0.15")
	}
  • Error:(43, 16) java: 找不到符号符号: 类 AnnotationCacheAspect位置: 类org.springframework.cache.aspectj.AspectJCachingConfiguration,原因是因为AnnotationCacheAspect文件不是class而是aspect所以jdk识别不了无法编译。我们需要安装aspectj.jar使用Ajc进行编译。

下载地址:AspectJ Downloads | The Eclipse Foundation

下载好之后,在此文件的同级目录执行java -jar aspectj-1.9.4.jar命令打开安装界面,一路next安装好就行

对AspectJ项目添加Facets,File -> Project Structure:

六、测试

1、创建一个自己学习的model

2、依赖:本模块中buil.gradle

plugins {
    id 'java'
}

group 'org.springframework'
version '5.2.9.BUILD-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile project(":spring-context")

}

test {
    useJUnitPlatform()
}

3、代码

package com.jason.test;
/*
 *@ClassName MyTestBean
 *@Author 王华春
 *@Version 1.0
 */
public class MyTestBean {
    public void testBean(){
        System.out.println("myTestBean");
    }
}

class MyTestBeanTest {
    @Test
    void tsetSimpleLoad(){
        BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("bean.xml"));
        MyTestBean bean = (MyTestBean) beanFactory.getBean("myTestBean");
        bean.testBean();
    }
}

<?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="myTestBean" class="com.jason.test.MyTestBean"/>
</beans>

4、测试结果

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术蜗牛-阿春

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值