最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

手把手教你怎么創(chuàng)建spring項(xiàng)目

 更新時(shí)間:2021年06月04日 16:29:58   作者:變優(yōu)秀的小白  
今天教大家怎么寫(xiě)spring項(xiàng)目,文中有非常詳細(xì)的圖文示例及介紹,對(duì)正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下

創(chuàng)建Spring項(xiàng)目

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

image.png

下載好會(huì)得到一個(gè).zip文件,解壓導(dǎo)入IDEA就可以開(kāi)始了,這是基礎(chǔ)的項(xiàng)目結(jié)構(gòu)

image.png

讓我們打開(kāi)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
@RestController
public class HelloSpringApplication {

	public static void main(String[] args) {
		SpringApplication.run(HelloSpringApplication.class, args);
	}

	@RequestMapping("/hello")
	public String hello() {
		return "Hello Spring";
	}
}

接著運(yùn)行我們修改的代碼,運(yùn)行成功后,console會(huì)打印如下信息

/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: default
2021-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 WebApplicationContext
2021-06-01 23:15:58.685  INFO 81444 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1073 ms
2021-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 CORRECT
2021-06-01 23:15:59.256  INFO 81444 --- [           main] o.s.b.a.ApplicationAvailabilityBean      : Application availability state ReadinessState changed to ACCEPTING_TRAFFIC
2021-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端口,下面我們來(lái)訪問(wèn)試試

// 調(diào)用我們定義的router
$ curl http://127.0.0.1:8080/hello                            
Hello Spring%
// 記得我們之前生成項(xiàng)目使用的actuator依賴,用于檢查項(xiàng)目是否健康
// 若出現(xiàn)DOWN,我們?cè)L問(wèn)url就會(huì)失敗
$ curl http://127.0.0.1:8080/actuator/health
{"status":"UP"}%        

Maven替我們打個(gè)包

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

沒(méi)命令。。。上鏈接

若有,可跳過(guò)

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

# 給文件夾權(quán)限
$ sudo chown -R root:wheel Downloads/apache-maven*
# 移動(dòng)
$ mv Downloads/apache-maven* /opt/apache-maven
# 打開(kāi)zsh配置文件
$ nano ~/.zshrc
# 新增命令,寫(xiě)入~/.zshrc
$ export PATH=$PATH:/opt/apache-maven/bin

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

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

繼續(xù)繼續(xù),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] ------------------------------------------------------------------------

image.png

可以看到生成的兩個(gè).jar包,一個(gè)18M一個(gè)卻3.0K,因?yàn)榍罢哂形覀兯械囊蕾?該.jar包可執(zhí)行),后者是原始的包

下面我們可以通過(guò)java -jar去執(zhí)行這個(gè)jar包,可以看到也是暴露在8080端口,我們的程序可以作為一個(gè)單獨(dú)的進(jìn)程運(yùn)行在里面

image.png

到此這篇關(guān)于手把手教你怎么寫(xiě)spring項(xiàng)目的文章就介紹到這了,更多相關(guān)spring項(xiàng)目?jī)?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JAVA-NIO之Socket/ServerSocket Channel(詳解)

    JAVA-NIO之Socket/ServerSocket Channel(詳解)

    下面小編就為大家?guī)?lái)一篇JAVA-NIO之Socket/ServerSocket Channel(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • mybatis-plus如何配置自定義數(shù)據(jù)類型TypeHandle

    mybatis-plus如何配置自定義數(shù)據(jù)類型TypeHandle

    這篇文章主要介紹了mybatis-plus如何配置自定義數(shù)據(jù)類型TypeHandle,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Spring?MVC和springboot靜態(tài)資源處理問(wèn)題

    Spring?MVC和springboot靜態(tài)資源處理問(wèn)題

    這篇文章主要介紹了Spring?MVC和springboot靜態(tài)資源處理問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • java實(shí)現(xiàn)從網(wǎng)上下載圖片到本地的方法

    java實(shí)現(xiàn)從網(wǎng)上下載圖片到本地的方法

    這篇文章主要介紹了java實(shí)現(xiàn)從網(wǎng)上下載圖片到本地的方法,涉及java針對(duì)文件操作的相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2015-07-07
  • 在SSM框架中將圖片上傳到數(shù)據(jù)庫(kù)中的實(shí)現(xiàn)代碼

    在SSM框架中將圖片上傳到數(shù)據(jù)庫(kù)中的實(shí)現(xiàn)代碼

    這篇文章主要介紹了在SSM框架中將圖片上傳到數(shù)據(jù)庫(kù)中的實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • java加密MD5實(shí)現(xiàn)及密碼驗(yàn)證代碼實(shí)例

    java加密MD5實(shí)現(xiàn)及密碼驗(yàn)證代碼實(shí)例

    這篇文章主要介紹了java加密MD5實(shí)現(xiàn)及密碼驗(yàn)證代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • 關(guān)于Java從本地文件復(fù)制到網(wǎng)絡(luò)文件上傳

    關(guān)于Java從本地文件復(fù)制到網(wǎng)絡(luò)文件上傳

    這篇文章主要介紹了關(guān)于Java從本地文件復(fù)制到網(wǎng)絡(luò)文件上傳,File?和?IO?流其實(shí)是很相似的,都是將文件從一個(gè)地方轉(zhuǎn)移到另一個(gè)地方,這也是流的特點(diǎn)之一,需要的朋友可以參考下
    2023-04-04
  • java參數(shù)傳遞之值傳遞和引用傳遞

    java參數(shù)傳遞之值傳遞和引用傳遞

    這篇文章主要介紹了java參數(shù)傳遞之值傳遞和引用傳遞,引用了兩個(gè)代碼實(shí)例來(lái)講解,有感興趣的同學(xué)可以研究下
    2021-02-02
  • centos7安裝java的多種方式總結(jié)

    centos7安裝java的多種方式總結(jié)

    這篇文章主要給大家介紹了關(guān)于centos7安裝java的多種方式,文中通過(guò)實(shí)例代碼以及圖文介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2023-01-01
  • 如何使用Java實(shí)現(xiàn)指定概率的抽獎(jiǎng)

    如何使用Java實(shí)現(xiàn)指定概率的抽獎(jiǎng)

    這篇文章主要給大家介紹了關(guān)于如何使用Java實(shí)現(xiàn)指定概率的抽獎(jiǎng)的相關(guān)資料,Java抽獎(jiǎng)程序的基本原理是通過(guò)隨機(jī)數(shù)生成器來(lái)實(shí)現(xiàn)隨機(jī)抽獎(jiǎng)的功能,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07

最新評(píng)論

浦县| 开化县| 化隆| 伊春市| 开鲁县| 加查县| 大宁县| 志丹县| 桂平市| 香格里拉县| 邳州市| 陆良县| 拉孜县| 宁南县| 英吉沙县| 新建县| 永丰县| 乌什县| 清水河县| 成武县| 潮州市| 施秉县| 岳普湖县| 金溪县| 全椒县| 上饶市| 突泉县| 五大连池市| 福海县| 壤塘县| 金川县| 来安县| 常熟市| 堆龙德庆县| 达日县| 古丈县| 五莲县| 呼图壁县| 宣汉县| 和顺县| 辰溪县|