springboot快速搭建,springboot搭建

來源:互聯網
上載者:User

springboot快速搭建,springboot搭建

環境

spring-boot-starter-parent 是一個特殊的starter,它提供了有用的Maven預設設定。同時,它也提供了一個 dependency-management 節點,這樣對於”blessed“依賴你可以省略version標記;spring-boot-starter-parent 本身並沒有提供依賴

<parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>1.3.0.BUILD-SNAPSHOT</version></parent>

Maven使用者可以繼承 spring-boot-starter-parent 項目來擷取合適的預設設定。該父項目提供以下特性:

  • 預設編譯層級為Java 1.6
  • 源碼編碼為UTF-8
  • 一個依賴管理節點,允許你省略普通依賴的 <version> 標籤,繼承自 spring-boot-dependencies POM。
  • 合適的資源過濾
  • 合適的外掛程式配置(exec外掛程式,surefire,Git commit ID,shade)
  • 針對 application.properties 和 application.yml 的資源過濾

註:你應該只需要在該依賴上指定Spring Boot版本。如果匯入其他的starters,你可以放心的省略版本號碼。

 

使用沒有父POM的Spring Boot

 

不是每個人都喜歡繼承 spring-boot-starter-parent POM。你可能需要使用公司標準parent,或你可能傾向於顯式聲明所有Maven配置。
如果你不使用 spring-boot-starter-parent ,通過使用一個 scope=import 的依賴,你仍能擷取到依賴管理的好處:

<dependencyManagement>    <dependencies>        <dependency>        <!-- Import dependency management from Spring Boot -->            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-dependencies</artifactId>            <version>1.3.0.BUILD-SNAPSHOT</version>            <type>pom</type>            <scope>import</scope>        </dependency>    </dependencies></dependencyManagement>

 

改變Java版本

spring-boot-starter-parent 選擇相當保守的Java相容策略。如果你遵循我們的建議,使用最新的Java版本,你可以添加一個 java.version 屬性:

<properties>        <java.version>1.8</java.version></properties>    

 

其他的”Starter POMs“簡單的提供依賴,這些依賴可能是你開發特定類型的應用時需要的。由於正在開發一個web應用,我們將添加一個 spring-boot-starter-web 依賴;如果依賴spring-boot-starter-parent父工程,版本號碼可以省略

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</artifactId>    <version>1.3.5.RELEASE</version></dependency>

編寫代碼

為了完成應用程式,我們需要建立一個單獨的Java檔案。Maven預設會編譯 src/main/java 下的源碼,所以你需要建立那樣的檔案結構,然後添加一個名為 src/main/java/Example.java 的檔案:

import org.springframework.boot.*;import org.springframework.boot.autoconfigure.*;import org.springframework.stereotype.*;import org.springframework.web.bind.annotation.*;@RestController@EnableAutoConfigurationpublic class Example {    @RequestMapping("/")    String home() {        return "Hello World!";    }    public static void main(String[] args) throws Exception {        SpringApplication.run(Example.class, args);    }}

使用"default"包

當類沒有包含 package 聲明時,它被認為處於 default package 下。通常不推薦使用 default package ,並應該避免使用它。因為對於使用 @ComponentScan , @EntityScan 或 @SpringBootApplication 註解的Spring Boot應用來說,來自每個jar的類都會被讀取,這會造成一定的問題。
註:我們建議你遵循Java推薦的包命名規範,使用一個反轉的網域名稱(例如 com.example.project )

定位main應用類

我們通常建議你將main應用類放在位於其他類上面的根包(root package)中。通常使用 @EnableAutoConfiguration 註解你的main類,並且暗地裡為某些項定義了一個基礎“search package”。例如,如果你正在編寫一個JPA應用,@EnableAutoConfiguration 註解的類所在包將被用來搜尋 @Entity 項。

使用根包允許你使用 @ComponentScan 註解而不需要定義一個 basePackage 屬性。如果main類位於根包中,你也可以使用 @SpringBootApplication 註解。

 

運行案例

我們的應用程式最後部分是main方法。這隻是一個標準的方法,它遵循Java對於一個應用程式進入點的約定。我們的main方法通過調用run,將業務委託給了Spring Boot的SpringApplication類。SpringApplication將引導我們的應用,啟動Spring,相應地啟動被自動設定的Tomcat web伺服器。我們需要將 Example.class 作為參數傳遞給run方法來告訴SpringApplication誰是主要的Spring組件。為了暴露任何的命令列參數,args數組也會被傳遞過去。

如果使用一個瀏覽器開啟http://localhost:8080,你應該可以看到以下輸出:

Hello World!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.