温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Idea 如何搭建Spring源码环境

发布时间:2020-10-28 15:57:07 来源:亿速云 阅读:184 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关Idea 如何搭建Spring源码环境,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

1. clone spring-framework 项目

1.1 找到github spring-framwwork 项目

先登录github 找到 spring-framework项目

https://github.com/spring-projects

Idea 如何搭建Spring源码环境

我选择的是 5.0.x

Idea 如何搭建Spring源码环境

如果你觉得你网速可以,那你可以直接从 github clone 下来, 我这里先把项目传到 gitee

1.2 fork 到gitee 码云

Idea 如何搭建Spring源码环境

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

Idea 如何搭建Spring源码环境

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

Idea 如何搭建Spring源码环境

Idea 如何搭建Spring源码环境

2.2 再导入导 idea 中

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

Idea 如何搭建Spring源码环境

Idea导入 选择文件夹

Idea 如何搭建Spring源码环境

选择使用Gradle

![image-20200924103346932](/Users/johnny/Library/Application Support/typora-user-images/image-20200924103346932.jpg)

静静的等待

Idea 如何搭建Spring源码环境

Idea 如何搭建Spring源码环境

2.3 排除 "spring-aspects"

排除了 spring-aspects 项目

打开settings.gradle 把 //include "spring-aspects" 注释了

Idea 如何搭建Spring源码环境

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

可以发现 依赖都加载完成后,idea 就能识别我们导入的 spring项目了,并且图标都变亮了

Idea 如何搭建Spring源码环境

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

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

3.1 新建module

右击项目 -》 new -》 module 选择 gradle 项目

Idea 如何搭建Spring源码环境

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

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<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);
 }
}

看完上述内容,你们对Idea 如何搭建Spring源码环境有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI