1.開發環境
IDE:

JAVA環境:

Tomcat:

2.使用Idea生成spring boot項目
以下是使用Idea生成基本的spring boot的步驟。
(1)創建工程第一步

(2)創建工程第二步

選擇設置后,下一步(Next)
(3)配置工程信息

參考上圖填寫工程信息。
Group和Artifact被統稱為“坐標”是為了保證項目唯一性而提出的,如果把項目弄到maven本地倉庫去,若想找到項目就必須根據這兩個id設置去查找。
GroupId一般分為多個段,一般可設置兩段,第一段為域,第二段為公司名稱。域又分為org、com、cn等等許多,其中org為非營利組織,com為商業組織。舉個apache公司的tomcat項目例子:這個項目的GroupId是org.apache,它的域是org(因為tomcat是非營利項目),公司名稱是apache,ArtifactId是tomcat。
在本例中,Group設置為cn.yy,cn表示域為中國,yy自己隨便寫的一個名稱,Artifact設置為spexample,表示這個項目的名稱是spexample。在創建Maven工程后,新建包的時候,包結構最好是cn.yy.spexample打頭的,如果有個StudentDao[Dao層的],它的全路徑就是cn.yy.spexample.dao.StudentDao。
填寫完后,下一步(Next)
(4)根據工程實際需求情況,選擇相關依賴

此處選擇springboot版本為2.0.2,由於構建的是一個web工程,所以選擇web
(5)最后一步

點擊完成(Finish)項目創建
3.簡單配置並運行項目
(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> <groupId>com.yy</groupId> <artifactId>springboottest</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <!--jar包形式--> <name>springboottest</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.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 的 Starter ,包括構建 RESTful 服務應用、Spring MVC 應用等,默認使用tomcat作為嵌入式Servlet 容器--> <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> <!--能夠將Spring Boot應用打包為可執行的jar或war文件,然后以通常的方式運行Spring Boot應用。--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
里面要注意的幾個地方:
<groupId>com.yy</groupId> <artifactId>springboottest</artifactId> <version>0.0.1-SNAPSHOT</version> <!--jar包形式--> <packaging>jar</packaging>
如果要改成war包部署,則改為<packaging>war<f/ormat>
另外還需要一個Maven插件
<build> <plugins> <!--能夠將Spring Boot應用打包為可執行的jar或war文件,然后以通常的方式運行Spring Boot應用。--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
配置start依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
spring-boot-starter-web 包含了 Tomcat 和 Spring MVC ,那啟動流程是這樣的。 標識 @SpringBoot App lication 的應用,初始化經過 spring-boot-starter 核心包中的自動化配置,構建了 Spring 容器,並通過 Tomcat 啟動 Web 應用。
(2)配置application.properties

SpringBoot中免除了大部分手動配置,但是對於一些特定的情況,還是需要我們進行手動配置的,SpringBoot為我們提供了application.properties配置文件,讓我們可以進行自定義配置,來對默認的配置進行修改,以適應具體的生產情況,當然還包括一些第三方的配置。幾乎所有配置都可以寫到application.peroperties文件中,這個文件會被SpringBoot自動加載,免去了我們手動加載的煩惱。
#應用啟動端口
server.port=8088
#spring Boot上下文
server.servlet.context-path=/sptest
(3)定義Controller
新建HelloController

package com.yy.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * Created by Administrator on 2018-05-18. */ @RestController public class HelloController { @RequestMapping(value = "/hello",method= RequestMethod.GET) public String sayHello() { String hello="Return HelloSpring Boot"; return hello; } }
給類添加@RestController注解,自定義方法sayHello添加@RequestMapping注解,如上。
(4)啟動應用
確保SpringboottestApplication方法有@SpringBootApplication注解。
spring boot提供了一個統一的注解@SpringBootApplication,其相當於 (默認屬性)@Configuration + @EnableAutoConfiguration + @ComponentScan

配置設置完成后,啟動項目,並在瀏覽器中訪問如下地址:
http://localhost:8088/sptest/hello

在瀏覽器中看到上面的信息,則訪問第一個springboot應用創建成功。
SpringboottestApplication
