Spring Boot實踐教程(一):Hello,world!

來源:互聯網
上載者:User

標籤:對話   reporting   ica   目錄   output   選擇   uil   public   構建   

??本篇會通過完成一個常用的Helloworld樣本,來開始我們的Spring Boot旅程

準備工作

15分鐘

開發環境

項目建立

??項目建立的時候我們可以選擇通過自訂配置Maven檔案來建立,也可以用Spring官方提供的Spring Initializr工具,Spring Initializr是一個可以簡化Spring Boot項目構建的工具,有兩種使用Spring Initializr的方法:

  • 官方提供的web頁面工具,地址

  • 開發工具整合的方式

??兩種方式操作的步驟是一樣的,只不過通過Web頁面工具多了一步將產生結果匯入到開發工具的過程,這裡我們採用的第二種:開發工具整合的Spring Initializr。

??在Idea中New Project >選擇Spring Initializ即出現如下對話方塊:

??填寫對應的Group、Artifact,選擇Type為Maven Project,並填寫其他相關資訊(如所示)後點擊下一步,在這個步驟中可以選擇對應的功能,選擇後Spring Boot就會為項目添加對應的依賴等,這裡我們只添加一個Web的基礎功能。進一步填寫項目路徑後即可完成一個Web項目的建立。

??看一下項目建立完成之後的結構:

spring-boot-1-helloworld    │  pom.xml        ├─src    │  ├─main    │  │  ├─java    │  │  │  └─com    │  │  │      └─practice    │  │  │          └─springboot    │  │  │              └─hello    │  │  │                  │  SpringBoot1HelloworldApplication.java    │  │  │                              │  │  └─resources    │  │      │  application.properties    │  │      ├─static    │  │      └─templates    │  └─test    │      └─java    │          └─com    │              └─practice    │                  └─springboot    │                      └─hello    │                              SpringBoot1HelloworldApplicationTests.java
項目運行

??我們可以通過運行main方法的方式將項目啟動起來,我們先建立一個Controller來看更直觀的看一下啟動之後的效果,在web包(需要手動建立)中建立HelloController:

package com.practice.springboot.hello.web;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController {    @RequestMapping("/")    public String hi(){        return "Congratulations! It works!";    }}

??運行Springboot1HelloApplication.java中的main方法,我們會在控制台中看到一個Spring的標誌和Spring Boot版本的說明。啟動完成之後在瀏覽器中訪問http://localhost:8080/ ,會看到。至此我們花了幾分鐘,就建立了一個web項目並將其運行起來了。在這個過程中我們沒有添加任何jar包的依賴,沒有配置Web.xml,沒有配置Spring和SpringMVC的環境,甚至都沒有將項目部署只Tomcat,因為這一切Spring Boot都為我們完成了。

項目分析

??首先我們來看一下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.practice.springboot</groupId>    <artifactId>hello</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <name>spring-boot-1-helloworld</name>    <description>spring-boot-1-helloworld</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.8.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>

??在pom檔案中可以看到以下資訊:

  1. 整個項目繼承於spring-boot-starter-parent
  2. 項目有兩個依賴包spring-boot-starter-web,spring-boot-starter-test

??有了以上兩個配置,Spring Boot就為我們配置了一個基礎的web項目的環境,包括依賴包和一個嵌入的tomcat環境。後續的文章裡面我們會分析一下這裡的starter。

項目打包

??在項目路徑下運行maven命令mvn package,跑完之後就會在項目的target目錄下得到一個jar檔案,這就是項目包。跟以前的war包不太一樣的是,這裡的項目包格式是jar包。

??可以在通過java -jar 檔案名稱.jar 來運行這個jar檔案,然後在瀏覽器中訪問http://localhost:8080/ ,可以看到結果與之前在開發環境下是一樣的。

總結

??通過以上的步驟,完成了一個初步的最基礎的Spring Boot的項目的建立和運行,後續我會通過實踐的方式來分析一下Spring Boot的一些常用功能和特性,並按照我自己的理解來分析一下Spring Boot的運行原理。

??參考:
??Spring Boot Reference Guide

歡迎關注我的公眾號:

Spring Boot實踐教程(一):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.