1. 程式人生 > >Spring Boot 2.0.0.M3使用案例,案例配置,常用命令,註解介紹,熱部署

Spring Boot 2.0.0.M3使用案例,案例配置,常用命令,註解介紹,熱部署

1.系統需求

Spring Boot 2.0.0.M3需要Java8 和 Spring 5.0.0.RC3或者更高版本。指定的支援的編譯工具是Maven 3.2+和Gradle 3(3.4 或 更高版本)

2.建立一個新專案

mvn archetype:generate -DgroupId=com.example -DartifactId=myproject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

最終生成的專案結構如下:
這裡寫圖片描述

3.編寫一個簡單的pom檔案

<?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.example</groupId> <artifactId
>
myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.M3</version
>
</parent> <!-- Add typical dependencies for a web application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!-- Package as an executable jar :為了建立一個可執行的jar,需要新增spring-boot-maven-plugin到我們的pom檔案--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <!-- Add Spring repositories --> <!-- (you don't need this if you are using a .RELEASE version) --> <repositories> <repository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> <snapshots><enabled>true</enabled></snapshots> </repository> <repository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories> </project>

將專案匯入進Eclipse中,專案結構如下:
這裡寫圖片描述

4.編寫Example程式碼

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 快速建立一個maven工程的方式:
 * mvn archetype:generate -DgroupId=com.example -DartifactId=myproject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
 * 
 * 
 * @RestController              :告訴Spring渲染這些結果的字串給呼叫者
 * @EnableAutoConfiguration     :通過這個命令假設你部署一個web應用,並且為你設定了spring的引數配置
 */
@RestController
@EnableAutoConfiguration
public class Example {

    @RequestMapping("/")
    String home() {
        return "Hello Word!";
    }

    /**
     * 除了在Eclipse中右鍵執行run,還可以使用命令:mvn spring-boot:run
     * 
     * 執行之後在瀏覽器中輸入:http://localhost:8080/。最後發現這兩種的結果一樣。
     * 
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Example.class,args);
    }
}

最後在瀏覽器中看到的內容如下:這裡寫圖片描述

5.其它命令列

1、通過  mvn dependency:tree 這個命令可以檢視當前spring-boot程式依賴哪些jar包,並列印成樹形的列表。
2、將程式打成一個jar包,使用的外掛是:
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

接著執行命令:mvn package,檢視target目錄,可以看到myproject-0.0.1-SNAPSHOT.jar這個jar包。
這個檔案大約在10M左右,如果你想瞥一下這個包裡面的東西。你可以使用jar tvf命令。
E:\workspace\springboot\springbootdemo>jar tvf target\myproject-0.0.1-SNAPSHOT.jar
     0 Sat Sep 02 00:42:06 GMT+08:00 2017 META-INF/
   518 Sat Sep 02 00:42:06 GMT+08:00 2017 META-INF/MANIFEST.MF
     0 Sat Sep 02 00:42:06 GMT+08:00 2017 BOOT-INF/
     0 Sat Sep 02 00:42:06 GMT+08:00 2017 BOOT-INF/classes/
     0 Fri Sep 01 23:56:26 GMT+08:00 2017 BOOT-INF/classes/com/
     0 Fri Sep 01 23:56:26 GMT+08:00 2017 BOOT-INF/classes/com/example/
     0 Fri Sep 01 23:56:26 GMT+08:00 2017 META-INF/maven/
     0 Fri Sep 01 23:56:26 GMT+08:00 2017 META-INF/maven/com.example/
     0 Fri Sep 01 23:56:26 GMT+08:00 2017 META-INF/maven/com.example/myproject/
   979 Sat Sep 02 00:27:46 GMT+08:00 2017 BOOT-INF/classes/com/example/Example.class
  1965 Sat Sep 02 00:38:04 GMT+08:00 2017 META-INF/maven/com.example/myproject/pom.xml

你還可以看到一個更小的檔案,這個檔案是在target目錄資料夾下的:myproject-0.0.1-SNAPSHOT.jar.original
這個檔案是spring-boot在它之前重新打包後的內容。

如果想執行這個應用程式,使用java -jar命令列:java -jar target\myproject-0.0.1-SNAPSHOT.jar
看到的效果如下:
E:\workspace\springboot\springbootdemo>java -jar target\myproject-0.0.1-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::             (v2.0.0.M3)

6 使用@SpringBootApplication註解

 @SpringBootApplication等價:@Configuration @EnableAutoConfiguration @ComponentScan

程式碼內容如下:

package com.example.myproject;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 等價 @Configuration @EnableAutoConfiguration @ComponentScan
 * @author toto
 *
 */
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

上面的程式碼等價:

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

7、SpringBoot還可以執行帶有遠端除錯支援的打包應用程式

$ java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n -jar target/myproject-0.0.1-SNAPSHOT.jar

8、SpringBoot執行應用

mvn spring-boot:run 這個命令可以直接執行springboot應用

如果想要使用作業系統變數,可以類似下面的方式使用:export MAVEN_OPTS=-Xmx1024m

9、SpringBoot熱部署

SpringBoot還要有一個額外的部署工具集,讓熱部署體驗更加舒服,減少除錯時間,在這個過程中需要的外掛是:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

如果想讓重啟功能完全失效(這樣在修改完內容之後,不會立即顯示出效果),可以在SpringApplication.run(Example.class,args);下配置:

//System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(Example.class,args);