关于java:Idea-搭建Spring源码环境

20次阅读

共计 4387 个字符,预计需要花费 11 分钟才能阅读完成。

Idea 搭建 Spring 源码环境

本篇次要解说如何应用 Ideal 搭建 Spring 的源码环境,想必大家都会多多少少去看过 Spring 的局部源码,个别咱们都是间接点进某个 Spring 类 而后 Idea 下面去下载,然而的确比拟麻烦,而且不能增加本人对源码的正文 了解,本篇就来解决这个问题,手把手应用 Idea 搭建 Spring framework,并且间接在 Spring framework 我的项目中增加咱们本人的 module 来验证环境是否正确。本过程会比拟耗时 而且容易出错 慢慢来吧。

1. clone spring-framework 我的项目

1.1 找到 github spring-framwwork 我的项目

先登录 github 找到 spring-framework 我的项目

https://github.com/spring-projects

我抉择的是 5.0.x

如果你感觉你网速能够,那你能够间接从 github clone 下来,我这里先把我的项目传到 gitee

1.2 fork 到 gitee 码云

拉取你要的 分支 git clone -b 分支

2. 查看 import-into-idea.md 文件

在下载的源码中 有一个文件是 import-into-idea 的 md 文件 外面有对于导入 idea 须要的 注意事项,咱们来关上它

The following has been tested against IntelliJ IDEA 2016.2.2

## Steps

_Within your locally cloned spring-framework working directory:_

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

## Known issues

1. `spring-core` and `spring-oxm` should be pre-compiled due to repackaged dependencies.
See `*RepackJar` tasks in the build and https://youtrack.jetbrains.com/issue/IDEA-160605).
2. `spring-aspects` does not compile due to references to aspect types unknown to
IntelliJ IDEA. See https://youtrack.jetbrains.com/issue/IDEA-64446 for details. In the meantime, the
'spring-aspects' can be excluded from the project to avoid compilation errors.
3. While JUnit tests pass from the command line with Gradle, some may fail when run from
IntelliJ IDEA. Resolving this is a work in progress. If attempting to run all JUnit tests from within
IntelliJ IDEA, you will likely need to set the following VM options to avoid out of memory errors:
    -XX:MaxPermSize=2048m -Xmx2048m -XX:MaxHeapSize=2048m
4. If you invoke "Rebuild Project" in the IDE, you'll have to generate some test
resources of the `spring-oxm` module again (`./gradlew :spring-oxm:compileTestJava`)    


## Tips

In any case, please do not check in your own generated .iml, .ipr, or .iws files.
You'll notice these files are already intentionally in .gitignore. The same policy goes for eclipse metadata.

## FAQ

Q. What about IntelliJ IDEA's own [Gradle support](https://confluence.jetbrains.net/display/IDEADEV/Gradle+integration)?

A. Keep an eye on https://youtrack.jetbrains.com/issue/IDEA-53476

大抵意思就是

2.1 在源码目录下执行 ./gradlew :spring-oxm:compileTestJava

2.2 再导入导 idea 中

会开始下载 Gradle 构建工具 等,会依据 gradle-wrapper.properties 中的指定版本下载,最好不要批改它的版本

Idea 导入 抉择文件夹

抉择应用 Gradle

静静的期待

2.3 排除 “spring-aspects”

排除了 spring-aspects 我的项目

关上 settings.gradle 把 //include “spring-aspects” 正文了

2.4 下载完依赖后(耗时可能要个 15-30 分钟)

能够发现 依赖都加载实现后,idea 就能辨认咱们导入的 spring 我的项目了,并且图标都变亮了

3. 引入自定义模块放入 SpringFramework 我的项目下

上面就是来验证 咱们的 源码环境是否 失常,须要引入一个自定义的 模块,并且依赖 core bean 等 spring 依赖

3.1 新建 module

右击我的项目 -》new -》module 抉择 gradle 我的项目

3.2 增加 依赖

在新建的 module 下 关上 build.gradle 引入上面的依赖 spring-beans , spring-context , spring-core , spring-expression

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'

    compile(project(":spring-beans"))
    compile(project(":spring-context"))
    compile(project(":spring-core"))
    compile(project(":spring-expression"))
}
3.3 查看 module 是否被引入

关上 settings.gradle 增加 include 'spring-demo',默认应用我说的创立 module 形式 会主动增加的最好检查一下

3.4 编写 测试代码
3.4.1 定义 Person 类
package com.johnny.bean;

/**
 * @author johnny
 * @create 2020-09-07 下午 11:22
 **/
public class Person {

   private String name;

   private int age;

   @Override
   public String toString() {
      return "Person{" +
            "name='" + name + '\'' +
            ", age=" + age +
            '}';
   }

   public String getName() {return name;}

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

   public int getAge() {return age;}

   public void setAge(int age) {this.age = age;}
}
3.4.2 resources 下新建 demo.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 class="com.johnny.bean.Person" id="person">
      <property name="name" value="johnny"/>
      <property name="age" value="10"/>
   </bean>
</beans>
3.4.3 新建 main 加载 xml 并且从容器中获取 bean
package com.johnny.bean;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author johnny
 * @create 2020-09-07 下午 11:24
 **/
public class DemoMain {public static void main(String[] args) {ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("demo.xml");
      Person person = classPathXmlApplicationContext.getBean(Person.class);
      System.out.println(person);
   }
}

能够看到 能获取到 容器中的 Bean,示意咱们的 spring 环境搭建正确

总结

本篇次要解说 如何应用 idea 搭建 spring 源码环境,过程其实很耗时 而且特地容易出错,总结就是 1. clone 代码,2. 进入源码目录执行 ./gradlew :spring-oxm:compileTestJava 3. 导入 idea 中 4. 排除 exclude the spring-aspects module 5. 自定义 module 验证环境,祝福大家环境搭建顺利。。。最好开个墙

本文由博客一文多发平台 OpenWrite 公布!

正文完
 0