1.開發環境
(推薦):jdk1.8+Maven(3.2+)+Intellij IDEA+windows10;
說明:
jdk:springboot官方說的很明確,到目前版本的springboot(1.5.9),官方指定要求jdk1.8以上;
依賴包管理:可以通過拷貝jar文件的方式管理依賴,但官方也推薦使用Apache Maven 3.2或更高版本等構件工具;
開發工具:個人推薦使用IDEA,功能很強大,使用流暢度和方便性較好;
2.開始我們的第一個springboot項目
本節目標:構造第一個springboot入門web項目,通過三種方式運行啟動;並通過瀏覽器得到服務器反饋結果;
步驟:
2.1.驗證本機的jdk版本和mvn版本
確保在jdk1.8和maven3.2+;
2.2.創建項目(使用springboot官方推薦的創建方式——spring initializr):
進入https://start.spring.io/,選擇要使用的springboot版本號,這里使用1.5.9,填寫好項目組信息,在Search for dependencies中搜索web並選中;
選擇生成項目后自動下載chapter01.zip;解壓chapter01.zip后,使用IDEA打開chapter01項目文件夾;
此外,還可以在IDEA中File-new-Project,選擇Spring Initializr,在IDEA中創建springboot項目;
(注意:此處如果你的項目報錯,請確保該項目的maven配置正確;IDEA的話打開,正確指定好使用的maven是3.2以上版本即可:
)
項目正常打開后的目錄結構如下:
Chapter01Application.java 內部包含main函數,是springboot項目的啟動類;
Chapter01ApplicationTests.java 測試類
pom.xml 依賴管理文件
application.properties 配置文件,初次生成的時候是空的,以后可以在里面填寫配置項;
有的同學到這里有些懵,以往java web項目不是有個WebRoot文件夾嗎,這里我明明配置的就是web項目,為什么會是這么個目錄結構呢?
這里其實就是springboot提倡的習慣,以后開發springboot,要習慣使用模板,摒棄jsp的前端方案(如果你執意要使用jsp的話也是可以的,我們后文會介紹);現在我們項目組開發,基本全是freemarker,而且基本上全部都是請求rest 接口,這樣前后端完全分離;避免了在jsp中寫java代碼的陋習(記得以前做過一個H5支付頁面,用jsp寫的,后來項目升級,前端用vue.js,結果后台需要重新寫n多接口);以后項目想更換前端方案也不會是坑;所以要接受並學習使用模板;要徹底執行前后端分離的思想;
以前使用jsp時,你會把jsp放在WEB-INF下面;以后使用模板,你需要把模板全放到例如resources-templates下面去;
sprinboot到底使用什么模板,因人而異;我個人喜歡freemarker;
以freemarker為例,模板.ftl文件會被放到resources.templates中,其他靜態資源文件會被放到resources-static中,這塊日后再婊;
2.3項目詳解
2.3.1詳解pom.xml
我們把目光先集中在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.zjt</groupId> <artifactId>chapter01</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>chapter01</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.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>
是不是看上去很簡單,其實里面大有玄機;
首先,注意打包文件格式是jar文件,這是因為,sprinboot使用mvn插件可以打包成可執行的內嵌web容器的jar,這對於發布微服務是很方便的;如果想打成war包和其他項目一起集成在tomcat中,也是可以的,這個日后再表;
構建項目:
方法1:父依賴(spring initializr默認構建方式):
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository -->
</parent>
此處,我們發現springboot官方默認使用父依賴的方式來構件springboot項目。ctrl+左鍵點擊跟蹤spring-boot-starter-parent,會發現其實她繼承了spring-boot-dependencies;包括日后我們常用的配置文件的過濾等
總結起來,可以說spring-boot-starter-parent,有如下特性:
默認編譯級別為Java 1.8 源碼編碼為UTF-8 一個依賴管理節點,允許你省略普通依賴的 <version> 標簽,繼承自 spring-boot-dependencies POM。 合適的資源過濾 合適的插件配置(exec插件,surefire,Git commit ID,shade) 針對 application.properties 和 application.yml 的資源過濾
只要指定了父依賴的version,那么其相關的其他自動依賴均無需再指定版本號,springboot已經自動管理好了最佳的依賴配置,如圖:
這也是springboot的方便之處;
除非你手動覆蓋自己的項目中的屬性,來達到修改某個依賴的版本號的目的;
方法2:使用import來引入spring-boot-dependencies
如果不想使用父依賴的方式,可以直接通過使用一個 scope=import 的依賴來構建:
<dependencyManagement> <dependencies> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.0.BUILD-SNAPSHOT</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
Starter POMS :
下面我們看一下pom.xml中的依賴模塊,發現有一些很有特色的spring-boot-starter-*,官方學名叫starter poms;
springboot使用各種starter poms來實現組件的熱插拔;每一個starter pom就是一個可以包含到應用中的一個方便的依賴關系描述符集合;可以獲取所有Spring及相關技術的一站式服務,而不需要翻閱示例代碼,拷貝粘貼大量的依賴描述符。
例如,如果你想使用Spring和JPA進行數據庫訪問,只需要在你的項目中包含 spring-boot-starter-data-jpa 依賴,然后你就可以開始了。
下面的應用程序starters是Spring Boot在 org.springframework.boot 組下提供的,我們可以方便地查找你的項目需要的其他組件的starter,直接添加即可自動加載依賴:
Table 13.1. Spring Boot application starters
Name | Description | Pom |
---|---|---|
Core starter, including auto-configuration support, logging and YAML 核心Spring Boot starter,包括自動配置支持,日志和YAML |
||
Starter for JMS messaging using Apache ActiveMQ |
||
Starter for using Spring AMQP and Rabbit MQ 對"高級消息隊列協議"的支持,通過 spring-rabbit 實現 |
||
Starter for aspect-oriented programming with Spring AOP and AspectJ 對面向切面編程的支持,包括 spring-aop 和AspectJ |
||
Starter for JMS messaging using Apache Artemis |
||
Starter for using Spring Batch 對Spring Batch的支持,包括HSQLDB數據庫 |
||
Starter for using Spring Framework’s caching support |
||
Starter for using Spring Cloud Connectors which simplifies connecting to services in cloud platforms like Cloud Foundry and Heroku 對Spring Cloud Connectors的支持,簡化在雲平台下(例如,Cloud Foundry 和Heroku)服務的連接 |
||
Starter for using Cassandra distributed database and Spring Data Cassandra |
||
Starter for using Cassandra distributed database and Spring Data Cassandra Reactive |
||
Starter for using Couchbase document-oriented database and Spring Data Couchbase |
||
Starter for using Couchbase document-oriented database and Spring Data Couchbase Reactive |
||
Starter for using Elasticsearch search and analytics engine and Spring Data Elasticsearch 對Elasticsearch搜索和分析引擎的支持,包括 spring-data-elasticsearch |
||
Starter for using Spring Data JPA with Hibernate 對"Java持久化API"的支持,包括 spring-data-jpa , spring-orm 和Hibernate |
||
Starter for using Spring Data LDAP |
||
Starter for using MongoDB document-oriented database and Spring Data MongoDB 對MongoDB NOSQL數據庫的支持,包括 spring-data-mongodb |
||
Starter for using MongoDB document-oriented database and Spring Data MongoDB Reactive |
||
Starter for using Neo4j graph database and Spring Data Neo4j |
||
Starter for using Redis key-value data store with Spring Data Redis and the Lettuce client |
||
Starter for using Redis key-value data store with Spring Data Redis reactive and the Lettuce client |
||
Starter for exposing Spring Data repositories over REST using Spring Data REST 對通過REST暴露Spring Data倉庫的支持,通過 spring-data-rest-webmvc 實現 |
||
Starter for using the Apache Solr search platform with Spring Data Solr 對Apache Solr搜索平台的支持,包括 spring-data-solr |
||
Starter for building MVC web applications using FreeMarker views 對FreeMarker模板引擎的支持 |
||
Starter for building MVC web applications using Groovy Templates views 對Groovy模板引擎的支持 |
||
Starter for building hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS 對基於HATEOAS的RESTful服務的支持,通過 spring-hateoas 實現 |
||
Starter for using Spring Integration 對普通 spring-integration 模塊的支持 |
||
Starter for using JDBC with the Tomcat JDBC connection pool 對JDBC數據庫的支持 |
||
Starter for building RESTful web applications using JAX-RS and Jersey. An alternative to 對Jersey RESTful Web服務框架的支持 |
||
Starter for using jOOQ to access SQL databases. An alternative to |
||
Starter for reading and writing json |
||
Starter for JTA transactions using Atomikos 對JTA分布式事務的支持,通過Atomikos實現 |
||
Starter for JTA transactions using Bitronix 對JTA分布式事務的支持,通過Bitronix實現 |
||
Spring Boot Narayana JTA Starter |
||
Starter for using Java Mail and Spring Framework’s email sending support 對 javax.mail 的支持 |
||
Starter for building web applications using Mustache views 對Mustache模板引擎的支持 |
||
Spring Boot Quartz Starter |
||
Starter for using Spring Security 對 spring-security 的支持 |
||
Starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito |
||
Starter for building MVC web applications using Thymeleaf views 對Thymeleaf模板引擎的支持,包括和Spring的集成 |
||
Starter for using Java Bean Validation with Hibernate Validator |
||
Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container 對全棧web開發的支持,包括Tomcat和 spring-webmvc |
||
Starter for using Spring Web Services |
||
Starter for building WebFlux applications using Spring Framework’s Reactive Web support |
||
Starter for building WebSocket applications using Spring Framework’s WebSocket support 對WebSocket開發的支持 |
除了應用程序的starters,下面的starters可以用於添加生產准備的特性:
Table 13.2. Spring Boot production starters
Name | Description | Pom |
---|---|---|
Starter for using Spring Boot’s Actuator which provides production ready features to help you monitor and manage your application 添加生產准備特性,比如指標和監控 |
最后,Spring Boot包含一些可用於排除或交換具體技術方面的starters。
Table 13.3. Spring Boot technical starters
Name | Description | Pom |
---|---|---|
Starter for using Jetty as the embedded servlet container. An alternative to 導入Jetty HTTP引擎(作為Tomcat的替代) |
||
Starter for using Log4j2 for logging. An alternative to 對Log4J日志系統的支持 |
||
Starter for logging using Logback. Default logging starter 導入Spring Boot的默認日志系統(Logback) |
||
Starter for using Reactor Netty as the embedded reactive HTTP server. |
||
Starter for using Tomcat as the embedded servlet container. Default servlet container starter used by 導入Spring Boot的默認HTTP引擎(Tomcat) |
||
Starter for using Undertow as the embedded servlet container. An alternative to 導入Undertow HTTP引擎(作為Tomcat的替代) |
使用maven插件用來打包springboot項目:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins>
</build>
2.3.2代碼詳解:
項目啟動類(Chapter01Application.java):
package com.zjt.chapter01; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Chapter01Application { public static void main(String[] args) { SpringApplication.run(Chapter01Application.class, args); } }
上述Chapter01Application.java為springboot的main啟動類;
這里我們開始學習到了springboot啟動類中的最重要的一個注解:@SpringBootApplication
注意:
1.項目啟動類,根據官方建議,一定要位於非 default package下;也就是說這個啟動類一定要含有package的聲明;建議使用反轉域名;如com.zjt.learn;
2.官方建議,main應用類要放在其他類上面的根包(root package)中;
3.@SpringBootApplication該注解在基於上述1和2兩點下,才可以使用(習慣優於配置);
由於該注解相當於:
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
_______________華麗的小分割__________________
@SpringBootConfiguration:說明當前類是一個配置類,就像xml配置文件,而現在是用java配置文件,效果是一樣的,它會被@ComponentScan掃描到。
@EnableAutoConfiguration(開啟自動配置,springboot的靈魂所在)注解通常都放到main所在類的上面,當main所在類位於root package的時候,這樣@EnableAutoConfiguration可以從逐層的往下搜索各個加注解的類,例如,你正在編寫一個JPA程序(如果你的pom里進行了配置的話),spring會自動去搜索加了@Entity注解的類,並進行調用;
@ComponentScan:用注解配置實現自動掃描,默認會掃描當前包和所有子包,和xml配置自動掃描效果一樣;
___________________________________________
所以我們的springboot很貼心的為我們准備了Springboot 提供了統一的注解@SpringBootApplication來替代以上三個注解,簡化程序的配置;
2.3.3.最后我們自己添加controller包,添加一個TestController.java,接受rest請求:
package com.zjt.chapter01.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @RequestMapping("/hello") public String Hello(){ return "hello world"; } }
2.4.啟動項目
2.4.1使用main啟動類啟動:
看到這樣就證明已經順利啟動了;
2.4.2使用mvn spring-boot:run”在命令行啟動;
2.4.3使用mvn package打成可執行jar;使用java -jar啟動;
mvn clean ;mvn build; mvn package;
打包后,命令行啟動:
OK,今天的入門我們就一起做到這里;后續會同大家共同探討很多springboot的實用教程和案例;文章中有什么問題或者想法歡迎老鐵們吐槽和我溝通;我會及時糾正錯誤,改進教程的品質;
做程序需要靜心,扎扎實實做技術;不忘初心,腳踏實地,就醬。