189 8069 5689

SpringFramework源代码环境搭建

概要

本文介绍使用IntelliJ IDEA搭建Spring Framework源代码环境,用于源代码阅读与debug。

创新互联公司网站建设由有经验的网站设计师、开发人员和项目经理组成的专业建站团队,负责网站视觉设计、用户体验优化、交互设计和前端开发等方面的工作,以确保网站外观精美、成都网站设计、网站制作、外贸营销网站建设易于使用并且具有良好的响应性。

环境搭建

1.下载源代码

访问Spring Framework在GitHub的地址,下载最新源代码。本人在下载时,版本号为5.2.2.BUILD-SNAPSHOT

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

2.根据说明导入IntelliJ IDEA

根据源代码文件中的IDEA导入说明进行操作,说明文件为源代码根目录的import-into-idea.md

Precompile `spring-oxm` with `./gradlew :spring-oxm:compileTestJava`
Import into IntelliJ (File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle)
When prompted exclude the `spring-aspects` module (or after the import via File-> Project Structure -> Modules)
Code away
  • 1.需要预编译spring-oxm,通过控制台,进入到Spring Framework目录,运行命令gradlew :spring-oxm:compileTestJava。
    注:Spring Framework是通过Gradle进行的编译打包,故需要提前安装Gradle
  • 2.导入至IDEA中, 操作方法:File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle
    Spring Framework源代码环境搭建
  • 3.排除spring-aspects模块,因IDEA的问题该模块不会被识别,如不排除则会提示编译错误。
    操作方法:spring-aspects右键 -> Load/Unload modules -> 添加spring-aspects为Unloaded modules
    Spring Framework源代码环境搭建

    测试DEMO

    在源代码项目目录下,本人直接创建了一个Maven测试项目,并创建了一个简单的Bean注册于获取的例子。
    pom.xml


  4.0.0
  cn.daiwuliang
  demo
  1.0-SNAPSHOT
  demo
  
  http://www.example.com
  
    UTF-8
    1.8
    1.8
  
  
    
      junit
      junit
      4.11
      test
    
      
      
          commons-logging
          commons-logging
          1.2
      
  
  
    
      
        
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-jar-plugin
          3.0.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
        
        
          maven-site-plugin
          3.7.1
        
        
          maven-project-info-reports-plugin
          3.0.0
        
      
    
  

TestDemo
package demo.test;
public class TestDemo {
    public String test(String str) {
        return String.format("ECHO: %s", str);
    }
}
App.java
package demo.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class App {
    public static void main(String[] args) {
        String XMLPath = "D:\\spring-framework-master\\demo\\src\\main\\java\\demo\\test\\spring-config.xml";
        ApplicationContext applicationContext = new FileSystemXmlApplicationContext(XMLPath);
        TestDemo td = (TestDemo) applicationContext.getBean("testDemo");
        System.out.println(td.test("Hello World!!!"));
    }
}
spring-config.xml


    

运行问题

  1. Demo模块需要添加相应的spring模块的依赖,添加方式:File -> Project Structure -> Dependencies,目前Demo中仅实现了简单的例子,故只添加了如下几个模块:
    Spring Framework源代码环境搭建
  2. spring-core中缺少commons-logging的依赖
    虽然已经在demo的pom中添加了依赖,但仍旧报错,本人的解决办法为在spring-core的spring-core.gradle再添加一次,并重新构建
    dependencies {
     ......
     compile("commons-logging:commons-logging:1.2")
    }
    
  3. CoroutinesRegistrar中找不到变量CoroutinesUtils
    CoroutinesUtils位于spring-core目录下的kotlin-coroutines中,看结构其为一个独立的模块,并且spring-core并未关联依赖。
    找到kotlin-coroutines/build/libs/kotlin-coroutines-5.2.2.BUILD-SNAPSHOT.jar->右键Add as Library

至此,运行App中的main方法,得到以下结果:

Hello World!

新闻名称:SpringFramework源代码环境搭建
网站链接:http://cdxtjz.cn/article/gjispc.html

其他资讯