本文是作者原創,版權歸作者所有.若要轉載,請注明出處
下載spring源碼,本文用的是版本如下:
springframework 5.1.x, IDE工具idea 2019.2.3 JAVA版本 jdk1.8.0_171 構建工具gradle-4.9
1.下載springframework 5.1.x源碼

2.解壓下載的壓縮包,在已有工程中導入該項目

3.選擇該項目路徑

4.選擇gradle導入

5.等待它自己構建

6.編譯完,有個彈框出現,點擊ok

7.如圖,設置gradle配置

8.先編譯spring-core模塊

9.編譯成功

我們可以看到,多了存放字節碼文件的build文件夾

10.編譯spring-oxm模塊,和上面一樣


11.忽略spring-aspects模塊

12.編譯整個spring模塊


13.放開spring-aspects模塊,並編譯


14.新建一個測試模塊,測試spring是否編譯成功




點擊,右上角的ok

加上以下代碼
//spring core compile(project(":spring-context")) //spring 對web的支持 compile(project(":spring-webmvc")) //連接池 compile(project(":spring-jdbc")) //mybatis core compile group: 'org.mybatis', name: 'mybatis', version: '3.5.0' //源碼 spring mybaits的插件包 compile group: 'org.mybatis', name: 'mybatis-spring', version: '2.0.0' //db compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6' //tomcat 容器 compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-core', version: '8.5.5' // jsp compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper', version: '8.5.5' // https://mvnrepository.com/artifact/com.alibaba/fastjson compile group: 'com.alibaba', name: 'fastjson', version: '1.2.50' // https://mvnrepository.com/artifact/org.aspectj/aspectjweaver compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.9.0'
最后build.gradle文件內容如下
plugins { id 'java' } group 'org.springframework' version '5.1.10.BUILD-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' //spring core compile(project(":spring-context")) //spring 對web的支持 compile(project(":spring-webmvc")) //連接池 compile(project(":spring-jdbc")) //mybatis core compile group: 'org.mybatis', name: 'mybatis', version: '3.5.0' //源碼 spring mybaits的插件包 compile group: 'org.mybatis', name: 'mybatis-spring', version: '2.0.0' //db compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6' //tomcat 容器 compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-core', version: '8.5.5' // jsp compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper', version: '8.5.5' // https://mvnrepository.com/artifact/com.alibaba/fastjson compile group: 'com.alibaba', name: 'fastjson', version: '1.2.50' // https://mvnrepository.com/artifact/org.aspectj/aspectjweaver compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.9.0' }
15.編寫demo
package demo01; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloSpring { private String input_str = null; public String getMyStr() { return this.input_str; } public void setMyStr(String strParam) { this.input_str = strParam; } public void Print() { System.out.println("Hello," + this.getMyStr()); } public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloSpring helloSpring = (HelloSpring) context.getBean("myFirstSpringDemo"); helloSpring.Print(); } }
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 https://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byType"> <bean id="myFirstSpringDemo" class="demo01.HelloSpring"> <property name="myStr"> <value>I am Spring</value> </property> </bean> </beans>
16.運行,報錯

可以看到錯誤信息提示我們在spring-context里找不到類,我們重新運行一下這個模塊的所有java下的test文件

其他的問題也是一樣的處理方式,全部處理完
繼續運行demo

好,編譯成功了,可以寫注釋了

至此,我們的spring源碼編譯成功.下面我會繼續更新spring相關的應用和源碼博客,歡迎大家繼續關注,可以的話隨手點個贊吧,謝謝大家
