springboot實用教程:2.搭建你的第一個springboot應用

来源:https://www.cnblogs.com/zhaojiatao/archive/2018/01/17/8296676.html
-Advertisement-
Play Games

1.開發環境 (推薦):jdk1.8+Maven(3.2+)+Intellij IDEA+windows10; 說明: jdk:springboot官方說的很明確,到目前版本的springboot(1.5.9),官方指定要求jdk1.8以上; 依賴包管理:可以通過拷貝jar文件的方式管理依賴,但官方 ...


 springboot

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

NameDescriptionPom

spring-boot-starter

Core starter, including auto-configuration support, logging and YAML

核心Spring Boot starter,包括自動配置支持,日誌和YAML

Pom

spring-boot-starter-activemq

Starter for JMS messaging using Apache ActiveMQ

Pom

spring-boot-starter-amqp

Starter for using Spring AMQP and Rabbit MQ

對"高級消息隊列協議"的支持,通過 spring-rabbit  實現

Pom

spring-boot-starter-aop

Starter for aspect-oriented programming with Spring AOP and AspectJ

對面向切麵編程的支持,包括 spring-aop  和AspectJ

Pom

spring-boot-starter-artemis

Starter for JMS messaging using Apache Artemis

Pom

spring-boot-starter-batch

Starter for using Spring Batch

對Spring Batch的支持,包括HSQLDB資料庫

Pom

spring-boot-starter-cache

Starter for using Spring Framework’s caching support

Pom

spring-boot-starter-cloud-connectors

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)服務的連接

Pom

spring-boot-starter-data-cassandra

Starter for using Cassandra distributed database and Spring Data Cassandra

Pom

spring-boot-starter-data-cassandra-reactive

Starter for using Cassandra distributed database and Spring Data Cassandra Reactive

Pom

spring-boot-starter-data-couchbase

Starter for using Couchbase document-oriented database and Spring Data Couchbase

Pom

spring-boot-starter-data-couchbase-reactive

Starter for using Couchbase document-oriented database and Spring Data Couchbase Reactive

Pom

spring-boot-starter-data-elasticsearch

Starter for using Elasticsearch search and analytics engine and Spring Data Elasticsearch

對Elasticsearch搜索和分析引擎的支持,包括 spring-data-elasticsearch

Pom

spring-boot-starter-data-jpa

Starter for using Spring Data JPA with Hibernate

對"Java持久化API"的支持,包括 spring-data-jpa  , spring-orm  和Hibernate

Pom

spring-boot-starter-data-ldap

Starter for using Spring Data LDAP

Pom

spring-boot-starter-data-mongodb

Starter for using MongoDB document-oriented database and Spring Data MongoDB

對MongoDB NOSQL資料庫的支持,包括 spring-data-mongodb

Pom

spring-boot-starter-data-mongodb-reactive

Starter for using MongoDB document-oriented database and Spring Data MongoDB Reactive

Pom

spring-boot-starter-data-neo4j

Starter for using Neo4j graph database and Spring Data Neo4j

Pom

spring-boot-starter-data-redis

Starter for using Redis key-value data store with Spring Data Redis and the Lettuce client

Pom

spring-boot-starter-data-redis-reactive

Starter for using Redis key-value data store with Spring Data Redis reactive and the Lettuce client

Pom

spring-boot-starter-data-rest

Starter for exposing Spring Data repositories over REST using Spring Data REST

對通過REST暴露Spring Data倉庫的支持,通過 spring-data-rest-webmvc  實現

Pom

spring-boot-starter-data-solr

Starter for using the Apache Solr search platform with Spring Data Solr

對Apache Solr搜索平臺的支持,包括 spring-data-solr

Pom

spring-boot-starter-freemarker

Starter for building MVC web applications using FreeMarker views

對FreeMarker模板引擎的支持

Pom

spring-boot-starter-groovy-templates

Starter for building MVC web applications using Groovy Templates views

對Groovy模板引擎的支持

Pom

spring-boot-starter-hateoas

Starter for building hypermedia-based RESTful web application with Spring MVC and Spring HATEOAS

對基於HATEOAS的RESTful服務的支持,通過 spring-hateoas  實現

Pom

spring-boot-starter-integration

Starter for using Spring Integration

對普通 spring-integration  模塊的支持

Pom

spring-boot-starter-jdbc

Starter for using JDBC with the Tomcat JDBC connection pool

對JDBC資料庫的支持

Pom

spring-boot-starter-jersey

Starter for building RESTful web applications using JAX-RS and Jersey. An alternative to spring-boot-starter-web

對Jersey RESTful Web服務框架的支持

Pom

spring-boot-starter-jooq

Starter for using jOOQ to access SQL databases. An alternative to spring-boot-starter-data-jpa or spring-boot-starter-jdbc

Pom

spring-boot-starter-json

Starter for reading and writing json

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 眾所周知,雲計算可以劃分為以下幾個層次的服務——IaaS、PaaS和SaaS,而今天我們今天講的多租戶架構就是一種常見的 SaaS 軟體架構模式,或者說是商業模式。 通常,一個多租戶軟體指的是依托雲計算的彈性環境,搭建並使用一個單一的應用程式實例來服務多個客戶,每個客戶稱之為“租戶”來共用同一個軟體 ...
  • 概念:設計模式是一套被反覆使用、多數人知曉的、經過分類編目的、代碼設計經驗的總結。 以下是對上面有下劃線的關鍵字的通俗解釋: 優點: 設計模式可以幫助我們改良項目的代碼,增強代碼的健壯性、可擴展性,為以後開發和維護鋪平道路。有過開發經驗的人都知道一個項目的代碼設計好壞對之後開發的影響,特別是從事維護 ...
  • 註:本文章示例為C#代碼,設計模式通用任何編程語言,可放心閱讀 設計模式(3W1H) What?針對特定問題特出的特定的解決方案 Why?讓程式有更好的可擴展度 Where?一般情況下,開發中真正使用設計模式的地方不多。面試 JVM底層機制 模式 框架底層應用了很多設計模式 How?任何時候、任何場 ...
  • 解決方法一 重置視窗佈局: windows > perspective > reset perspective 解決方法二: 如果上述方法不好使,採用下麵的這種方法: 刪除workspace的/.metadata/.plugins/org.eclipse.core.resources/.projec ...
  • 堆和棧都是Java用來在RAM中存放數據的地方。 堆 (1)Java的堆是一個運行時數據區,類的對象從堆中分配空間。這些對象通過new等指令建立,通過垃圾回收器來銷毀。 (2)堆的優勢是可以動態地分配記憶體空間,需要多少記憶體空間不必事先告訴編譯器,因為它是在運行時動態分配的。但缺點是,由於需要在運行時 ...
  • 在上一篇文章中(Java的參數傳遞是值傳遞還是引用傳遞),主要分析了java語言的參數傳遞只有按值傳遞而沒有按引用傳遞。 先看一下微軟的C#文檔對按引用傳遞的定義(如下截圖):https://docs.microsoft.com/zh-cn/dotnet/csharp/language-refere ...
  • 瞭解就行了** 通過新建file建立含有標簽,標簽屬性##xml的格式如下,就是通過<>節點來區別數據結構的: <?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <year ...
  • 當一個對象被當作參數傳遞到一個方法後,在此方法內可以改變這個對象的屬性,那麼這裡到底是值傳遞還是引用傳遞? 答:是值傳遞。Java 語言的參數傳遞只有值傳遞。當一個實例對象作為參數被傳遞到方法中時,參數的值就是該對象的引用的一個副本。指向同一個對象,對象的內容可以在被調用的方法內改變,但對象的引用( ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...