什么是Spring boot
Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發(fā)過程。該框架使用了特定的方式來進行配置,從而使開發(fā)人員不再需要定義樣板化的配置。
Spring boot 的優(yōu)點
Spring Boot是基于Spring的一個輕量級框架,其特點是簡單、快速和方便,非常適合用于微服務開發(fā)。
- 輕松創(chuàng)建獨立的Spring應用程序。
- 內嵌Tomcat、jetty等web容器,不需要部署WAR文件。
- 提供一系列的“starter” 來簡化的Maven配置。
- 開箱即用,盡可能自動配置Spring。
創(chuàng)建項目(IDEA)
1.創(chuàng)建一個項目

新建項目

選擇Spring Initializr

填寫項目名稱,選擇maven和java版本

選擇Web
創(chuàng)建完成后項目結構如下

image.png
2.添加依賴
找到pom.xml文件,加入依賴(一般創(chuàng)建項目時會自動生成):
<?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>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.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>
<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>
其他依賴根據(jù)項目需求添加。
3.創(chuàng)建目錄和配置文件
創(chuàng)建 src/main/resources 源文件目錄,并在該目錄下創(chuàng)建 application.properties 文件、static 和 templates 的文件夾。
- application.properties:用于配置項目運行所需的配置數(shù)據(jù)。
- static:用于存放靜態(tài)資源,如:css、js、圖片等。
-
templates:用于存放模板文件。
resources文件
4.創(chuàng)建啟動類
在 src>>main>>java>>com.example.demo>>DemoApplication中編寫以下代碼:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
5.案例演示
@RestController
public class hello {
@GetMapping("/helloworld")
public String helloworld(){
return "helloworld";
}
}

案例演示
