Spring源码解析

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

Idea导入Spring源码

下载

下载gradle

因为Spring源码里没有使用Maven依赖而是使用gradle依赖所以我们需要在本地下载安装并配置gradle环境。注意这里下载安装的gradle版本应与Spring源码中的gradle版本对应。这里推荐下载我的

链接: https://pan.baidu.com/s/1YVww-x7Furqq3s0KcN27CQ 提取码: 6ai4

下载Spring源码

Spring源码中gradle版本应与自己本地下载的gradle保持一致这里推荐下载我的

链接: https://pan.baidu.com/s/186W8TluEc-uOVcKe8Fadbg 提取码: vw8m

安装

解压gradle

  • 1直接解压gradle到一个目录即完成安装。
  • 2需要配置环境变量GRADLE_HOME与Path。
  • 3验证gradle是否安装成功gradle -v

这里要注意本地需要安装JDK8并配置环境变量

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

解压Spring源码

  • 1将下载下来的Spring源码压缩包解压到本地某个目录下

  • 2切换到自己解压的Spring源码目录下(这里是我的C:\Users\Administrator\Desktop\myCode\spring源码学习\spring-framework-5.2.6.RELEASE)在目录上单击输入cmd进入终端执行gradlew :spring-oxm:compileTestJava 命令

  • 3执行gradlew :spring-oxm:compileTestJava可能报错这里下图有解决方案
    -在这里插入图片描述
    在这里插入图片描述
    解决方案在Spring源码中找到build.gradle文件
    在这里插入图片描述在这里插入图片描述

    ## 注释代码
    id 'io.spring.gradle-enterprise-conventions' version '0.0.2'
    ## 修改代码版本号
    mavenBom "com.fasterxml.jackson:jackson-bom:2.10.5"
    mavenBom "io.netty:netty-bom:4.1.39.Final"
    
  • 4在Spring源码中找到settings.gradle 文件添加阿里镜像下载依赖会快很多
    在这里插入图片描述

    ## 添加代码
    maven{ url 'https://maven.aliyun.com/nexus/content/groups/public/'}
    
  • 5在Spring源码中找到build.gradle文件添加如下代码保证gradle能正常在idea中被构建
    在这里插入图片描述

    ## 如果idea导入项目依赖有问题 
    ## 报POM relocation to an other version number is not fully supported in Gradle : xml-apis#xml-apis...问题则添加如下代码即可解决
    configurations.all {
    	resolutionStrategy {
    		force 'xml-apis:xml-apis:1.0.b2'
    	}
    }
    

导入Spring源码

从本地导入项目到idea中

在这里插入图片描述

在这里插入图片描述

以工程的形式导入

在这里插入图片描述

然后等待项目加载依赖即可

修改idea配置

修改gradle

在这里插入图片描述

修改编码格式

在这里插入图片描述

重新加载依赖

在这里插入图片描述

创建测试项目

前置工作

验证Spring源码环境是否正常

在源码项目中新建一个spring-z-ioc模块以module的形式创建, 见下图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

创建spring-z-ioc成功后等idea加载完成

在这里插入图片描述

创建spring-z-ioc成功后等idea加载完成

Spring源码中的settings.gradle文件多了一个子引用
在这里插入图片描述

在spring-z-ioc的build.gradle文件中添加以下代码重点
在这里插入图片描述

## 添加代码
compile (project(":spring-aop"))
//compile (project(":spring-aspects")) // 这个不要打开 否则会报错
compile (project(":spring-beans"))
compile (project(":spring-context"))
compile (project(":spring-context-indexer"))
compile (project(":spring-context-support"))
compile (project(":spring-core"))
compile (project(":spring-expression"))
compile (project(":spring-instrument"))
compile (project(":spring-jcl"))
compile (project(":spring-jdbc"))
compile (project(":spring-jms"))
compile (project(":spring-messaging"))
compile (project(":spring-orm"))
compile (project(":spring-oxm"))
compile (project(":spring-test"))
compile (project(":spring-tx"))
compile (project(":spring-web"))
compile (project(":spring-webmvc"))
compile (project(":spring-webflux"))
compile (project(":spring-websocket"))

注Spring-aspect工程里面的类不要去打开打开后某些类可能会报错重启一下idea即可恢复正常

代码测试

package com.kai;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component
class TestService {
	public void testMethod() {
		System.out.println("Spring源码导入测试成功");
	}
}

@Configuration
@ComponentScan("com.kai")
public class Test {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Test.class);
		TestService testService = context.getBean("testService", TestService.class);
		testService.testMethod();
	}
}

//public class Test {
//	public static void main(String[] args) {
//		System.out.println("111111111");
//	}
//}

在这里插入图片描述

解决乱码问题

  • 点击 IDEA 顶部菜单栏中的 Help
  • 点击 Edit Custom VM Options
  • 追加 -Dfile.encoding=UTF-8 到文档末尾
  • 重启 IDEA 即可

完结撒花愿天下的每一位程序员少走弯路向着朝阳前行

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Spring