一、SpringBoot簡介
開發團隊:Pivotal團隊
主要目的:簡化新Spring應用的初始搭建以及開發過程。
秉持理念:約定優於配置。(該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置)
二、SpringBoot的特點
1、快速創建獨立的Spring應用程序。
Spring Boot 並不是對 Spring 功能上的增強,而是提供了一種快速使用 Spring 的方式
2、嵌入的Tomcat,不需要部署war文件
3、簡化了Maven的配置
4、自動裝配Spring
5、開箱即用,沒有代碼生成、也不需要XML的配置
6、是微服務的入門級框架,SpringBoot是SpringCloud的基礎
7、提供生產就緒功能:如指標、健康檢查、外部配置等
三、玩耍環境推薦
Maven 3.3.9 + JDK1.8 + IDEA (Ultimate)
有關Maven的配置以及IDEA中配置Maven可以看我的另一篇博客
http://www.cnblogs.com/zmfx/p/8903871.html
四、創建首個SpringBoot項目
新建Sring Initalizr項目
然后點擊next,顯示為下面這張圖片
這里我就不建什么helloworld、hellospringboot之類的項目了,注意工程名字要全小寫,package配置的是包名,可以根據自己的喜愛設置,然后點擊next
這里我們選擇web工程,然后next
這里是讓我們選擇自己的工作路徑,建議不要包含中文路徑,點擊finish
下面,我們可以刪除一些暫時用不到的文件夾和文件(一共是3個)
注意我使用的springboot版本號為2.0.1.RELEASE,盡量和我一致,不能然后面我所使用的方法你可能用不了。
如果這里顯示為紅色的話,是因為maven還沒有把所需要的依賴下載到本地倉庫。
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.zmfx</groupId> <artifactId>dog</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>dog</name> <description>Demo project for Spring Boot</description> <parent><!--定義了SpringBoot的版本信息--> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.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> c <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin><!--SpringBoot的基本配置--> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
當所有的maven依賴下載完畢的時候,你項目的目錄結構樣子大體應該是這個樣子的
依然是老套路,新建一個HelloController,來和SpringBoot打個招呼吧
package com.zmfx.hello; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * 這里的@RestController 相當於@ResponseBody + @Controller */ @RestController public class HelloController { @RequestMapping(value = "/hello",method = RequestMethod.GET) public String helloSpringBoot(){ return "Hello SpringBoot!"; } }
我們點擊IDEA的運行按鈕,然后在控制台會看到SpringBoot內置的Tomcat訪問的默認端口號8080
然后在網址中輸入 http://127.0.0.1:8080/hello 頁面會展示 Hello SpringBoot!
說明我們成功的使用SpringBoot項目和SpringBoot打過招呼了
這里需要說明一下,SpringBoot的啟動類 DogApplication
package com.zmfx; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication //用來聲明這是啟動SpringBoot程序的啟動類,必須有 public class DogApplication { public static void main(String[] args) { SpringApplication.run(DogApplication.class, args); } }
今天就先分享到這里,后面的知識會陸續以博客的方式分享出來。