一.簡介
springBoot的理念是讓你不需要在項目中進行大量的配置就能讓項目快速運行起來。使用springBoot輕易的就可以建立一個獨立啟動並執行,基於Spring准生產層級的架構項目,而不用或者很少的Spring配置。 二.開發環境
eclipse版本:4.6.1
maven版本:3.3.3
JDK版本:1.8.0_111
SpringBoot版本:1.4.3 三.項目結構
圖 四.操作步驟
1.建立一個Maven項目
圖
圖
圖
2.建立完成後,如果項目報紅色,(1).需要對項目右鍵-》屬性-》Generate Deloyment Desriptor Stub。(2).項目右鍵-》Maven-》Update Project
圖
圖
圖 五.檔案清單
項目建好後,修改以下檔案。
pom.xml
<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>springbootDemo</groupId><artifactId>springbootDemo</artifactId><version>1.0</version><packaging>war</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.3.RELEASE</version><relativePath /></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
說明:添加spring-boot-starter-web依賴,會自動添加Tomcat和Spring MVC的依賴,那麼Spring Boot會對Tomcat和Spring MVC進行自動設定。
Application.java
package com.dqiang.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @author StemQ * @version v1.0 * Blog:http://blog.csdn.net/stemq * Web:www.dqiang.com */@SpringBootApplication@RestControllerpublic class Application {@RequestMapping("/")public String greeting() {return "Hello World!";}public static void main(String[] args) {SpringApplication.run(Application.class, args);}}
說明:@SpringBootApplication是Spring Boot的核心註解,也是一個組合註解。主要組合了@Configuration、@EnableAutoConfiguration、@ComponentScan。如果不使用組合註解@SpringBootApplication則可以直接使用@Configuration、@EnableAutoConfiguration、@ComponentScan。
application.properties
#tomcat\u7AEF\u53E3\u4FEE\u6539server.port=9090
說明:此檔案可以不配置,預設springBoot整合的Tomcat啟動連接埠是8080,因為我系統有別的環境佔用8080連接埠,所以將項目的運行連接埠修改為9090 六.運行
1.項目右鍵-》Run As-》Spring Boot App
圖
2.訪問
圖 七.Demo下載
點擊下載