建議
分析源碼建議不要使用Idea或者Eclipse等IDE工具的反編譯功能或者導入源碼包的方式看源碼,那樣不能給框架的源碼做注釋,所以分析源碼之前都得先下載源碼並構建,然后在項目中新建一個Module看代碼是否能夠正常啟動與運行。
另外因為博客是在看源碼后寫的,我自己看的源碼版本是2.1.X,這里演示的2.2.X,方法和2.1.X是一樣的。
備注:無意中發現一個比較優質的博客:https://blog.csdn.net/qq_26000415/article/list/7,速覽了一下他的文章列表,都是挺高級的貨,推廣一下(當然每個博主寫的文章都可能有誤,建議大家在閱讀的時候一定要自己驗證)
官方BUILD SOURCE建議
下面這段英文對於想要快速構建編譯環境的同學就不要嘗試了,因為它會下載一個Maven,使用默認的配置重新下載一些依賴包,下載Maven耗時,下載你之前已經下載過的包更耗時。。。但是它有應該有一個優點簡單一步構建成功,否則就不是官方文檔。
You don’t need to build from source to use Spring Boot (binaries in repo.spring.io), but if you want to try out the latest and greatest, Spring Boot can be easily built with the maven wrapper. You also need JDK 1.8.
$ ./mvnw clean install
If you want to build with the regular mvn
command, you will need Maven v3.5.0 or above.
Note | You may need to increase the amount of memory available to Maven by setting a MAVEN_OPTS environment variable with the value -Xmx512m . Remember to set the corresponding property in your IDE as well if you are building and running tests there (e.g. in Eclipse go to Preferences→Java→Installed JREs and edit the JRE definition so that all processes are launched with those arguments). This property is automatically set if you use the maven wrapper. |
---|---|
Also see CONTRIBUTING.adoc if you wish to submit pull requests, and in particular please fill out the Contributor’s Agreement before your first change, however trivial.
Building reference documentation
First of all, make sure you have built the project:
$ ./mvnw clean install
The reference documentation requires the documentation of the Maven plugin to be available so you need to build that first since it’s not generated by default.
$ ./mvnw clean install -pl spring-boot-project/spring-boot-tools/spring-boot-maven-plugin -Pdefault,full
The documentation also includes auto-generated information about the starters. You might have that in your local repository already (per the first step) but if you want to refresh it:
$ ./mvnw clean install -f spring-boot-project/spring-boot-starters
Once this is done, you can build the reference documentation with the command below:
$ ./mvnw clean prepare-package -pl spring-boot-project/spring-boot-docs -Pdefault,full
使用已有Maven構建
下載
地址:https://github.com/spring-projects/spring-boot/tree/2.2.x
修改Maven配置
有的同學從來沒有使用過Maven命令行編譯過代碼,那么使用命令行編譯,Maven讀取的還是Maven自己目錄下的setting.xml文件,所以要修改里面的內容
修改1,將Maven本地倉庫目錄修改為IDE工具中Maven本地倉庫目錄,比如:
<localRepository>F:\data\maven\repository</localRepository>
修改2:為了能更快的下載,可以將配置文件中的Maven倉庫鏡像設置為阿里雲的
<mirrors> <!-- 阿里雲倉庫 central--> <mirror> <id>alimaven</id> <mirrorOf>central</mirrorOf> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/repositories/central/</url> </mirror> <!-- 中央倉庫1 --> <mirror> <id>repo1</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://repo1.maven.org/maven2/</url> </mirror> <!-- 中央倉庫2 --> <mirror> <id>repo2</id> <mirrorOf>central</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://repo2.maven.org/maven2/</url> </mirror> <!-- 阿里雲的maven-public路徑 --> <mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror> </mirrors>
如果嫌這個比較麻煩,直接將自己IDE中指定的setting.xml文件覆蓋MAVEN_HOME/conf/setting.xml
構建
使用命令構建,使用skipTests跳過SpringBoot中的測試,
mvn clean install -DskipTests -Pfast
整個構建過程花的時間比較長,我自己使用了1小時53分鍾,構建成功的結果頁
導入項目
打開Idea,選擇File-open file-選擇構建成功目錄下pom.xml,然后選擇open as project選項
最后等待導入成功
新建測試工程
在spring-boot-project目錄下新建一個工程
在main目錄下新建一個resources目錄,並標記為“Resources root”
導入spring-boot-starter-web依賴
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.2.3.BUILD-SNAPSHOT</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.0</version> <scope>provided</scope> </dependency> </dependencies>
新建啟動類和Controller
@SpringBootApplication public class BootApplication { public static void main(String[] args) { SpringApplication.run(BootApplication.class,args); } }
@RestController public class UserController { @RequestMapping("/query") public User query(){ return new User("Messi",33); } }
@Setter @Getter public class User { private String userName; private Integer age; public User(){} public User(String userName,Integer age){ this.userName=userName; this.age=age; } }
啟動-測試
那么恭喜,接下來就可以開始分析源碼之旅了
來源:沈陽網站優化