珠聯壁合地設天造|M1 Mac os(Apple Silicon)基於vscode(arm64)配置搭建Java開發環境(集成web框架Springboot)

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"原文轉載自「劉悅的技術博客」","attrs":{}},{"type":"link","attrs":{"href":"https://v3u.cn/a_id_194","title":"","type":null},"content":[{"type":"text","text":"https://v3u.cn/a_id_194","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"也許有人從未聽說過Python,但是不會有人沒聽說過Java,它作爲一個擁有悠久歷史的老牌編程語言,常年雄踞TIOBE編程語言榜首,其頂尖的霸主地位不可撼動,而M1 mac業已發佈了一段時間,作爲跨時代的頂級芯片系統,這兩大巨頭能否珠聯璧合,讓開發者們猛虎添翼、更上層樓?本次我們嘗試在M1 mac系統中搭建Java開發環境,並且集成目前Web開發領域紅的發紫的Springboot框架,另外,誰說玩兒Java就必須得用Eclipse或者IntelliJ IDEA?我們就驕傲地使用Vscode。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先需要應對的是OpenJDK的選擇,這裏推薦使用Azul公司提供的zulu版本,Zulu又是什麼? 它是Azul提供商業支持的OpenJDK商業發行版,與Oracle JDK有高度的一致性,而最重要的一點是,它支持arm64架構,這正是M1 mac 運行Java項目的先決條件,下載地址:https://www.azul.com/downloads/?version=java-11-lts&os=macos&architecture=arm-64-bit&package=jdk ,這裏我們選擇了LTS(長期維護)版本的jdk11,注意一定要選擇Arm64版本的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/69/69ad00e32d07b088dde1ebd90812c75c.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下載成功後,雙擊安裝即可,安裝成功後,zulu會把jdk放在下面這個路徑裏:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果願意,可以配置一下環境變量,這樣方便以後操作多版本jdk:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"vim ~/.zshrc\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"添加:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"存盤退出,重啓終端,輸入","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"java -version\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"返回:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"openjdk version \"11.0.11\" 2021-04-20 LTS \nOpenJDK Runtime Environment Zulu11.48+21-CA (build 11.0.11+9-LTS) \nOpenJDK 64-Bit Server VM Zulu11.48+21-CA (build 11.0.11+9-LTS, mixed mode) \n(base) ➜ ~ \n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"表示arm64版本的JDK安裝成功。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接着來安裝Maven,它是專門爲Java項目打造的管理和構建工具,它能幫我們快速編譯,測試,打包,發佈Java項目,非常方便。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一般情況下,可以去Maven官網下載二進制文件,這裏推薦一個更簡單的方案,就是使用Homebrew,注意這裏一定要使用arm64架構的,安裝命令如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"/bin/bash -c \"$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)\"\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"基於arm64的Homebrew統一安裝在系統的/opt/homebrew目錄,主要是爲了和老版的x86區別開。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"安裝成功後,輸入命令:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"brew -v\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"將會返回版本號:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"(base) ➜ ~ brew -v \nHomebrew 3.0.2 \nHomebrew/homebrew-core (git revision ae4118; last commit 2021-02-28) \nHomebrew/homebrew-cask (git revision 9510a9; last commit 2021-05-20)\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"代表安裝成功。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"之後使用brew命令來安裝Maven:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"brew install maven\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"就是這麼簡單,安裝成功後,無需單獨配置環境變量,直接可以運行:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"mvn -version\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"返回信息:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"(base) ➜ ~ mvn -version \nApache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) \nMaven home: /opt/homebrew/Cellar/maven/3.6.3_1/libexec \nJava version: 11.0.11, vendor: Azul Systems, Inc., runtime: /Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home \nDefault locale: zh_CN_#Hans, platform encoding: UTF-8 \nOS name: \"mac os x\", version: \"11.2.2\", arch: \"aarch64\", family: \"mac\" \n(base) ➜ ~\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"默認會安裝最新穩定版。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同時,運行 brew list maven,就可以查看maven的具體安裝目錄,這個一會兒會用到:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"(base) ➜ ~ brew list maven \n/opt/homebrew/Cellar/maven/3.6.3_1/bin/mvn \n/opt/homebrew/Cellar/maven/3.6.3_1/bin/mvnDebug \n/opt/homebrew/Cellar/maven/3.6.3_1/bin/mvnyjp \n/opt/homebrew/Cellar/maven/3.6.3_1/libexec/bin/ (4 files) \n/opt/homebrew/Cellar/maven/3.6.3_1/libexec/boot/ (2 files) \n/opt/homebrew/Cellar/maven/3.6.3_1/libexec/conf/ (3 files) \n/opt/homebrew/Cellar/maven/3.6.3_1/libexec/lib/ (70 files)\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"至此,運行Java項目的兩大前提:JDK和Maven,我們就都安裝完畢了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面讓我們配置M1 mac下開發Java的利器:Vscode,去官網下載即可:https://code.visualstudio.com/Download# ,但是需要注意的是,一定要選擇arm64架構的版本:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/2b/2bae235cb4d26262ef8ac77f70483bc0.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下載後,進行安裝操作,安裝完成後,我們需要裝兩個插件幫助我們運行Java項目。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先打開左側應用商店菜單,搜索:Java Extension Pack 。進行安裝,這是Java的擴展包,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/a7/a7968c4cc1b12588e2a92d7fa45a96f6.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"隨後,搜索:Spring Boot Extension Pack ,進行安裝,它是Springboot的擴展包。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/50/5071eadad185d65c3f10ed992157e65b.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"安裝這倆個擴展包時,系統會陸續自動安裝另外一些依賴包,耐心等待即可。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"安裝成功後,重啓vscode,隨後打開上面菜單中的code->首選項->設置,搜索maven,然後點選“在settings.json”中編輯:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/a7/a7968c4cc1b12588e2a92d7fa45a96f6.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在配置文件中,需要加入以下配置:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":" \"java.home\": \"/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home\", //JAVA_HOME \n \n // 指定 maven 的配置文件位置 \n \"java.configuration.maven.userSettings\": \"/opt/homebrew/Cellar/maven/3.6.3_1/libexec/conf/settings.xml\", \n \n //maven 環境變量,指定爲 mvn \n \"maven.executable.path\": \"mvn\", \n \n //自動更新影響的pom \n \"maven.pomfile.autoUpdateEffectivePOM\": true, \n //跳過測試, 不需要跳過可刪除 \n \"maven.executable.options\": \"-DskipTests\", \n \n //當此值設爲 true 時,且配置了“java.home”時, \n //新建終端會自動添加環境 JDK 環境變量 爲 “java.home\"的值 \n \"maven.terminal.useJavaHome\": true, \n \n //指定 maven 所用 jdk \n \"maven.terminal.customEnv\": [ \n { \n \"environmentVariable\": \"JAVA_HOME\", //環境變量名 \n \"value\": \"/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home\", //JDK 安裝 路徑 \n \n } \n ], \n \n //在構建時,自動修改 java 項目的配置信息 ,classpath信息 \n \"java.configuration.updateBuildConfiguration\": \"automatic\",\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏主要的變量就是JDK的路徑和Maven配置文件的路徑,分別對應到剛剛安裝好的兩個路徑,分別是:/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home和/opt/homebrew/Cellar/maven/3.6.3_1/libexec/conf/settings.xml","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"做好配置後,安裝好的擴展就可以用了,再次重啓vscode。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"讓我們來體驗一下Springboot的魅力,輸入快捷鍵:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"command + shift + p","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"打開命令模式,鍵入:spring,選擇創建 Maven 項目:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/a7/a7968c4cc1b12588e2a92d7fa45a96f6.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"選擇Springboot版本:2.4.5;語言:Java;GroupId直接默認回車;項目名稱:mydemo;打包方式選擇:Jar;JDK版本:11;默認依賴:DevTools(代碼修改熱更新,無需重啓)、Web(集成tomcat、SpringMVC)、Lombok(智能生成setter、getter、toString等接口,無需手動生成,代碼更簡介)、Thymeleaf (模板引擎)。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"選擇好要引入的包後直接回車,在新彈出的窗口中選擇項目路徑,到此Spring Boot項目創建完成:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d8/d8accbf0a0b4123f52ed7ab4d14fda6b.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"隨後將創建好的mydemo項目導入到vscode的工作空間:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/1a/1a9c9e99d28f173fb6f784122d5c513c.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"之後,點選左側小蟲子圖標,爲項目添加啓動文件,選擇Java:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/14/1423c756d1d0c0e8544e673b5c662713.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"此時,需要等待一下,vscode會導入相關依賴。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"創建成功後,會發現項目的根目錄新增了一個.vscode文件件裏面有一個啓動文件launch.json:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"{ \n // 使用 IntelliSense 瞭解相關屬性。 \n // 懸停以查看現有屬性的描述。 \n // 欲瞭解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387 \n \"version\": \"0.2.0\", \n \"configurations\": [ \n { \n \"type\": \"java\", \n \"name\": \"Launch Current File\", \n \"request\": \"launch\", \n \"mainClass\": \"${file}\" \n }, \n { \n \"type\": \"java\", \n \"name\": \"Launch MydemoApplication\", \n \"request\": \"launch\", \n \"mainClass\": \"com.example.mydemo.MydemoApplication\", \n \"projectName\": \"mydemo\" \n } \n ] \n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有了它,我們就可以用內置服務器啓動項目了,現在打開項目文件夾中的入口文件/mydemo/src/main/java/com/example/mydemo/MydemoApplication.java,改成下面的樣子:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"package com.example.mydemo; \n \nimport org.springframework.boot.SpringApplication; \nimport org.springframework.boot.autoconfigure.SpringBootApplication; \n \nimport org.springframework.web.bind.annotation.RequestMapping; \nimport org.springframework.web.bind.annotation.RestController; \n \n@RestController \n@SpringBootApplication \npublic class MydemoApplication { \n \n public static void main(String[] args) { \n SpringApplication.run(MydemoApplication.class, args); \n } \n \n @RequestMapping(\"/\") \n public String home() { \n return \"Hey,歡迎來到M1 mac的世界\"; \n } \n \n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏我們用簡單的路由映射到一個簡單的視圖,首頁返回一串文字。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"接着打開小蟲子圖標,點開控制檯,然後點擊三角箭頭啓動項目:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ba/ba3d50374272bdec2e2be99f8f3ab72a.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"服務應聲啓動:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"(base) ➜ mydemo git:(master) ✗ /usr/bin/env \"/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java\" -agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:62011 -Dfile.encoding=UTF-8 -cp /var/folders/5x/gpftd0654bv7zvzyv39449rc0000gp/T/cp_bp8k59s9mveb6o8xc0fjjde5w.jar com.example.mydemo.MydemoApplication \n \n . ____ _ __ _ _ \n /\\\\ / ___'_ __ _ _(_)_ __ __ _ \\ \\ \\ \\ \n( ( )\\___ | '_ | '_| | '_ \\/ _` | \\ \\ \\ \\ \n \\\\/ ___)| |_)| | | | | || (_| | ) ) ) ) \n ' |____| .__|_| |_|_| |_\\__, | / / / / \n =========|_|==============|___/=/_/_/_/ \n :: Spring Boot :: (v2.3.10.RELEASE) \n \n2021-05-20 23:05:27.439 INFO 39304 --- [ restartedMain] com.example.mydemo.MydemoApplication : Starting MydemoApplication on liuyue with PID 39304 (/Users/liuyue/wodfan/work/mydemo/bin/main started by liuyue in /Users/liuyue/wodfan/work/mydemo) \n2021-05-20 23:05:27.443 INFO 39304 --- [ restartedMain] com.example.mydemo.MydemoApplication : No active profile set, falling back to default profiles: default \n2021-05-20 23:05:27.675 INFO 39304 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable \n2021-05-20 23:05:27.676 INFO 39304 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG' \n2021-05-20 23:05:29.223 INFO 39304 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) \n2021-05-20 23:05:29.237 INFO 39304 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat] \n2021-05-20 23:05:29.237 INFO 39304 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.45] \n2021-05-20 23:05:29.301 INFO 39304 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext \n2021-05-20 23:05:29.301 INFO 39304 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1624 ms \n2021-05-20 23:05:29.689 INFO 39304 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' \n2021-05-20 23:05:29.975 WARN 39304 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : Unable to start LiveReload server \n2021-05-20 23:05:30.036 INFO 39304 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' \n2021-05-20 23:05:30.063 INFO 39304 --- [ restartedMain] com.example.mydemo.MydemoApplication : Started MydemoApplication in 3.13 seconds (JVM running for 3.838)\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"此時打開瀏覽器,訪問:http://localhost:8080","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b4/b4ab301d9a3ff5a4f79307cf278abba1.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"結語:在過往印象中,Java是一款嚴肅的語言,它古板、教條、一絲不苟,處處循規蹈矩,但其實,隨着時代的發展,你會發現它其實是與時俱進的,在時代的浪潮中迸發出了Spring boot這樣“習慣優於配置”的框架,徹底顛覆了傳統Java 項目笨重、繁多的配置、低下的開發效率、複雜的部署流程以及第三方技術集成難度大的特點,並且在M1 mac系統下,我們可以很輕鬆的用Vscode來寫Java代碼,就像寫Python一樣。最後,奉上項目代碼與君共觴:https://gitee.com/QiHanXiBei/mydemo.git","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"原文轉載自「劉悅的技術博客」 ","attrs":{}},{"type":"link","attrs":{"href":"https://v3u.cn/a_id_194","title":"","type":null},"content":[{"type":"text","text":"https://v3u.cn/a_id_194","attrs":{}}]}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章