新手必看!SpringBoot 系列之從 0 搭建項目

 


前言

使用SpringBoot已經也有兩年多了,從一開始對SpringBoot的零認知到如今平常開發必接觸的框架,說實話受益良多,其實SpringBoot就是Spring的擴展,之前咱們作框架整合以及開發過程當中會有大量的配置文件須要配置,而SpringBoot的出現就是把咱們從大量配置文件xml中解救出來,再也不須要作過多bean配置、DI配置,使用SpringBoot以後只須要集中在application配置文件中作簡單屬性配置便可,因爲SpringBoot內嵌了Tomcat這樣還免去了咱們安裝Tomcat的麻煩,咱們只須要運行項目根目錄下啓動類的main方法便可啓動項目,是否是對比以往的項目有沒有感受牛逼plus,今天先說到這,接下來咱們學習如何從零搭建SpringBoot項目。java


提示:如下是本篇文章正文內容,下面案例可供參考web

1、開發工具安裝,環境安裝準備工做

  1. 開發工具:Eclipse/IntelliJ IDEA(我用的IDEA)
    開發工具可自行去官網下載
  2. JAVA環境:JDK(我用的1.8版本)
    JDK自行去官網下載, window環境變量配置教程.
  3. Jar管理:Maven(我用的IDEA插件maven3)
    也可自行安裝maven在開發工具setting中進行配置。

2、開發工具安裝Spring幫助插件

1.Eclipse安裝Spring Tools4插件

因爲我使用的是IntelliJ IDEA開發工具,這裏我就不作詳細介紹了。spring

2.IntelliJ IDEA安裝Spring Assistant插件

  1. 打開IDEA,單擊菜單欄中的「File->Setting->plugins」打開插件窗口。apache

  2. 在插件窗口搜索「spring」或「Spring Assistant」回車,找到以下圖Install安裝。(我已經安裝過了,因此是按鈕是Uninstall)
    在這裏插入圖片描述
    瀏覽器

  3. 重啓IDEA生效。tomcat

3、搭建SpringBoot項目工程

1.使用IDEA中的插件「Spring Assistant」建立項目。

①菜單欄「file->new->project」打開建立項目窗口。
在這裏插入圖片描述
②默認next
在這裏插入圖片描述
③根據本身須要更改,next。
在這裏插入圖片描述
④本次就建立一個web項目因此咱們勾選spring web ,而後next。
在這裏插入圖片描述
springboot

⑤Finish。
在這裏插入圖片描述
app

2.項目結構。

①下圖是使用插件「Spring Assistant」幫咱們生成的項目。
在這裏插入圖片描述
框架

②從①圖中能夠看出來建立SpringBoot項目仍是很簡單的。maven

  1. maven項目pom.xml加入springboot依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
  1. 建立啓動類DemoApplication.java,注意啓動類必定要建立在代碼根目錄哦,否則後續加入其餘代碼會啓動報錯。
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
  1. application配置文件,此文件能夠放項目使用的一些屬性。
    更改項目啓動端口,項目全局路徑,等等均可以。
  2. 咱們能夠建立一個Controller控制器,加上@RestController註解,在控制器中加一個test方法,方法加上@GetMapping("/test")
package com.example.demo.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DemoController { @GetMapping("/test") public String test() { return "Hello World"; } }
  1. 項目都建立好了,啓動看看實際效果,DemoApplication.java類右鍵而後Run 'DemoApplication’或Debug 'DemoApplication’啓動。
    在這裏插入圖片描述
    項目默認啓動端口是8080,也可在application配置文件自定義,啓動日誌:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.4.RELEASE) 2020-10-24 14:19:35.103 INFO 109976 --- [ restartedMain] com.example.demo.DemoApplication : Starting DemoApplication on wb_lichao001 with PID 109976 2020-10-24 14:19:35.108 INFO 109976 --- [ restartedMain] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default 2020-10-24 14:19:35.236 INFO 109976 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable 2020-10-24 14:19:35.236 INFO 109976 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG' 2020-10-24 14:19:37.408 INFO 109976 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2020-10-24 14:19:37.426 INFO 109976 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2020-10-24 14:19:37.427 INFO 109976 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.38] 2020-10-24 14:19:37.579 INFO 109976 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-10-24 14:19:37.580 INFO 109976 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2342 ms 2020-10-24 14:19:37.907 INFO 109976 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2020-10-24 14:19:38.088 INFO 109976 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729 2020-10-24 14:19:38.145 INFO 109976 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2020-10-24 14:19:38.164 INFO 109976 --- [ restartedMain] com.example.demo.DemoApplication : Started DemoApplication in 4.024 seconds (JVM running for 6.96)
  1. 打開瀏覽器,訪問http://localhost:8080/test,成功返回Hello World。
    在這裏插入圖片描述

總結

今天建立是使用插件的方式來幫咱們建立SpringBoot項目,其實咱們能夠先建立一個maven項目,而後pom.xml加入依賴,建立Application.java啓動類(加@SpringBootApplication註解,main方法),新增application配置文件,最終也能建立出一個最簡單的springboot項目,今天分享就到這裏,但願可以幫助到你們,記得收藏點贊哦!!!有補充/須要均可評論留言,大家的支持就是博主的動力!!!

更多好文敬請關注公衆號:main方法

相關文章
相關標籤/搜索