SpringBoot系列一:入門


SpringBoot提供了一種新的編程範式,能在最小的阻力下開發Spring應用程序,有了它你可以敏捷地開發Spring應用程序,專注於應用程序的功能,不用在Spring的配置上多花功夫,甚至完全不用配置。
在這裏插入圖片描述


SpringBoot精要

Spring Boot將很多魔法帶入了Spring應用程序的開發之中,其中最重要的是以下四個核心。

自動配置

針對很多Spring應用程序常見的應用功能,Spring Boot能自動提供相關配置。 Spring Boot會爲常見配置場景進行自動配置。它有大把的辦法幫你減輕配置負擔這些自動配置涉及Java持久化API(Java Persistence API,JPA)、Thymeleaf模板、安全和Spring MVC等。

起步依賴

告訴Spring Boot需要什麼功能,它就能引入需要的庫。 如果打算利用Spring Boot的起步依賴,你只需添加Spring Boot的Web起步依賴( org.springframework.boot:spring-boot-starter-web ),僅此一個。它會根據依賴傳遞把其他所需依賴引入項目裏。簡而言之,你不再需要考慮支持某種功能要用什麼庫了,引入相關起步依賴就行。此外,Spring Boot的起步依賴還把你從“需要這些庫的哪些版本”這個問題裏解放了出來。起步依賴引入的庫的版本都是經過測試的,因此你可以完全放心,它們之間不會出現不兼容的情況。

命令行界面

這是Spring Boot的可選特性,藉此你只需寫代碼就能完成完整的應用程序,無需傳統項目構建。 Spring Boot CLI讓只寫代碼即可實現應用程序成爲可能,Spring Boot CLI利用了起步依賴和自動配置,讓你專注於代碼本身。不過Spring Boot CLI是Spring Boot的非必要組成部分。雖然它爲Spring帶來了驚人的力量,大大簡化了開發,但也引入了一套不太常規的開發模型。

Actuator

讓你能夠深入運行中的Spring Boot應用程序,一探究竟。 Actuator要提供在運行時檢視應用程序內部情況的能力。安裝了Actuator就能窺探應用程序的內部情況了,包括如下細節:

  • Spring應用程序上下文裏配置的Bean
  • Spring Boot的自動配置做的決策
  • 應用程序取到的環境變量、系統屬性、配置屬性和命令行參數
  • 應用程序裏線程的當前狀態
  • 應用程序最近處理過的HTTP請求的追蹤情況
  • 各種和內存用量、垃圾回收、Web請求以及數據源用量相關的指標

SpringBoot並不是什麼

Spring Boot可以把Web應用程序變爲可自執行的JAR文件,不用部署到傳統Java應用服務器裏就能在命令行裏運行。Spring Boot在應用程序裏嵌入了一個Servlet容(Tomcat、Jetty或Undertow),以此實現這一功能。但這是內嵌的Servlet容器提供的功能,不是Spring Boot實現的。簡而言之,從本質上來說,Spring Boot就是Spring,它做了那些沒有它你自己也會去做的SpringBean配置。


SpringBoot輸出HelloWorld

通過Spring Initializr創建SpringBoot項目後,項目的結構如下圖所示:
在這裏插入圖片描述
項目結構中各個文件夾以及文件的作用如下圖所示:
在這裏插入圖片描述
在main中的java文件夾下創建Controller包,在包中創建Controller類,代碼如下:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("test")
public class HelloWorldController {
    @GetMapping("hello")
    public String sayHello() {
        return "Hello World";
    }
}

運行DemoAppication類,控制檯輸出如下:

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

2020-06-03 10:02:00.528  INFO 9544 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication on X-709-PC with PID 9544 (E:\IDEA\SpringBoot\demo1\target\classes started by X-709 in E:\IDEA\SpringBoot)
2020-06-03 10:02:00.530  INFO 9544 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2020-06-03 10:02:01.319  INFO 9544 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-06-03 10:02:01.326  INFO 9544 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-06-03 10:02:01.326  INFO 9544 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.35]
2020-06-03 10:02:01.405  INFO 9544 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-06-03 10:02:01.405  INFO 9544 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 843 ms
2020-06-03 10:02:01.544  INFO 9544 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-06-03 10:02:01.670  INFO 9544 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-06-03 10:02:01.677  INFO 9544 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 1.441 seconds (JVM running for 1.986)

在瀏覽器中輸入http://localhost:8080/test/hello,結果如下圖所示:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章