中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

SpringBoot系列一:SpringBoot入門

2018-11-22    來(lái)源:importnew

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

1 SpringBoot HelloWorld

功能:瀏覽器發(fā)送 sayHello 請(qǐng)求,服務(wù)器接受請(qǐng)求并處理,響應(yīng) Hello。

1.1 創(chuàng)建一個(gè)maven工程

<groupId>com.seagetech</groupId>
<artifactId>springboot-helloworld</artifactId>
<version>1.0.0</version>

1.2 下載官方參考文檔

在官網(wǎng)找到相應(yīng)的下載地址(官網(wǎng)地址:https://spring.io/projects)或者直接使用如下地址下載:https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/pdf/spring-boot-reference.pdf。文檔名稱 “spring-boot-reference.pdf”,版本2.10.RELEASE。

1.3 使用官方參考文檔

Part II. Getting Started > 11. Developing Your First Spring Boot Application。

1.3.1 創(chuàng)建POM
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.0.RELEASE</version>
</parent>
1.3.2 添加 ClassPath 依賴
<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
</dependencies>

1.3.3 編寫代碼

創(chuàng)建一個(gè)主程序類,類名隨便取。因?yàn)槲覀兪亲鲆粋(gè) HelloWorld 的項(xiàng)目,所有我們創(chuàng)建一個(gè) HelloWorldApplication 類:

/**
 * @auther wangzb
 * @date 2018/11/9 21:32
 */
@Controller
@EnableAutoConfiguration
public class HelloWorldApplication {

    @RequestMapping(value = "/sayHello")
    @ResponseBody
    public String sayHello(String name){
        return "Hello," + name;
    }

    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.class, args);
    }
}
1.3.4 運(yùn)行例子

啟動(dòng) main 方法后,查看 SpringBoot 啟動(dòng)日志:

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.0.RELEASE)

....... . . .
....... . . . (log output here)
....... . . .  

... Tomcat started on port(s): 8080 (http) with context path ''
... Started HelloWorldApplication in 3.577 seconds (JVM running for 4.446)

可以看到Tomcat的啟動(dòng)日志:

Tomcat started on port(s): 8080 (http) with context path ”

即,Tomcat 默認(rèn)啟動(dòng)端口為 8080,項(xiàng)目根路徑為 “”,所以在瀏覽器或者 Postman 中輸入地址:http://localhost:8080/sayHello?name=wangzb,瀏覽器或Postman響應(yīng):

Hello,wangzb
1.3.5 創(chuàng)建一個(gè)可執(zhí)行的 jar 文件

要?jiǎng)?chuàng)建一個(gè)可執(zhí)行 jar,我們需要將 spring-boot-maven-plugin 添加到我們的 pom.xml 中。在dependencies部分下面插入以下幾行:

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>

接下來(lái)就可以打包,在 Idea 中點(diǎn)擊 package,執(zhí)行打包命令,之后我們?cè)陧?xiàng)目的 target 包下就可以看到剛打包好的 springboot-helloworld-1.0.0.jar 包。在包的路徑下,打開(kāi) CMD 命令行,執(zhí)行如下命令:

$ java -jar springboot-helloworld-1.0.0.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.0.RELEASE)
....... . . .
....... . . . (log output here)
....... . . .
........ Started Example in 2.536 seconds (JVM running for 2.864)

要退出應(yīng)用程序,請(qǐng)按 Ctrl+c 或者直接關(guān)閉命令窗口,再次在瀏覽器中訪問(wèn)上面地址,發(fā)現(xiàn)鏈接已不能再訪問(wèn),如果在 Linux 系統(tǒng)中,且要后臺(tái)長(zhǎng)期運(yùn)行,可以在命令后加&符號(hào),即:

java -jar springboot-helloworld-1.0.0.jar &

2 HelloWorld探究

2.1 使用IntelliJ IDEA快速創(chuàng)建SpringBoot項(xiàng)目

1) 新建項(xiàng)目

2) 左邊菜單選擇 Spring Initializr,右邊菜單選擇 JDK 版本,然后點(diǎn)擊Next

3) 填寫 Maven 坐標(biāo) GroupId/ArtifactId、打包形式(本例使用Jar的方式,war包形式請(qǐng)看后續(xù)文章)、項(xiàng)目名稱/版本等基本信息,然后點(diǎn)擊Next

4) 選擇 SpringBoot 版本,以及需要依賴的包,這里我們選擇最新版2.1.0,導(dǎo)入Web模塊依賴包,點(diǎn)擊Next

5) 修改項(xiàng)目名稱以及項(xiàng)目存放路徑,默認(rèn)即可,點(diǎn)擊 Finish

2.2 SpringBoot項(xiàng)目結(jié)構(gòu)分析

通過(guò) 2.1 的講解,我們熟悉了如何快速創(chuàng)建 SpringBoot 項(xiàng)目。接下來(lái)分析 SpringBoot 項(xiàng)目結(jié)構(gòu),上節(jié)中創(chuàng)建好的 SpringBoot 項(xiàng)目結(jié)構(gòu)如下:

2.2.1 pom.xml文件
2.2.1.1 父項(xiàng)目
<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.1.0.RELEASE</version>
   <relativePath/> <!-- lookup parent from repository -->
</parent>

按住Ctrl,點(diǎn)擊 spring-boot-starter-parent 進(jìn)入 spring-boot-starter-parent 項(xiàng)目,它的父項(xiàng)目是:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.1.0.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
</parent>

如上,可以看到 spring-boot-dependencies。它是真正管理 SpringBoot 項(xiàng)目所有依賴版本,是 Spring Boot 的版本仲裁中心,以后我們導(dǎo)入依賴默認(rèn)是不需要寫版本(沒(méi)有在 dependencies 里面管理的依賴自然需要聲明版本號(hào))。

2.2.1.2 啟動(dòng)器(Starter)
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

spring-boot-starter:spring-boot 場(chǎng)景啟動(dòng)器。幫我們導(dǎo)入了 Web 模塊正常運(yùn)行所依賴的組件。Spring Boot 將所有的功能場(chǎng)景都抽取出來(lái),做成許多啟動(dòng)器(starter),只需要在項(xiàng)目里面引入這些啟動(dòng)器,相關(guān)場(chǎng)景的所有依賴都會(huì)導(dǎo)入進(jìn)來(lái)。要用什么功能就導(dǎo)入什么場(chǎng)景的啟動(dòng)器。

1) 以下列舉 SpringBoot 官方提供的 starter pom

更多請(qǐng)參考 “spring-boot-reference.pdf” 2.1.0.RELEASE,13.5節(jié)Starters。

2) 除了官方的 starter pom 外,還有第三方為 SpringBoot 所寫的 starter pom

如下表所示:

2.2.2 resources文件目錄結(jié)構(gòu)
  1. static:保存所有的靜態(tài)資源,如 js、css、images 等。
  2. templates:保存所有的模板文件,SpringBoot 默認(rèn) jar 使用嵌入式的 Tomcat,默認(rèn)不支持 JSP 頁(yè)面,可以使用模板引擎(Thymeleaf、Freemarker )。
  3. application.properties:SpringBoot 應(yīng)用配置文件,可以修改一些默認(rèn)的配置,如修改 Tomcat 端口,項(xiàng)目根路徑等。
2.2.3 主程序類

打開(kāi)2.1節(jié)創(chuàng)建的 springboot-demo 項(xiàng)目,找到 SpringBootDemoApplication 類:

package com.seagetech.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);
   }
}

基本和1.3.3節(jié)創(chuàng)建的主程序類一樣,main 方法加注解的形式,不同的是,Idea 創(chuàng)建的主程序類使用的是 @SpringBootApplication 注解,它是 SpringBoot 的核心注解,也是一個(gè)組合注解,打開(kāi)這個(gè)注解,源碼如下:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan

@SpringBootApplication 注解的核心功能其實(shí)是:@SpringBootConfiguration、@EnableAutoConfigration、@ComponentScan 三個(gè)注解提供。有關(guān)這幾個(gè)注解的詳解,請(qǐng)閱讀?Spring Boot 系列二:SpringBoot自動(dòng)配置原理。

標(biāo)簽: linux 代碼 服務(wù)器

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:談?wù)?Java 類加載機(jī)制

下一篇:讓SpringBoot啟動(dòng)更快一點(diǎn)