idea构建spring源码项目

201 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。​ 本文已参与「新人创作礼」活动,一起开启掘金创作之路。

 成功的环境 : jdk1.8.0_271/idea2019.1.3/gradle-4.4.1-bin.zip/spring-framework1-5.0.x

特别感谢借鉴的另两位博主

IntelliJ IDEA导入Spring源码_VFINE的博客-CSDN博客_idea导入spring源码

spring源码构建_l梁晴的博客-CSDN博客_spring源码构建

1.下载源代码

源码下载地址

GitHub - spring-projects/spring-framework: Spring Framework

不建议从这个地址直接下载,太慢,可以fork到国内的码云上再下载非常快,

(源码下载可以在搜索引擎中搜索github,进入后搜索spring-framework即可找到)

也可以从这里下载,这个只需要安装并在idea中配置好gradle,就可以直接导入运行的,本地测试成功:

1294480-spring-framework1-5.0.x(3).zip-互联网文档类资源-CSDN下载

我是直接下载的压缩包

 找到gradle-wrapper.properties配置文件(这里指定了默认的gradle版本,下载这个版本就可以了):

2.下载gradle解压后配置环境变量

下载地址: Gradle | Releases

环境变量配置方法 : Gradle安装及环境变量配置_iBaoxing的博客-CSDN博客_grade环境变量配置

3.修改配置文件

gradle-wrapper.properties修改结果:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=file:///D:\myprogram\tools\gradle-4.4.1-bin.zip

distributionUrl=services.gradle.org/distributio…

改为

distributionUrl=file:///D:\myprogram\tools\gradle-4.4.1-bin.zip
这样就不需要再次下载了

build.gradle修改

		maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
		maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } 
		maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}

新增以下配置(没有以下配置,构建过程超级慢……),定义全局的国内镜像,注意该配置不能在plugins { }之前

allprojects {
	repositories {
		maven { url 'http://maven.aliyun.com/nexus/content/repositories/google' }
		maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
		maven { url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
		maven { url 'http://repo.springsource.org/plugins-release'}
	}
}

4.配置idea

配置gradle

 注意taget目录

这里我踩了个大坑,自己其他项目有target目录需要忽略,这里恰巧需要用到这个目录 

5.导入项目

 跟maven相似,项目加载会下载相关jar包,这个过程比较漫长,可能有半个小时

6.新建测试模块:

选中项目右键-->new -->modoule

 默认就是这样的

 填入项目名

修改新项目中build.gradle,添加依赖

    compile(project(":spring-context"))
    compile(project(":spring-core"))
    compile(project(":spring-beans"))
    compile(project(":spring-aop"))

7.添加测试类

package com.study;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestMy {

	public static void main(String[] args) {
		ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SysConfig.class);
		User user = (User)applicationContext.getBean("user");
		System.out.println(user.toString());
	}
}

package com.study;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class SysConfig {

	@Bean
	public User user(){
		return new User("lq",12);
	}
}

package com.study;

public class User {
	private String name;
	private Integer age;

	public User() {
	}

	public User(String name, Integer age) {
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}


}

8.运行下main方法试试:

core模块报错 

重新编译一下spring-core

编译成功

spring-context没有成功引用spring-Instrument

 修改引用spring-instrument

 9.大功告成,完结

\