1. 程式人生 > >SpringBoot快速入門+進階實踐

SpringBoot快速入門+進階實踐

一、什麼是SpringBoot?

  Spring Boot 是一個輕量級框架,可以完成基於 Spring 的應用程式的大部分配置工作。在本教程中,將學習如何使用 Spring Boot 的 starter、特性和可執行 JAR 檔案結構,快速建立能直接執行的基於 Spring 的應用程式。

  前提條件
    • JDK 8 
    • Eclipse IDE 
    • Apache Maven 

  Spring Boot 的目的是提供一組工具,以便快速構建容易配置的 Spring 應用程式。

  官方介紹:Spring Boot 使您能輕鬆地建立獨立的、生產級的、基於 Spring 且能直接執行的應用程式。我們對 Spring 平臺和第三方庫有自己的看法,所以您從一開始只會遇到極少的麻煩。

 

  對於SpringBoot:

  首先,它很有主見
  Spring Boot 擁有觀點。換句話說,Spring Boot 擁有合理的預設值,所以您可以使用這些常用值快速構建應用程式。
  例如,Tomcat 是一個非常流行的 Web 容器。預設情況下,Spring Boot Web 應用程式使用了一個嵌入式 Tomcat 容器。
  其次,它可以自定義
  如果無法改變其想法,具有主見的框架就不是很好的框架。您可以根據自己的喜好輕鬆地自定義 Spring Boot 應用程式,無論是在進行初始配置時還是在開發週期的後期階段。

 二、HelloWorld快速入門

  1、先看下專案概況

    

 

   2、Maven專案構建

  

  3、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.spring</groupId> <artifactId>HelloBootDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- Spring Boot 啟動父依賴 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- 配置熱部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <!-- Spring Boot web依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>

  4、建立啟動類  Application

package com.spring.demo;

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);
    }
}

  5、建立控制類  HelloWorldController

package com.spring.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {
    @Autowired
    private Environment env;

    @RequestMapping("/info")
    public String info() {
        return "Hello   World !! " + env.getProperty("url");
    }
}

  4、自定義配置檔案  application.properties

server.port=8088
url=http://www.helloworld.com

  5、啟動驗證

  直接在Application類中Run As 選擇Java Application 即可啟動SpringBoot

啟動成功後控制檯會輸出: 

之後在瀏覽器中輸入http://localhost:8088/info就能看到輸出

  6、熱部署

  我們在開發中反覆修改類、頁面等資源,每次修改後都是需要重新啟動才生效,這樣每次啟動都很麻煩,浪費了大量的時間,能不能在我修改程式碼後不重啟就能生效呢?可以,在pom.xml中新增如下配置就可以實現這樣的功能,我們稱之為熱部署。

<!-- 配置熱部署 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

 

 三、SpringBoot進階

  SpringBoot整合Dubbo

  SpringBoot整合ActiveMQ