1. Spring Boot 的由來
大家都知道,Spring框架是Java生態中舉足輕重的輕量型框架,幫助我們廣大的大佬們進行Java開發,Spring框架的發展非常的迅速,從最開始的Spring核心容器幫助我們管理Java物件,到后來各種業務上的問題,Spring框架幾乎都有與之對應的解決方案,但隨著越來越多的業務集成到Spring框架中,Spring與各組件的組態檔越來越多,配置越來越繁雜,開發人員不能很好的專注業務邏輯,給開發人員帶來了困擾,所以說,Spring框架本身是輕量型的,但是Spring的配置卻是重量型的,Spring的開發者早早的就注意到了這個問題,為了解決這個問題,Spring Boot 應運而生,Spring Boot 的核心理念是約定優于配置,即:Java開發者不需要關心各個jar包之間的依賴關系,依賴關系由Spring開發者們提前幫你們配置好了,并打成jar包,Java開發者只需要引入jar就可以快速開發,極大的提高了Java開發者的效率,而且組態檔也只剩下了一個,在Spring Boot 出現之前,開發一個專案的的專案搭建作業可能需要30分鐘左右,Spring Boot 出現之后,5分鐘都不要,開發者們就可以進行代碼的撰寫,極大的提高了開發者的效率,也為Spring 框架注入了新的生命力,
2. Spring Boot 的 hello world
Spring Boot 這么好的框架我們怎么能不學呢?接下來,我將帶領大家學習 Spring Boot 這個開啟新紀元的框架,學習一個新知識,肯定是從 hello world 開始啦!
-
2.1 創建工程,引入依賴
創建工程相信大家都會,省略此程序,直接引入maven依賴,
pom.xml:<!--springboot父工程--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <!--springboot框架web組件--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.2.2.RELEASE</version> </dependency> </dependencies> <build> <!--springboot的maven插件--> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <compilerArgs> <arg>-parameters</arg> </compilerArgs> </configuration> </plugin> </plugins> </build> -
2.2 組態檔的配置
application.yml:server: port: 8080 # 應用的埠 servlet: context-path: /butterflytri # 整個應用的映射地址 spring: application: name: helloworld # 應用名稱 -
2.3 代碼撰寫
-
首先創建包:

-
創建
HelloWorldApplication.java類:package org.butterflytri; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author: WJF * @date: 2020/5/15 * @description: HelloWorldApplication */ /** * {@link SpringBootApplication}: 這個注解幫助你加載組態檔{@link 'application.yml'}的配置, * 被這個注解標識的類就是啟動類,是整個工程的入口, */ @SpringBootApplication public class HelloWorldApplication { public static void main(String[] args) { SpringApplication.run(HelloWorldApplication.class,args); } } -
創建
HelloWorldController.java類:package org.butterflytri.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author: WJF * @date: 2020/5/15 * @description: Hello World! */ @RestController @RequestMapping("/helloWorld") public class HelloWorldController { private static final String HELLO_WORLD = "Hello World!"; /** * 運行專案,訪問:http://localhost:8080/butterflytri/helloWorld/go 即可看到hello world! * @return String */ @RequestMapping("/go") public String go() { return HELLO_WORLD; } }
-
-
2.4 啟動工程,訪問方法
訪問
http://localhost:8080/butterflytri/helloWorld/go路徑,即可在頁面看到應用的回應結果:Hello World!
3. 專案地址
本專案傳送門:
- GitHub ---> spring-boot-helloworld
- Gitee ---> spring-boot-helloworld
此教程會一直更新下去,覺得博主寫的可以的話,關注一下,也可以更方便下次來學習,
- 作者:Butterfly-Tri
- 出處:Butterfly-Tri個人博客
- 著作權所有,歡迎保留原文鏈接進行轉載??
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/188613.html
標籤:Java
上一篇:簡版在線聊天Websocket
