Spring源码编译

版本 :IDEA 2018.1 ; Spring 5.1.10.RELEASE; gradle-4.10.3 ;

1:下载源码

https://github.com/spring-projects/spring-framework/tree/v5.1.10.RELEASE

2:修改build.gradle文件

解压spring-framework-5.1.10.RELEASE.zip 进入到文件夹,打开build.gradle文件,添加阿里云镜像,提高编译速度。

repositories {
   mavenCentral()
   maven { url "https://repo.spring.io/libs-spring-framework-build" }
   maven { url "https://repo.spring.io/snapshot" } 
   maven { url "http://maven.aliyun.com/nexus/content/groups/public/"}
   maven {url "https://plugins.gradle.org/m2/"}
}
  

3:编译

gradlew :spring-oxm:compileTestJava

4:idea配置gradle-4.10.3

(1):下载: https://services.gradle.org/distributions/

(2) idea配置gradle

将编译后的Spring源码导入到IDEA中

导入后等待编译,大约需要半小时时间,编译完成:

5:添加测试模块代码

new->model->gradle-->输入模块名称

build.gradle添加依赖


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

随意添加任意bean:
package com.tie.beans;

import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl {

   public void sayHi(){
      System.out.println("Hello Spring!");
   }
}   

package com.tie.beans;

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


@Configuration
@ComponentScan("com.tie.beans")
public class MainStat {

   public static void main(String[] args) {
      ApplicationContext context=new AnnotationConfigApplicationContext(MainStat.class);
      UserServiceImpl bean = context.getBean(UserServiceImpl.class);
      bean.sayHi();

   }

}

正确输出,大功告成!

6:运行时遇到错误

Kotlin java.lang.abstractmethoderror Error:Kotlin: [Internal Error] java.lang.IllegalStateException:
    
解决方案:


这是kotlin插件版本问题,在idea 工具中File->Settings->Plugins输入Kotlin,点击Browse repositories去升级