1.開發環境 (推薦):jdk1.8+Maven(3.2+)+Intellij IDEA+windows10; 說明: jdk:springboot官方說的很明確,到目前版本的springboot(1.5.9),官方指定要求jdk1.8以上; 依賴包管理:可以通過拷貝jar文件的方式管理依賴,但官方 ...
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 |
![]() 更多相關文章
一周排行
![]() 所有分類
|