Spring源码研究--下载-编译-导入eclipse-验证

时间:2023-03-09 18:03:37
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上午才编译通过)
Spring源码研究--下载-编译-导入eclipse-验证
②,执行gradle install 这个具体神马作用我还不清楚,但是不执行这个命令导入①编译后的代码进eclipse后会有一堆build path error,猜想应该是将步骤1中down下来的依赖存入gradle本地仓库以便于项目查找使用相关依赖。

Spring源码研究--下载-编译-导入eclipse-验证

③,直接通过eclipse的import到入Spring-framework源码目录

groovy这个项目可能会有方法找不到,主要原因时你的eclipse没有装Groovy插件,装上后clean下项目就OK了Spring源码研究--下载-编译-导入eclipse-验证

④,验证

写一个简单的测试项目,简单的IoC调用功能,依赖spring-context项目

Spring源码研究--下载-编译-导入eclipse-验证

a),TestSpring.java
 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.test.bean.Lover; public class TestSpring {
/**
* @param args
*/
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
Lover p = ctx.getBean("lover",Lover.class);
p.info();
} }

b),Lover.java

 package com.test.bean;

 public class Lover {

     private String name;
private int age; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void info(){
System.out.println("Spring is a best lover fro you~~~");
}
}

c),bean.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
"> <bean id="lover" class="com.test.bean.Lover" scope="prototype"></bean> </beans>

三,总结

  本文主要小结了Spring源码的下载(github),编译(gradle),导入eclipse的过程,最后简单测试了导入eclipse后的SpringIoC模块的功能。从此开启了Spring源码深入研究之路。

本文版权所有,转载请注明出处