springboot項目快速搭建的方法步驟
1. 問題描述
springboot的面世,成為Java開發(fā)者的一大福音,大大提升了開發(fā)的效率,其實springboot只是在maven的基礎(chǔ)上,對已有的maven gav進(jìn)行了封裝而已,今天用最簡單的代碼快速入門springboot。
2. 解決方案
強烈推薦大家使用Idea的付費版(破解感謝下藍(lán)宇),Idea對maven、git等插件支持的更加好。
使用idea自帶的spring Initializr(實際調(diào)用的是springboot的官網(wǎng)上的initializr),快速新建springboot項目。
2.1 新建Springboot項目
(1)file->new->project

(2)點擊next(第一個)
創(chuàng)建springboot項目(因為連接的國外的網(wǎng)站,next有時會幾秒的延遲),將兩個值改成自己的配置,Group:com.laowang ,Artifact:sptest,其他可以不用動,點擊ok

(3)點擊next(第二個)
選擇web-》spring web starter

(4)點擊next(第三個)
不用做修改,直接finish

新建springboot項目已經(jīng)完成。
2.2 springboot默認(rèn)生成三個文件
默認(rèn)生成的三個文件
2.2.1. pom.xml
<?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.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.laowang</groupId>
<artifactId>sptest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sptest</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
重點就一個gav:spring-boot-starter-web,其他可以刪除。
2.2.2 application.properties
該文件默認(rèn)為空,springboot的默認(rèn)啟動端口號:8080,可以在改文件修改。
2.2.3 啟動類文件(SptestApplication.java)
package com.laowang.sptest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SptestApplication {
public static void main(String[] args) {
SpringApplication.run(SptestApplication.class, args);
}
}
重點是標(biāo)簽:@SpringBootApplication
2.3 驗證springboot
在com.laowang.sptest報下新建ctroller包,并新建類:HelloController
package com.laowang.sptest.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
@Controller
public class HelloController {
@RequestMapping("/")
@ResponseBody
public String getHello() {
return "hello";
}
}
執(zhí)行效果:

服務(wù)正常啟動。
2.4 重點說明
需要說明兩點:
(1)類文件要放在跟啟動類同級或者下一目錄下,本項目為:com.laowang.sptest包下面。因為springboot默認(rèn)掃描加載啟動類同級或者下級目錄的標(biāo)簽類(@RestController,@Service ,@Configuraion,@Component等),假如真需要加載其他目錄的標(biāo)簽類,可以通過在啟動上配置標(biāo)簽@ComponentScan(具體包)來進(jìn)行掃描加載。
(2)資源文件默認(rèn)放到resources下面,templates默認(rèn)放的是動態(tài)文件,比如:html文件,不過要搭配thymeleaf 使用(pom文件中需新加gav)。
其他也沒什么了,springboot主要是通過spring提供的多個starter和一些默認(rèn)約定,實現(xiàn)項目的快速搭建。
到此這篇關(guān)于springboot項目快速搭建的方法步驟的文章就介紹到這了,更多相關(guān)springboot快速搭建內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解設(shè)計模式在Spring中的應(yīng)用(9種)
這篇文章主要介紹了詳解設(shè)計模式在Spring中的應(yīng)用(9種),詳細(xì)的介紹了這9種模式在項目中的應(yīng)用,具有一定的參考價值,感興趣的可以了解一下2019-04-04
Springboot jpa @Column命名大小寫問題及解決
這篇文章主要介紹了Springboot jpa @Column命名大小寫問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
Netty核心功能之?dāng)?shù)據(jù)容器ByteBuf詳解
這篇文章主要為大家介紹了Netty核心功能之?dāng)?shù)據(jù)容器ByteBuf詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10

