SPRINGBOOT學習LESSON1:初識SRINGBOOT,ECLIPSE搭建HELLOWORLD

http://www.cnblogs.com/larryzeal/p/5765945.html

在閱讀很多資料後,對MAVEN,SPRINGBOOT有一個大概的瞭解,由於對SPRINGMVC MAVEN還不太熟悉,暫且將原理放下。
MAVEN相當於一個資源倉庫,根據POM(PROJECT OBJECT MODEL)文件去自動下載管理所需資源,相比於其他方式的優點在於集中管理,更加方便清晰。
SPRINGBOOT項目就依賴於MAVEN或GRADLE這種程序構建管理工具。

eclipse搭建springboot項目很簡單。
1.新建MAVEN項目
沒有特殊配置就勾選創建簡單的項目。可以省去選擇原型的步驟,對於HELLOWORLD就用簡單項目即可。然後輸入artifactId,就是項目名稱即可
在這裏插入圖片描述
在這裏插入圖片描述
新建成功後項目結構如下:
在這裏插入圖片描述
2.配置POM引入SPRINGBOOT框架
初始POM如下

<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.tpri.demo.springboot</groupId>
  <artifactId>helloworld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>helloworld</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

junit的依賴和默認的屬性因爲用不到可刪除,刪除後也要把項目自帶的JAVA代碼一同刪除。否則編譯錯誤。

POM標籤詳細介紹:
https://www.cnblogs.com/sharpest/p/7738444.html
properties標籤內可配置屬性,配置的屬性可以使用${}方式進行引用。
dependencies標籤內配置所有的依賴包,maven會根據配置的信息自動下載資源至本地。一般在用戶文件夾中的.m2文件內。具體位置可配置maven調整。
parent標籤可進行繼承,就像JAVA的類繼承一樣,可直接繼承已有項目的配置。
plugins標籤內配置使用的插件。
所以配置時簡單的配置
繼承spring-boot-starter-parent項目,引入spring-boot-starter,spring-boot-starter-web依賴,然後使用spring-boot-maven-plugin插件即可。
配置後POM如下。

    <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.tpri.demo.springboot</groupId>
    <artifactId>helloworld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>helloworld</name>
    <url>http://maven.apache.org</url>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

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

3.創建項目啓動入口及helloworldController
在src/main/java下新建包helloworld
然後新建項目啓動入口Application.java。不能把application.java建在默認包裏,否則可能啓動異常。
入口需要使用註解@SpringBootApplication

package helloworld;

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

@SpringBootApplication
public class Application {

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

然後新建helloworldController,和springmvc不同的就是controller使用的註解爲@RestController

package helloworld;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    /**
     * <B>方法名稱:</B>helloworld<BR>
     * <B>概要說明:</B><BR>
     * 
     * @param xxx TEST
     * @return String RET
     */
    @RequestMapping("/")
    public String hello(String xxx) {
        return xxx + "Greetings from Spring Boot!";
    }
}

完成後在項目上右鍵-MAVEN-UPDATE PROJECT項目將會自動完成資源的下載等步驟,只需要等完成後啓動項目即可。
啓動項目由兩種方法:
1.項目右鍵-RUN AS-JAVA APPLICATION-選擇helloworld包下的Application類啓動。
2.在Application類中,右鍵RUN AS - JAVA APPLICATION

後臺出現spring日誌圖標就正常啓動了。
在這裏插入圖片描述
瀏覽器中打開。localhost:8080/就可以看到項目了。之後開發就和springMVC沒有區別了。
在這裏插入圖片描述
如果想要更換端口,則需要在application.properties中進行配置。
簡單項目中沒有該文件。需要自行建立。
在src/main下建立資源目錄resources
然後新建文件application.proeprties 在這裏插入圖片描述
在文件中配置server.port=80即可,啓動可看後臺日誌中端口改變。
在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章