1. 程式人生 > >springboot2入門(1-第一個應用)

springboot2入門(1-第一個應用)

pom.xml

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.BUILD-SNAPSHOT</version>
    </parent>
  • 這個starter提供了一些有用的maven預設值,但不包含任何依賴庫。
  • 提供了依賴管理功能,讓我們可以不用指定依賴庫的版本。
  • 其他的starter提供的是在開發某個具體應用的時候很可能需要的依賴。
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
  • 我們要開發一個web應用,所以添加了這個starter
    。可以看到,沒有指定version,因為parent為我們預設指定了合適的version。如果需要特定版本,也開始在這裡加上version。
  • 新增依賴:spring boot 自身和tomcat web server。

controller

// file:src/main/java/Example.java
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration public class Example { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); } }
  • @RestController 註解這是一個web controller,能夠處理web請求。Rest:處理結果當作字串返回給web請求。
  • @RequestMapping 註解路由資訊,當接收到對這個路徑的請求的時候,呼叫這個方法。這兩個註解都是spring mvc元件的註解。
  • @EnableAutoConfiguration 告訴spring根據我們新增的依賴資訊,自動推測配置資訊。而spring-boot-starter-web添加了tomcat和spring mvc依賴,所以它會推測這是一個web應用,並進行配置。
  • SpringApplication run方法啟動應用:自動配置的tomcat。

執行應用

  • spring-boot-starter-parent POM中定義了goalrun,在project根目錄下,可以這樣啟動應用:
    mvn spring-boot:run

建立可執行的jar

需要新增外掛來打包。
在前面提到的pom.xml中新增以下內容:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

spring-boot-starter-parent 中包含了用於goal repackageexecutions 配置。如果沒有spring-boot-starter-parent POM,就需要我們手動定義這個配置,比如這樣:

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.0.BUILD-SNAPSHOT</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

注意 這裡的version
上面這個例子,在maven 的packagephaserepackage所有依賴包(* 包括 provided 依賴包)。

可以通過設定,排除某些依賴包,比如這樣:

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.0.BUILD-SNAPSHOT</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>com.foo</groupId>
                            <artifactId>bar</artifactId>
                        </exclude>
                    </excludes>
                    <excludeArtifactIds>my-lib,another-lib</excludeArtifactIds>
                    <excludeGroupIds>com.foo</excludeGroupIds>
                </configuration>
            </plugin>

使用 java -jar啟動上面打包後的應用。

以上