SprintBoot簡單入門


1、什么是SpringBoot

SpringBoot是基於Spring的基礎上提供了一套全新的框架,其目的是為了在開發時簡化Spring的相關配置及開發過程。在SpringBoot未出來之前,准備搭建一個Spring的開發環境需要配置一堆的XML文件,而SpringBoot就是去除了大量的XML配置文件,簡化了復雜的依賴管理。

Spring Boot集成了大量常用的第三方庫配置,Spring Boot 應用中這些第三方庫幾乎可以是零配置的開箱即用(out-of-the-box),大部分的Spring Boot應用都只需要非常少量的配置代碼(基於Java 的配置),開發者能夠更加專注於業務邏輯。

2、SpringBoot特征

  • 獨立運行的Spring項目,使用jar包的形式獨立運行,只需通過命令java -jar xx.jar即可運行。
  • 內嵌Servlet容器(例如TomcatJetty或者Undertow 等),應用無需打成WAR包 。
  • 提供starter簡化Maven配置,提供了一系列的starter項目對象模型(POMS)來簡化 Maven配置。
  • 提供了大量的默認自動配置,來簡化項目的開發,開發人員也通過配置文件修改默認配置。
  • 自帶應用監控(如指標、健康檢查和外部化配置)。
  • 沒有代碼生成和XML配置。

3、快速搭建SpringBoot

  • 引入maven依賴
    首先引入parent依賴
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.3</version>
</parent>

因為是要開發一個web項目,因此需要引入web依賴。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • 創建啟動類
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}
  • 創建controller
@RestController
@RequestMapping(path = "/v1/index")
public class IndexController {
    @GetMapping(path = "")
    public String index(){
        return "Hello Spring Boot!";
    }
}
  • 啟動項目
Connected to the target VM, address: '127.0.0.1:54766', transport: 'socket'

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

2021-08-04 23:16:55.694  INFO 15528 --- [           main] com.tenghu.sb.Application                : Starting Application using Java 1.8.0_301 on Arvin with PID 15528 (E:\project\java\spring-boot-study\target\classes started by admin in E:\project\java\spring-boot-study)
2021-08-04 23:16:55.696  INFO 15528 --- [           main] com.tenghu.sb.Application                : No active profile set, falling back to default profiles: default
2021-08-04 23:16:56.231  INFO 15528 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-08-04 23:16:56.237  INFO 15528 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-08-04 23:16:56.237  INFO 15528 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.50]
2021-08-04 23:16:56.292  INFO 15528 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-08-04 23:16:56.292  INFO 15528 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 567 ms
2021-08-04 23:16:56.503  INFO 15528 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-08-04 23:16:56.509  INFO 15528 --- [           main] com.tenghu.sb.Application                : Started Application in 1.065 seconds (JVM running for 1.607)
2021-08-04 23:17:07.919  INFO 15528 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-08-04 23:17:07.919  INFO 15528 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-08-04 23:17:07.920  INFO 15528 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms

看到這個結果,表示SpringBoot項目就已經啟動成功了,現在使用瀏覽器訪問路徑http://localhost:8080/v1/index就可以看到瀏覽器輸出結果:Hello Spring Boot!

以上是手動建立一個空的maven項目,手動添加依賴的方式創建一個簡單的spring boot項目,另外還有兩種方式可以快速生成spring boot項目:

  • 使用IDEA,里面已經集成了SpringBoot插件
  • 使用官網提供的創建SpringBoot插件。https://start.spring.io/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM