一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - springboot學(xué)習(xí)之構(gòu)建簡(jiǎn)單項(xiàng)目搭建步驟詳解

springboot學(xué)習(xí)之構(gòu)建簡(jiǎn)單項(xiàng)目搭建步驟詳解

2021-06-08 14:59廚房小碼農(nóng) Java教程

這篇文章主要介紹了springboot學(xué)習(xí)之構(gòu)建簡(jiǎn)單項(xiàng)目搭建步驟詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

概述

相信對(duì)于java開發(fā)者而言,spring和springmvc兩個(gè)框架一定不陌生,這兩個(gè)框架需要我們手動(dòng)配置的地方非常多,各種的xml文件,properties文件,構(gòu)建一個(gè)項(xiàng)目還是挺復(fù)雜的,在這種情況下,springboot應(yīng)運(yùn)而生,他能夠快速的構(gòu)建spring項(xiàng)目,而且讓項(xiàng)目正常運(yùn)行起來的配置文件非常少,甚至只需要幾個(gè)注解就可以運(yùn)行整個(gè)項(xiàng)目。

總的說來,springboot項(xiàng)目可以打成jar包獨(dú)立運(yùn)行部署,因?yàn)樗鼉?nèi)嵌servlet容器,之前spring,springmvc需要的大量依賴,可以通過starter來幫助我們簡(jiǎn)化配置,當(dāng)然還有其他好多優(yōu)點(diǎn),這里就不一一贅述,小伙伴們可以自行搜索解答。

簡(jiǎn)單項(xiàng)目構(gòu)建

工具

eclipse maven

首先,我們新建一個(gè)maven項(xiàng)目,在eclipse左側(cè)右擊選擇new----》other,選擇新建maven project

springboot學(xué)習(xí)之構(gòu)建簡(jiǎn)單項(xiàng)目搭建步驟詳解

輸入group id,artifact id,點(diǎn)擊完成

springboot學(xué)習(xí)之構(gòu)建簡(jiǎn)單項(xiàng)目搭建步驟詳解

這樣一個(gè)簡(jiǎn)單的項(xiàng)目架子就完成了,但是啥都沒有,項(xiàng)目結(jié)構(gòu)如下圖所示:

springboot學(xué)習(xí)之構(gòu)建簡(jiǎn)單項(xiàng)目搭建步驟詳解

下面我們就開始配置搭建springboot項(xiàng)目。

1.添加依賴

springboot學(xué)習(xí)之構(gòu)建簡(jiǎn)單項(xiàng)目搭建步驟詳解

完整porm代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<project xmlns="http://maven.apache.org/pom/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
  xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>
 
  <groupid>com.cfxmn.springboot</groupid>
  <artifactid>springbootdemo</artifactid>
  <version>0.0.1-snapshot</version>
  <packaging>jar</packaging>
 
  <!-- 通過繼承spring-boot-starter-parent項(xiàng)目來獲得一些合理的默認(rèn)配置 -->
  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>1.5.6.release</version>
  </parent>
 
  <properties>
    <project.build.sourceencoding>utf-8</project.build.sourceencoding>
  </properties>
 
  <dependencies>
    <!-- spring boot web 依賴 -->
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
    </dependency>
 
    <!-- spring boot test 依賴 -->
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-test</artifactid>
      <scope>test</scope>
    </dependency>
    <!-- 使用lombok可以減少很多重復(fù)代碼的書寫。比如說getter/setter/tostring等方法的編寫 -->
    <dependency>
      <groupid>org.projectlombok</groupid>
      <artifactid>lombok</artifactid>
    </dependency>
  </dependencies>
</project>

下面我們新建一些包和添加項(xiàng)目的啟動(dòng)類,如下圖所示:

springboot學(xué)習(xí)之構(gòu)建簡(jiǎn)單項(xiàng)目搭建步驟詳解

其中,控制器democontroller的內(nèi)容非常簡(jiǎn)單,內(nèi)容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.cfxmn.springboot.springbootdemo.controller;
import org.springframework.web.bind.annotation.postmapping;
import org.springframework.web.bind.annotation.restcontroller;
import lombok.extern.slf4j.slf4j;
@restcontroller
@slf4j
public class democontroller {
  @postmapping("/demo")
 
  public void demotest() {
 
    // 這邊簡(jiǎn)單起見,打印一下日志
 
    log.info("success call");
 
  }
 
}

可能有些同學(xué)對(duì)其中的幾個(gè)注解有些疑問,我這邊簡(jiǎn)單說明下,

1.restcontroller

這個(gè)注解其實(shí)就是@responsebody + @controller

2.postmapping

這個(gè)注解其實(shí)就是@requestmapping("xxxxxx", method=requestmethod.post)

這兩個(gè)其實(shí)都是組合注解,簡(jiǎn)化使用

我們?cè)賮砜纯?,?xiàng)目的啟動(dòng)類springbootdemoapplication的內(nèi)容:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.cfxmn.springboot.springbootdemo;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
@springbootapplication
 
public class springbootdemoapplication {
 
  public static void main(string[] args) {
 
    springapplication.run(springbootdemoapplication.class, args);
 
  }
}

是的,你沒看錯(cuò),只要運(yùn)行這個(gè)main方法,就能啟動(dòng)這個(gè)spring項(xiàng)目,具體是怎么啟動(dòng)的容器,我們之后再分析,其實(shí)主要就是在注解springbootapplication上。

下面我們就來運(yùn)行下,看下啟動(dòng)日志:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
.  ____     _      __ _ _
 
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
 
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 
 \\/ ___)| |_)| | | | | || (_| | ) ) ) )
 
 ' |____| .__|_| |_|_| |_\__, | / / / /
 
 =========|_|==============|___/=/_/_/_/
 
 :: spring boot ::    (v1.5.6.release)
 
 
 
2018-10-25 23:52:41.985 info 1700 --- [      main] c.c.s.s.springbootdemoapplication    : starting springbootdemoapplication on desktop-kb78hjk with pid 1700 (e:\workspace\springbootdemo\target\classes started by gepengfa in e:\workspace\springbootdemo)
 
2018-10-25 23:52:41.990 info 1700 --- [      main] c.c.s.s.springbootdemoapplication    : no active profile set, falling back to default profiles: default
 
2018-10-25 23:52:42.088 info 1700 --- [      main] ationconfigembeddedwebapplicationcontext : refreshing org.springframework.boot.context.embedded.annotationconfigembeddedwebapplicationcontext@7f416310: startup date [thu oct 25 23:52:42 cst 2018]; root of context hierarchy
 
2018-10-25 23:52:44.561 info 1700 --- [      main] s.b.c.e.t.tomcatembeddedservletcontainer : tomcat initialized with port(s): 8080 (http)
 
2018-10-25 23:52:44.584 info 1700 --- [      main] o.apache.catalina.core.standardservice  : starting service [tomcat]
 
2018-10-25 23:52:44.588 info 1700 --- [      main] org.apache.catalina.core.standardengine : starting servlet engine: apache tomcat/8.5.16
 
2018-10-25 23:52:44.813 info 1700 --- [ost-startstop-1] o.a.c.c.c.[tomcat].[localhost].[/]    : initializing spring embedded webapplicationcontext
 
2018-10-25 23:52:44.813 info 1700 --- [ost-startstop-1] o.s.web.context.contextloader      : root webapplicationcontext: initialization completed in 2733 ms
 
2018-10-25 23:52:45.074 info 1700 --- [ost-startstop-1] o.s.b.w.servlet.servletregistrationbean : mapping servlet: 'dispatcherservlet' to [/]
 
2018-10-25 23:52:45.083 info 1700 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean  : mapping filter: 'characterencodingfilter' to: [/*]
 
2018-10-25 23:52:45.083 info 1700 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean  : mapping filter: 'hiddenhttpmethodfilter' to: [/*]
 
2018-10-25 23:52:45.083 info 1700 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean  : mapping filter: 'httpputformcontentfilter' to: [/*]
 
2018-10-25 23:52:45.085 info 1700 --- [ost-startstop-1] o.s.b.w.servlet.filterregistrationbean  : mapping filter: 'requestcontextfilter' to: [/*]
 
2018-10-25 23:52:45.582 info 1700 --- [      main] s.w.s.m.m.a.requestmappinghandleradapter : looking for @controlleradvice: org.springframework.boot.context.embedded.annotationconfigembeddedwebapplicationcontext@7f416310: startup date [thu oct 25 23:52:42 cst 2018]; root of context hierarchy
 
2018-10-25 23:52:45.705 info 1700 --- [      main] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/demo],methods=[post]}" onto public void com.cfxmn.springboot.springbootdemo.controller.democontroller.demotest()
 
2018-10-25 23:52:45.710 info 1700 --- [      main] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/error]}" onto public org.springframework.http.responseentity<java.util.map<java.lang.string, java.lang.object>> org.springframework.boot.autoconfigure.web.basicerrorcontroller.error(javax.servlet.http.httpservletrequest)
 
2018-10-25 23:52:45.711 info 1700 --- [      main] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.modelandview org.springframework.boot.autoconfigure.web.basicerrorcontroller.errorhtml(javax.servlet.http.httpservletrequest,javax.servlet.http.httpservletresponse)
 
2018-10-25 23:52:45.759 info 1700 --- [      main] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler]
 
2018-10-25 23:52:45.759 info 1700 --- [      main] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/**] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler]
 
2018-10-25 23:52:45.817 info 1700 --- [      main] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler]
 
2018-10-25 23:52:46.321 info 1700 --- [      main] o.s.j.e.a.annotationmbeanexporter    : registering beans for jmx exposure on startup
 
2018-10-25 23:52:46.529 info 1700 --- [      main] s.b.c.e.t.tomcatembeddedservletcontainer : tomcat started on port(s): 8080 (http)
 
2018-10-25 23:52:46.599 info 1700 --- [      main] c.c.s.s.springbootdemoapplication    : started springbootdemoapplication in 5.092 seconds (jvm running for 5.764)

從啟動(dòng)日志標(biāo)黃的部分可以看出,項(xiàng)目啟動(dòng)成功了,訪問端口默認(rèn)是8080(這個(gè)端口是可以改動(dòng)的)

下面我們通過postman請(qǐng)求下,

springboot學(xué)習(xí)之構(gòu)建簡(jiǎn)單項(xiàng)目搭建步驟詳解

查看控制臺(tái)

?
1
2018-10-25 23:59:26.385 info 1700 --- [nio-8080-exec-2] c.c.s.s.controller.democontroller    : success call

說明調(diào)用成功。

到此,一個(gè)簡(jiǎn)單的springboot項(xiàng)目就構(gòu)建完成了,但這只是一個(gè)空的架子,內(nèi)容還可載豐富。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.cnblogs.com/xiaobaobei/p/9853712.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美成人精品福利网站 | 九九99热久久精品在线6 | 国产拍拍拍免费专区在线观看 | 国产精品免费网站 | 秀婷程仪公欲息肉婷在线观看 | 久久成人精品免费播放 | 精品在线免费观看视频 | 青草热视频 | babes性欧美30 | 高清在线免费观看 | 免费超级乱淫视频播放性 | 青青青草免费 | 精品综合久久久久久8888 | 臀控福利大臀的网站 | 久久国产综合精品欧美 | 男插女的下面免费视频夜色 | 国产黄色大片网站 | 亚洲29p| 热99re久久精品国产 | ady@ady9.映画网 | 小SAO货叫大声点妓女 | 朝鲜女人free性hu | 亚洲成年 | 日韩成人在线网站 | 四虎成人4hutv影院 | 国产精品视频第一区二区 | 全黄h全肉细节文在线观看 全彩成人18h漫画 | 2022最新国产在线 | 亚1洲二区三区四区免费 | 日韩色图区 | 国产乱子伦一区二区三区 | 青草青草视频 | 精品一区二区三区在线视频观看 | 网站色小妹 | 小鸟酱视频在线观看 | 亚洲视频在线看 | 国产性视频 | 香港三级血恋3 | 欧美灰丝袜丝交nylons | 美女扒开尿口让男生添 漫画 | 黑人巨大videosjapan高清 黑人好大 |