环境说明:
系统: 64位 windows7专业版
idea 版本: ultinate 2018.3
jdk 版本: jdk8_202
一. 资源下载
1. spring 源码下载链接 : https://github.com/spring-projects/spring-framework
下载完成, 解压到工程源码存放目录
2. 因 spring 源码采用 gradle 编译, 故需要下载gradle.链接: https://gradle.org/releases/
下载完成解压到软件安装目录 (勿用中文路径), 复制路径, 然后设置环境变量:
GRADLE_HOME: D:\software_ww\gradle-5.6.3
Path (win10为PATH) 后添加: %GRADLE_HOME%\bin
然后检查 gradle 是否配置成功: cmd 窗口输入: gradle -version 出现如下内容即成功:
3. 升级 Idea 插件 Kotlin:
若 Kotlin 版本低于 v1.3.50 则可能存在 spring 源码和 Kotlin 插件冲突的情况, 因国内特殊环境, 需要手动升级:
a. 查看当前 Kotlin 插件版本: settings -> Plugins -> Installed -> 搜索kotlin ->点击查看详情:
b. 下载 kotlin 新版插件链接: https://plugins.jetbrains.com/plugin/6954-kotlin/versions
找到对应 IJ2018.3-1 版本的 Kotlin,点击下载
c. 手动导入插件:
settings -> Plugins -> 右上角不起眼的小齿轮: install plugin from disk...
重启 idea 即可
二. 资源预备
1. 在 spring 源码目录中右键 gradlew.bat ,管理员运行~ 经过漫长的等待显示 Successful
2. 在 spring 源码目录中编辑 build.gradle ,添加国内 maven 镜像:
buildscript {
repositories {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
}
dependencies {
classpath 'org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.16'
classpath 'io.spring.asciidoctor:spring-asciidoctor-extensions:0.1.3.RELEASE'
}
}
3. spring 源码目录内 地址栏输入 cmd , 执行: gradle.bat :spring-oxm:compileTestJava, 漫长等待后 Successful~
三. 源码导入
1. Idea Import Project
2. 精确选择到: 源码中的build.gradle文件
3. 点击OK转到界面,配置如图:
4. 再次经过漫长的等待后, 显示导入成功~
四. 简单调试
1. 在父模块上右键, 新增maven模块, 编写简单测试工程spring_demo,maven依赖加入 junit 4.12和commons-logging 1.2
2. 重要:将源码添加为 Idea 依赖:
file -> project structure...
添加如下依赖:
3. 编写service层接口和实现类代码:
接口:
package online.hcyg.service;
public interface IDemoService {
void run();
}
实现:
package online.hcyg.service.impl;
import online.hcyg.service.IDemoService;
public class DemoServiceImpl implements IDemoService {
@Override
public void run() {
System.out.println("run~");
}
}
xml配置文件:
<?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-3.2.xsd
">
<bean id="demoService" class="online.hcyg.service.impl.DemoServiceImpl"></bean>
</beans>
测试类:
package com.huawei.test;
import online.hcyg.service.IDemoService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DemoTest {
@Test
public void test(){
ApplicationContext context= new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
IDemoService demoService = context.getBean("demoService", IDemoService.class);
demoService.run();
}
}
运行即可debug到框架内部~
五. Bug集合
1. Error:Kotlin: [Internal Error] java.lang.LinkageError: loader constraint violation: loader (instance of org/jetbrains/kotlin/cli/jvm/plugins/PluginURLClassLoader$SelfThenParentURLClassLoader) previously initiated loading for a different type with name "kotlin/sequences/Sequence"
解决:
遇到该Bug其实是未按博主步骤, 升级 Kotlin插件版本所致, 解决办法如下:
a. github上解决思路是升级Idea版本, 且该方案得以让该 Issue 关闭, 如下:
b. 升级 Idea 的 Kotlin插件版本, 该答案见github另一个Issue: (可见本博文: 一. 3)
----------------------------------------------------------------------------完--------------------------------------------------------------------------------------------
作者:划船一哥
来源:CSDN
https://me.csdn.net/weixin_42711325
版权声明:本文为博主原创文章,转载请附上博文链接!