天天看點

Maven快速使用教程(二) spring boot 項目建構

1. 使用Maven來建構Spring boot項目

通路spring boot的官網。http://projects.spring.io/spring-boot/ 選擇目前最新穩定版本,目前的最新穩定版本是1.4.0 。找到Quick Start部分,如下圖所示:

Maven快速使用教程(二) spring boot 項目建構

2. 建立一個pro-springboot的Maven項目:

修改pom.xml,添加官網quick start建議添加的内容:

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

需要介紹2個标簽,分别是<parent>和<exclusion>。

<parent>标簽,表示要引用另外一個項目的pom.xml。表示這個項目依賴于parent标簽内的項目。

下面修改pom檔案,并開發測試程式。

pom.xml

<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.vip</groupId>
  <artifactId>pro-springboot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>

  <name>pro-springboot</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  
  <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  
  
</project>      

SampleController.java

package com.vip.pro_springboot;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration
public class SampleController {

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

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

直接右鍵SampleController.java,“run as Java Application”即可,控制台出現下面的畫面:

Maven快速使用教程(二) spring boot 項目建構

在浏覽器輸入http://localhost:8080/ 出現下面結果:

Maven快速使用教程(二) spring boot 項目建構

3. 遇到的問題:

在上面的開發過程中,曾經遇到問題,Maven項目前有個“紅色感歎号”,Mave在下載下傳相關依賴時實際上有問題,到配置的m2的repository目錄删除相關檔案夾,再右鍵項目,Maven --> Update Project...重新下載下傳就行了。

在定位錯誤時,參看Eclipse的Problems标簽視圖,如下圖所示:

Maven快速使用教程(二) spring boot 項目建構

如果發現某個Maven下載下傳的jar包有問題,根據errors裡的提示,到相應的maven本地repository下,删除對應的目錄。

4. 嘗試修改應用的端口:

上邊的應用,已經能夠跑通了。但是,如果我們的8080端口已經被占用了的話,需要修改spring mvc占用的端口,該如何修改?

1.增加一個配置檔案:

首先,在src/main下添加一個檔案夾,叫resources,并把該檔案夾添加到類路徑。

2. 建立一個檔案(名為resources):

在這個檔案夾中建立檔案application.properties,并在裡邊輸入:

server.port=8089      

重新啟動後,端口号就改成8089了。隻能通過該IP通路:http://localhost:8089/。

5. Maven常用标簽總結:

1.<scope>标簽:

scope是作用域,依賴的作用域。可以在測試、編譯、運作、打包等等情況下依賴進來。預設是compile作用域。

2.<exclusion>标簽:

排除依賴。

3.不可以循環依賴。

6. maven的配置:

在搭建自己的maven的環境過程中,對2個地方進行了特殊設定,一個是maven預設使用的jdk的版本,一個是本地倉庫的位置(從中央倉庫将jar包等下載下傳到哪個地方),這2個地方都是在$M2_HOME/conf/setting.xml中配置的。

1.本地倉庫位置:

<localRepository>D:\java_workdir\.m2\repository</localRepository>      
<profile>  
    <id>jdk18</id>  
     <activation>  
          <activeByDefault>true</activeByDefault>  
          <jdk>1.8</jdk>  
     </activation>  
     <properties>  
          <maven.compiler.source>1.8</maven.compiler.source>  
          <maven.compiler.target>1.8</maven.compiler.target>  
          <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>  
     </properties>   
</profile>