spring源码编译

351 阅读1分钟

spring版本:v5.2.6.RELEASE

1. 克隆spring源码

git clone https://github.com/spring-projects/spring-framework.git

2. 切换版本至v5.2.6.RELEASE

进入spring-framework下执行
git checkout -b v5.2.6.RELEASE v5.2.6.RELEASE

3. 配置gradle全局下载仓库,为了国内加速下载依赖

对所有项目生效,在~/.gradle/下创建init.gradle文件,填入以下内容

allprojects {
    repositories {
        def ALIYUN_REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public'
        def ALIYUN_JCENTER_URL = 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
        all {
            ArtifactRepository repo ->
                if (repo instanceof MavenArtifactRepository) {
                    def url = repo.url.toString()
                    if (url.startsWith('https://repo1.maven.org/maven2')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
                        remove repo
                    }
                    if (url.startsWith('https://jcenter.bintray.com/')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
                        remove repo
                    }
                }
        }
        maven {
            url ALIYUN_REPOSITORY_URL
            url ALIYUN_JCENTER_URL
        }
    }
}

4. 执行./gradlew :spring-oxm:compileTestJava,效果如下

效果图

若gradle无法下载可手动下载,放置gradle/wrapper下,然后修改distributionUrl。如图

5. Idea导入

Import Project -> 勾选Gradle -> Finshed

6. 构建自己的Demo工程

6.1 新建工程

6.2 引入依赖

在build.gradle中加入

dependencies {
    compile(project(":spring-context"))
    testCompile group: 'junit', name: 'junit', version: '4.12'
}
6.3 编写代码

UserService.class

### interface
public interface UserService {
	void sayHello();
}

UserServiceImpl.class

### impl
public class UserServiceImpl implements UserService {
	@Override
	public void sayHello() {
		System.out.println("Hello spring framework.");
	}
}

SpringApplication.class

public class SpringApplication {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
		applicationContext.register(UserServiceImpl.class);
		applicationContext.refresh();
		UserService userService = applicationContext.getBean(UserService.class);
		userService.sayHello();
	}
}
6.4 最终效果