Hello ! 我是小小,今天是本周的第五篇,本周第五篇將會著重講解 Spring Boot 之 Hello World
前言
Spring Boot 出現的原因
Spring Boot的出現,主要是用來解決 Spring 過去的一些問題,提出了約定優于配置的思想,默認對很多方法進行了設定,使得開發者可以快速的構建專案,集成第三方的內容,使得開發效率大大提升,
基本概念
Spring Boot 不單單是一套框架,是一套體系,目的是簡化 Spring 的開發,
特點
基于 Spring 的開發提供更快的入門 直接上手,冗余代碼沒有 內嵌式容器 簡化 Spring
核心功能
極度依賴構建工具 能夠進行自動化的配置
Hello World
Maven創建
創建一個新的空工程,分別創建 module,如下圖所示
創建 Maven Module
創建一個 Module,選擇 Maven 工程,勾選以前用的 web 骨架填寫好 GroupID,ArtifactID
選擇好以后,按住回車
這樣就完成了一個基本的 maven 專案的創建
添加起步依賴
根據 Spring Boot 的要求,進行簡單的測驗,以及添加相應的起步依賴 專案需要繼承 Spring Boot 的起步依賴 Spring boot starter parent 為了集成 Spring MVC 進行 Controller 開發,需要匯入 Spring boot starter web
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
</parent>
<groupId>cn.ideal</groupId>
<artifactId>springboot_01_start</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
撰寫 Spring Boot 啟動類
這里撰寫 Spring Boot 啟動類
package cn.ideal;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class);
}
}
創建控制層
package cn.ideal.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class QuickStartController {
@RequestMapping("/test")
@ResponseBody
public String test(){
return "springboot 訪問測驗,起飛,飛飛飛飛 ~ ~ ~";
}
}
測驗 Spring Boot
專案啟動,控制臺會輸出如下內容
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _
( ( )___ | '_ | '_| | '_ / _` |
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |___, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.7.RELEASE)
2020-05-10 22:11:34.973 INFO 30580 --- [ main] cn.ideal.MySpringBootApplication : Starting MySpringBootApplication on LAPTOP-5T03DV1G with PID 30580 (F:developIdeaProjectsframework-codespringboot_01_demospringboot_01_starttargetclasses started by abc in F:developIdeaProjectsframework-codespringboot_01_demo)
2020-05-10 22:11:34.976 INFO 30580 --- [ main] cn.ideal.MySpringBootApplication : No active profile set, falling back to default profiles: default
2020-05-10 22:11:35.686 INFO 30580 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-05-10 22:11:35.693 INFO 30580 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-05-10 22:11:35.693 INFO 30580 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.34]
2020-05-10 22:11:35.765 INFO 30580 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-05-10 22:11:35.766 INFO 30580 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 747 ms
2020-05-10 22:11:35.884 INFO 30580 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-05-10 22:11:35.990 INFO 30580 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-05-10 22:11:35.993 INFO 30580 --- [ main] cn.ideal.MySpringBootApplication : Started MySpringBootApplication in 1.318 seconds (JVM running for 2.665)
圖片顯示如下輸入創建的 controller 專案直接列印出來
專案打包成為 jar 包
添加依賴
<plugin>
<!-- 打包插件 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
單擊右邊,進行package 進行打包,選擇package選項
可以看到target下產生了新的jar包,這里直接在cmd中運行
關于作者
我是小小,一枚小小的程式猿,bye~bye!
小明菜市場
推薦閱讀
● 太好了 | 這篇寫的太好了!Spring Boot + Redis 實作介面冪等性
● 挖礦事業開啟!小小帶你賺錢帶你飛!
● 財政 | 十月,我的財政狀況
● 調優 | 別再說你不會 JVM 性能監控和調優了
● 沒想到 | 萬萬沒想到 Java 中最重要的關鍵字竟然是這個
給我個好看再走好嗎?
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/221852.html
標籤:其他
上一篇:單向哈希函式與MAC實驗
