您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關Idea 如何搭建Spring源碼環境,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
1. clone spring-framework 項目
先登錄github 找到 spring-framework項目
https://github.com/spring-projects
我選擇的是 5.0.x
如果你覺得你網速可以,那你可以直接從 github clone 下來, 我這里先把項目傳到 gitee
拉取你要的 分支 git clone -b 分支
在下載的源碼中 有一個文件是 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
大致意思就是
./gradlew :spring-oxm:compileTestJava
會開始下載 Gradle 構建工具 等,會根據 gradle-wrapper.properties 中的指定版本下載,最好不要修改它的版本
Idea導入 選擇文件夾
選擇使用Gradle
![image-20200924103346932](/Users/johnny/Library/Application Support/typora-user-images/image-20200924103346932.jpg)
靜靜的等待
排除了 spring-aspects 項目
打開settings.gradle 把 //include "spring-aspects" 注釋了
可以發現 依賴都加載完成后,idea 就能識別我們導入的 spring項目了,并且圖標都變亮了
下面就是來驗證 我們的 源碼環境是否 正常, 需要引入一個自定義的 模塊,并且依賴 core bean 等spring依賴
右擊項目 -》 new -》 module 選擇 gradle 項目
在新建的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")) }
打開settings.gradle 添加 include 'spring-demo' ,默認使用我說的創建module 方式 會自動添加的最好檢查一下
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); } }
看完上述內容,你們對Idea 如何搭建Spring源碼環境有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。