1. 程式人生 > 其它 >& 【02】 在編譯後的Spring原始碼中打斷點及寫註釋

& 【02】 在編譯後的Spring原始碼中打斷點及寫註釋

在學習Spring原始碼時,發現原始碼在IDEA中為只讀狀態(File is read-only),無法編輯及增加註釋,增加了我們學習原始碼的難度。

本文記錄:如何將原始碼的只讀狀態變為編輯狀態(增加註釋來輔助理解原始碼)

1. 搭建簡單的Spring專案

專案整體結構

pom.xmlSpring的版本需與編譯的原始碼版本一致

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.trytry</groupId>
    <artifactId>spring-source-code</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.version>5.2.8.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
        </dependency>
        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
        </dependency>
        <!-- 日誌相關依賴 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.10</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.2</version>
        </dependency>
    </dependencies>
</project>

spring.xml

<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"
    default-lazy-init="false">

    <bean id="student" class="cn.trytry.bean.Student" />

</beans>

cn.trytry.bean.Student

package cn.trytry.bean;

import lombok.Data;

@Data
public class Student {
    private String username = "trytry";
}

cn.trytry.test.TestSpring

package cn.trytry.test;

import cn.trytry.bean.Student;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {

    @Test
    public void test1() {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Student bean = applicationContext.getBean(Student.class);
        System.out.println(bean.getUsername());
    }

}

啟動並檢查TestSpring.test1()是否正常執行:

2. 將編譯後原始碼匯入到搭建的專案中

此處以spring-context包為例

  1. File->Project Structure->Libraries->選中spring-context->選中右側的Classes->點選+號
  1. 選中-> 編譯後原始碼專案\spring-context\build\libs\spring-context-5.2.8.RELEASE.jar
  1. 將之前Classes下的spring-context包刪除
  1. 選中Sources->點選+號 ->選中整個spring-context包 -> OK -> OK
  1. 將之前Sources下的spring-context-sources包刪除
  1. 此時就可以在原始碼中增加註釋了

3. 編輯原始碼後,原始碼與包不一致的情況

增加註釋後會出現,斷點打到註釋上面的情況,是因為原始碼與包不一致,此時應重新打包。

原始碼工程中重新打Jar包(此處為spring-context包):雙擊jar即可重新打包

打包完成: