Spring Framework源代码环境搭建

概要

本文介绍使用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

1. Precompile `spring-oxm` with `./gradlew :spring-oxm:compileTestJava`
2. Import into IntelliJ (File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle)
3. When prompted exclude the `spring-aspects` module (or after the import via File-> Project Structure -> Modules)
4. 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源代码环境搭建_第1张图片
  • 3.排除spring-aspects模块,因IDEA的问题该模块不会被识别,如不排除则会提示编译错误。
    操作方法:spring-aspects右键 -> Load/Unload modules -> 添加spring-aspects为Unloaded modules
    Spring Framework源代码环境搭建_第2张图片

测试DEMO

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

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0modelVersion>

  <groupId>cn.daiwulianggroupId>
  <artifactId>demoartifactId>
  <version>1.0-SNAPSHOTversion>

  <name>demoname>
  
  <url>http://www.example.comurl>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <maven.compiler.source>1.8maven.compiler.source>
    <maven.compiler.target>1.8maven.compiler.target>
  properties>

  <dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.11version>
      <scope>testscope>
    dependency>

	  
	  <dependency>
	  	<groupId>commons-logginggroupId>
	  	<artifactId>commons-loggingartifactId>
	  	<version>1.2version>
	  dependency>
  dependencies>

  <build>
    <pluginManagement>
      <plugins>
        
        <plugin>
          <artifactId>maven-clean-pluginartifactId>
          <version>3.1.0version>
        plugin>
        
        <plugin>
          <artifactId>maven-resources-pluginartifactId>
          <version>3.0.2version>
        plugin>
        <plugin>
          <artifactId>maven-compiler-pluginartifactId>
          <version>3.8.0version>
        plugin>
        <plugin>
          <artifactId>maven-surefire-pluginartifactId>
          <version>2.22.1version>
        plugin>
        <plugin>
          <artifactId>maven-jar-pluginartifactId>
          <version>3.0.2version>
        plugin>
        <plugin>
          <artifactId>maven-install-pluginartifactId>
          <version>2.5.2version>
        plugin>
        <plugin>
          <artifactId>maven-deploy-pluginartifactId>
          <version>2.8.2version>
        plugin>
        
        <plugin>
          <artifactId>maven-site-pluginartifactId>
          <version>3.7.1version>
        plugin>
        <plugin>
          <artifactId>maven-project-info-reports-pluginartifactId>
          <version>3.0.0version>
        plugin>
      plugins>
    pluginManagement>
  build>
project>

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

<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
	<bean id="testDemo" class="demo.test.TestDemo"/>
beans>

运行问题

  1. Demo模块需要添加相应的spring模块的依赖,添加方式:File -> Project Structure -> Dependencies,目前Demo中仅实现了简单的例子,故只添加了如下几个模块:
    Spring Framework源代码环境搭建_第3张图片
  2. spring-core中缺少commons-logging的依赖
    虽然已经在demo的pom中添加了依赖,但仍旧报错,本人的解决办法为在spring-core的spring-core.gradle再添加一次,并重新构建
dependencies {
	......
	compile("commons-logging:commons-logging:1.2")
}
  1. 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!

你可能感兴趣的:(其他)