Idea導入Spring源碼 基本調試 全文指南

環境說明:

系統: 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
版權聲明:本文爲博主原創文章,轉載請附上博文鏈接!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章