1.打開IDEA,創建新項目,選擇Spring Initializr

2.輸入Artifact

3.勾選Web

4.點擊finish完成

5.進入項目,可以將以下內容刪除

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>
<groupId>com.example</groupId>
<artifactId>springbootdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springbootdemo</name>
<description>Demo project for Spring Boot</description>
<!--起步依賴-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--開發web項目相關依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot單元測試-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--maven構建-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
6.創建一個HelloController
package com.example;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "hello,this is a springboot demo";
}
}
7.程序自動生成的SpringbootdemoApplication,會有一個@SpringBootApplication的注解,這個注解用來標明這個類是程序的入口
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//入口
@SpringBootApplication
public class SpringbootdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootdemoApplication.class, args);
}
}
@SpringBootApplication開啟了Spring的組件掃描和springboot的自動配置功能,相當于將以下三個注解組合在了一起
(1)@Configuration:表名該類使用基于Java的配置,將此類作為配置類
(2)@ComponentScan:啟用注解掃描
(3)@EnableAutoConfiguration:開啟springboot的自動配置功能
8.運行SpringbootdemoApplication類

測試:
在地址欄中輸入http://localhost:8080/hello

9.使用啟動jar包的方式啟動
(1)首先進入項目所在目錄,如果是mac系統在項目上右鍵,選擇Reveal in Finder,Windows系統在項目上右鍵選擇Show in Explorer,即可打開項目所在目錄
(2)打開終端,進入項目所在目錄
cd /Users/shanml/IdeaProjects/SpringbootDemo
輸入mvn install,構建項目
(3)構建成功后,在項目target文件夾下會多出一個jar包
(4)使用java -jar springbootdemo-0.0.1-SNAPSHOT.jar
啟動jar包即可

參考:
廖師兄:兩小時學會Springboot
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。