Spring源碼研究--下載-編譯-導入eclipse-驗證


一,環境配置

操作系統:Unbutu14.04LTS

JDK: 1.8.0_40

git: 1.9.1

gradle: 2.2.1

二,源碼下載-編譯-導入eclipse-驗證

1,下載

使用git直接clone Spring源碼到本地:git clone git://github.com/SpringSource/spring-framework.git

2,編譯

編譯過程,這里所謂的編譯是通過gradle工具編譯Spring-framework的項目文件,主要干的事情就是下載Spring各個模塊依賴的jar包,這個過程一般情況下非常慢,可能是因為資源都在牆外,並且可能出現jar包很久下載不下來的情況,這時候直接ctrl+c退出編譯命令后再次執行命令,對於已經下載下來的以來包不會再次下載,所以編譯的過程相當與可以斷線重連的效果。

①進入源碼目錄執行:gradle eclipse -x :eclipse (我整整花費了1上午才編譯通過)

②,執行gradle install 這個具體神馬作用我還不清楚,但是不執行這個命令導入①編譯后的代碼進eclipse后會有一堆build path error,猜想應該是將步驟1中down下來的依賴存入gradle本地倉庫以便於項目查找使用相關依賴。

③,直接通過eclipse的import到入Spring-framework源碼目錄

 groovy這個項目可能會有方法找不到,主要原因時你的eclipse沒有裝Groovy插件,裝上后clean下項目就OK了

④,驗證

寫一個簡單的測試項目,簡單的IoC調用功能,依賴spring-context項目

a),TestSpring.java
 1 import org.springframework.context.ApplicationContext;
 2 import org.springframework.context.support.ClassPathXmlApplicationContext;
 3 
 4 import com.test.bean.Lover;
 5 
 6 
 7 public class TestSpring {
 8     /**
 9      * @param args
10      */
11     @SuppressWarnings("resource")
12     public static void main(String[] args) {
13         ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
14         Lover p = ctx.getBean("lover",Lover.class);
15         p.info();
16     }
17 
18 }

b),Lover.java

 1 package com.test.bean;
 2 
 3 public class Lover {
 4     
 5     private String name;
 6     private int age;
 7     
 8     public String getName() {
 9         return name;
10     }
11     public void setName(String name) {
12         this.name = name;
13     }
14     public int getAge() {
15         return age;
16     }
17     public void setAge(int age) {
18         this.age = age;
19     }
20     public void info(){
21         System.out.println("Spring is a best lover fro you~~~");
22     }
23 }

c),bean.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3  xmlns="http://www.springframework.org/schema/beans"
 4  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5  xmlns:context="http://www.springframework.org/schema/context"
 6  xsi:schemaLocation="http://www.springframework.org/schema/beans 
 7                     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 8                     http://www.springframework.org/schema/context
 9                     http://www.springframework.org/schema/context/spring-context-2.5.xsd
10                     ">
11                     
12 <bean id="lover" class="com.test.bean.Lover" scope="prototype"></bean>
13 
14 </beans>

三,總結

  本文主要小結了Spring源碼的下載(github),編譯(gradle),導入eclipse的過程,最后簡單測試了導入eclipse后的SpringIoC模塊的功能。從此開啟了Spring源碼深入研究之路。

 

本文版權所有,轉載請注明出處


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM