电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

手把手教你怎么創建spring項目

瀏覽:55日期:2022-08-11 10:54:37
創建Spring項目

通過spring.io生成初始代碼,配置如下

手把手教你怎么創建spring項目

下載好會得到一個.zip文件,解壓導入IDEA就可以開始了,這是基礎的項目結構

手把手教你怎么創建spring項目

讓我們打開src/main/java/geektime/spring/hello/hellospring/HelloSpringApplication.java,修改成如下代碼

package geektime.spring.hello.hellospring;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@RestControllerpublic class HelloSpringApplication {public static void main(String[] args) {SpringApplication.run(HelloSpringApplication.class, args);}@RequestMapping('/hello')public String hello() {return 'Hello Spring';}}

接著運行我們修改的代碼,運行成功后,console會打印如下信息

/Library/Java/JavaVirtualMachines/jdk-15.0.1.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=55568:/Applications/IntelliJ IDEA CE.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Users/lidean/Downloads/hello-spring/target/classes:/Users/lidean/.m2/repository/org/springframework/boot/spring-boot-starter-actuator/2.5.0/spring-boot-starter-actuator-2.5.0.jar:/Users/lidean/.m2/repository/org/springframework/boot/spring-boot-starter/2.5.0/spring-boot-starter-2.5.0.jar:/Users/lidean/.m2/repository/org/springframework/boot/spring-boot/2.5.0/spring-boot-2.5.0.jar:/Users/lidean/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.5.0/spring-boot-autoconfigure-2.5.0.jar:/Users/lidean/.m2/repository/org/springframework/boot/spring-boot-starter-logging/2.5.0/spring-boot-starter-logging-2.5.0.jar:/Users/lidean/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:/Users/lidean/.m2/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar:/Users/lidean/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.14.1/log4j-to-slf4j-2.14.1.jar:/Users/lidean/.m2/repository/org/apache/logging/log4j/log4j-api/2.14.1/log4j-api-2.14.1.jar:/Users/lidean/.m2/repository/org/slf4j/jul-to-slf4j/1.7.30/jul-to-slf4j-1.7.30.jar:/Users/lidean/.m2/repository/jakarta/annotation/jakarta.annotation-api/1.3.5/jakarta.annotation-api-1.3.5.jar:/Users/lidean/.m2/repository/org/yaml/snakeyaml/1.28/snakeyaml-1.28.jar:/Users/lidean/.m2/repository/org/springframework/boot/spring-boot-actuator-autoconfigure/2.5.0/spring-boot-actuator-autoconfigure-2.5.0.jar:/Users/lidean/.m2/repository/org/springframework/boot/spring-boot-actuator/2.5.0/spring-boot-actuator-2.5.0.jar:/Users/lidean/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.12.3/jackson-databind-2.12.3.jar:/Users/lidean/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.12.3/jackson-annotations-2.12.3.jar:/Users/lidean/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.12.3/jackson-core-2.12.3.jar:/Users/lidean/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.12.3/jackson-datatype-jsr310-2.12.3.jar:/Users/lidean/.m2/repository/io/micrometer/micrometer-core/1.7.0/micrometer-core-1.7.0.jar:/Users/lidean/.m2/repository/org/hdrhistogram/HdrHistogram/2.1.12/HdrHistogram-2.1.12.jar:/Users/lidean/.m2/repository/org/latencyutils/LatencyUtils/2.0.3/LatencyUtils-2.0.3.jar:/Users/lidean/.m2/repository/org/springframework/boot/spring-boot-starter-web/2.5.0/spring-boot-starter-web-2.5.0.jar:/Users/lidean/.m2/repository/org/springframework/boot/spring-boot-starter-json/2.5.0/spring-boot-starter-json-2.5.0.jar:/Users/lidean/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jdk8/2.12.3/jackson-datatype-jdk8-2.12.3.jar:/Users/lidean/.m2/repository/com/fasterxml/jackson/module/jackson-module-parameter-names/2.12.3/jackson-module-parameter-names-2.12.3.jar:/Users/lidean/.m2/repository/org/springframework/boot/spring-boot-starter-tomcat/2.5.0/spring-boot-starter-tomcat-2.5.0.jar:/Users/lidean/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.46/tomcat-embed-core-9.0.46.jar:/Users/lidean/.m2/repository/org/apache/tomcat/embed/tomcat-embed-el/9.0.46/tomcat-embed-el-9.0.46.jar:/Users/lidean/.m2/repository/org/apache/tomcat/embed/tomcat-embed-websocket/9.0.46/tomcat-embed-websocket-9.0.46.jar:/Users/lidean/.m2/repository/org/springframework/spring-web/5.3.7/spring-web-5.3.7.jar:/Users/lidean/.m2/repository/org/springframework/spring-beans/5.3.7/spring-beans-5.3.7.jar:/Users/lidean/.m2/repository/org/springframework/spring-webmvc/5.3.7/spring-webmvc-5.3.7.jar:/Users/lidean/.m2/repository/org/springframework/spring-aop/5.3.7/spring-aop-5.3.7.jar:/Users/lidean/.m2/repository/org/springframework/spring-context/5.3.7/spring-context-5.3.7.jar:/Users/lidean/.m2/repository/org/springframework/spring-expression/5.3.7/spring-expression-5.3.7.jar:/Users/lidean/.m2/repository/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar:/Users/lidean/.m2/repository/org/springframework/spring-core/5.3.7/spring-core-5.3.7.jar:/Users/lidean/.m2/repository/org/springframework/spring-jcl/5.3.7/spring-jcl-5.3.7.jar geektime.spring.hello.hellospring.HelloSpringApplication . ____ _ __ _ _ / / ___’_ __ _ _(_)_ __ __ _ ( ( )___ | ’_ | ’_| | ’_ / _` | / ___)| |_)| | | | | || (_| | ) ) ) ) ’ |____| .__|_| |_|_| |___, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot ::(v2.5.0)2021-06-01 23:15:57.484 INFO 81444 --- [ main] g.s.h.h.HelloSpringApplication : Starting HelloSpringApplication using Java 15.0.1 on MacBook-Pro-9.local with PID 81444 (/Users/lidean/Downloads/hello-spring/target/classes started by lidean in /Users/lidean/Downloads/hello-spring)2021-06-01 23:15:57.488 INFO 81444 --- [ main] g.s.h.h.HelloSpringApplication : No active profile set, falling back to default profiles: default2021-06-01 23:15:58.610 INFO 81444 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)2021-06-01 23:15:58.626 INFO 81444 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]2021-06-01 23:15:58.626 INFO 81444 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.46]2021-06-01 23:15:58.685 INFO 81444 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2021-06-01 23:15:58.685 INFO 81444 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1073 ms2021-06-01 23:15:59.156 INFO 81444 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path ’/actuator’2021-06-01 23:15:59.195 INFO 81444 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ’’2021-06-01 23:15:59.208 INFO 81444 --- [ main] g.s.h.h.HelloSpringApplication : Started HelloSpringApplication in 2.658 seconds (JVM running for 3.251)2021-06-01 23:15:59.254 INFO 81444 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state LivenessState changed to CORRECT2021-06-01 23:15:59.256 INFO 81444 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state ReadinessState changed to ACCEPTING_TRAFFIC2021-06-01 23:22:00.698 INFO 81444 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet ’dispatcherServlet’2021-06-01 23:22:00.698 INFO 81444 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet: Initializing Servlet ’dispatcherServlet’2021-06-01 23:22:00.699 INFO 81444 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet: Completed initialization in 1 ms

從上面輸出的信息我們可以知道Tomcat初始化部署在了8080端口,下面我們來訪問試試

// 調用我們定義的router$ curl http://127.0.0.1:8080/hello Hello Spring%// 記得我們之前生成項目使用的actuator依賴,用于檢查項目是否健康// 若出現DOWN,我們訪問url就會失敗$ curl http://127.0.0.1:8080/actuator/health{'status':'UP'}%

讓Maven替我們打個包

$ mvn clean packeage -Dmaven.test.skipzsh: command not found: mvn

沒命令。。。上鏈接

若有,可跳過

作者用的是macOs的zsh,需要做下面幾步

# 給文件夾權限$ sudo chown -R root:wheel Downloads/apache-maven*# 移動$ mv Downloads/apache-maven* /opt/apache-maven# 打開zsh配置文件$ nano ~/.zshrc# 新增命令,寫入~/.zshrc$ export PATH=$PATH:/opt/apache-maven/bin

搞定后control+X回車搞定~ 重啟下終端,檢查mvn版本即可

$ zsh# 檢查Maven$ mvn -versionApache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)Maven home: /opt/apache-mavenJava version: 15.0.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-15.0.1.jdk/Contents/HomeDefault locale: zh_CN_#Hans, platform encoding: UTF-8OS name: 'mac os x', version: '10.15.6', arch: 'x86_64', family: 'mac'

繼續繼續,maven打包我們的代碼

$ mvn clean package -Dmaven.test.skip [INFO] Scanning for projects...[INFO] [INFO] -----------------< geektime.spring.hello:hello-spring >-----------------[INFO] Building hello-spring 0.0.1-SNAPSHOT[INFO] --------------------------------[ jar ]---------------------------------[INFO] [INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ hello-spring ---[INFO] Deleting /Users/lidean/Downloads/hello-spring/target[INFO] [INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ hello-spring ---[INFO] Using ’UTF-8’ encoding to copy filtered resources.[INFO] Using ’UTF-8’ encoding to copy filtered properties files.[INFO] Copying 1 resource[INFO] Copying 0 resource[INFO] [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ hello-spring ---[INFO] Changes detected - recompiling the module![INFO] Compiling 1 source file to /Users/lidean/Downloads/hello-spring/target/classes[INFO] [INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ hello-spring ---[INFO] Not copying test resources[INFO] [INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ hello-spring ---[INFO] Not compiling test sources[INFO] [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ hello-spring ---[INFO] Tests are skipped.[INFO] [INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ hello-spring ---[INFO] Building jar: /Users/lidean/Downloads/hello-spring/target/hello-spring-0.0.1-SNAPSHOT.jar[INFO] [INFO] --- spring-boot-maven-plugin:2.5.0:repackage (repackage) @ hello-spring ---[INFO] Replacing main artifact with repackaged archive[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 2.965 s[INFO] Finished at: 2021-06-02T00:02:00+08:00[INFO] ------------------------------------------------------------------------

手把手教你怎么創建spring項目

可以看到生成的兩個.jar包,一個18M一個卻3.0K,因為前者有我們所有的依賴(該.jar包可執行),后者是原始的包

下面我們可以通過java -jar去執行這個jar包,可以看到也是暴露在8080端口,我們的程序可以作為一個單獨的進程運行在里面

手把手教你怎么創建spring項目

到此這篇關于手把手教你怎么寫spring項目的文章就介紹到這了,更多相關spring項目內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 精密钢管,冷拔精密无缝钢管,精密钢管厂,精密钢管制造厂家,精密钢管生产厂家,山东精密钢管厂家 | 河南mpp电力管_mpp电力管生产厂家_mpp电力电缆保护管价格 - 河南晨翀实业 | 乙炔气体报警装置|固定式氯化氢检测仪|河南驰诚电气百科 | 康明斯发电机,上柴柴油发电机,玉柴柴油发电机组_海南重康电力官网 | 过跨车_过跨电瓶车_过跨转运车_横移电动平车_厂区转运车_无轨转运车 | 深圳激光打标机_激光打标机_激光焊接机_激光切割机_同体激光打标机-深圳市创想激光科技有限公司 深圳快餐店设计-餐饮设计公司-餐饮空间品牌全案设计-深圳市勤蜂装饰工程 | 扒渣机,铁水扒渣机,钢水扒渣机,铁水捞渣机,钢水捞渣机-烟台盛利达工程技术有限公司 | 网优资讯-为循环资源、大宗商品、工业服务提供资讯与行情分析的数据服务平台 | 无锡市珂妮日用化妆品有限公司|珂妮日化官网|洗手液厂家 | 首页_欧瑞传动官方网站--主营变频器、伺服系统、新能源、软起动器、PLC、HMI | 高空重型升降平台_高空液压举升平台_高空作业平台_移动式升降机-河南华鹰机械设备有限公司 | 手板-手板模型-手板厂-手板加工-生产厂家,[东莞创域模型] | 吸音板,隔音板,吸音材料,吸音板价格,声学材料 - 佛山诺声吸音板厂家 | LHH药品稳定性试验箱-BPS系列恒温恒湿箱-意大利超低温冰箱-上海一恒科学仪器有限公司 | 陕西安闸机-伸缩门-车牌识别-广告道闸——捷申达门业科技 | 压片机_高速_单冲_双层_花篮式_多功能旋转压片机-上海天九压片机厂家 | 拉伸膜,PE缠绕膜,打包带,封箱胶带,包装膜厂家-东莞宏展包装 | 橡胶接头|可曲挠橡胶接头|橡胶软接头安装使用教程-上海松夏官方网站 | 裹包机|裹膜机|缠膜机|绕膜机-上海晏陵智能设备有限公司 | 不锈钢监控杆_监控立杆厂家-廊坊耀星光电科技有限公司 | 北京发电车出租-发电机租赁公司-柴油发电机厂家 - 北京明旺盛安机电设备有限公司 | 煤矿支护网片_矿用勾花菱形网_缝管式_管缝式锚杆-邯郸市永年区志涛工矿配件有限公司 | 除甲醛公司-甲醛检测-广西雅居环境科技有限公司 | 拼装地板,悬浮地板厂家,悬浮式拼装运动地板-石家庄博超地板科技有限公司 | 硫酸亚铁-聚合硫酸铁-除氟除磷剂-复合碳源-污水处理药剂厂家—长隆科技 | 扬子叉车厂家_升降平台_电动搬运车|堆高车-扬子仓储叉车官网 | 清管器,管道清管器,聚氨酯发泡球,清管球 - 承德嘉拓设备 | 挨踢网-大家的导航! | 不锈钢搅拌罐_高速搅拌罐厂家-无锡市凡格德化工装备科技有限公司 | 超声波成孔成槽质量检测仪-压浆机-桥梁预应力智能张拉设备-上海硕冠检测设备有限公司 | Dataforth隔离信号调理模块-信号放大模块-加速度振动传感器-北京康泰电子有限公司 | 阻燃剂-氢氧化镁-氢氧化铝-沥青阻燃剂-合肥皖燃新材料 | 柔性测斜仪_滑动测斜仪-广州杰芯科技有限公司 | 折弯机-刨槽机-数控折弯机-数控刨槽机-数控折弯机厂家-深圳豐科机械有限公司 | 手持式3d激光扫描仪-便携式三维立体扫描仪-北京福禄克斯 | 上海办公室设计_办公楼,写字楼装修_办公室装修公司-匠御设计 | 石油/泥浆/不锈钢防腐/砂泵/抽砂泵/砂砾泵/吸砂泵/压滤机泵 - 专业石油环保专用泵厂家 | 电液推杆生产厂家|电动推杆|液压推杆-扬州唯升机械有限公司 | 正压送风机-多叶送风口-板式排烟口-德州志诺通风设备 | 不锈钢水箱生产厂家_消防水箱生产厂家-河南联固供水设备有限公司 | 刹车盘机床-刹车盘生产线-龙口亨嘉智能装备|