SpingBoot學習(一)

1、概述

  Spring Boot是爲了簡化Spring應用的建立、運行、調試、部署等而出現的,使用它能夠作到專一於Spring應用的開發,而無需過多關注XML的配置。html

簡單來講,它提供了一堆依賴打包,並已經按照使用習慣解決了依賴問題---習慣大於約定。java

   Spring Boot默認使用tomcat做爲服務器,使用logback提供日誌記錄。web

2、使用

  一、Spring Boot提供了一系列的依賴包,因此須要構建工具的支持:maven 或 gradle。spring

  二、pom.xml  apache

<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>cn.larry.spring</groupId>
    <artifactId>larry-spring-demo4</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>

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

  說明:   一般讓你的Maven POM文件繼承 spring-boot-starter-parent,並聲明一個或多個 Starter POMs依賴便可。瀏覽器

  一、那spring-boot-starter-parent是什麼?tomcat

  原文:https://blog.csdn.net/qq_35981283/article/details/77802771 服務器

  Maven的用戶能夠經過繼承spring-boot-starter-parent項目來得到一些合理的默認配置。這個parent提供瞭如下特性:mvc

  (1)默認使用Java 8
  (2)使用UTF-8編碼
  (3)一個引用管理的功能,在dependencies裏的部分配置能夠不用填寫version信息,這些version信息會從spring-boot-dependencies裏獲得繼承。
  (4)識別過來資源過濾(Sensible resource filtering.)
  (5)識別插件的配置(Sensible plugin configuration (exec plugin, surefire, Git commit ID, shade).)
  (6)可以識別application.properties和application.yml類型的文件,同時也能支持profile-specific類型的文件(如: application-foo.properties and application-foo.yml,這個功能能夠更好的配置不一樣生產環境下的配置文件)。
  (7)maven把默認的佔位符${…​}改成了@..@app

   二、starter模塊,簡單的說,就是一系列的依賴包組合。

     spring-boot-starter-web  包含了不少內容,spring-webmvc、spring-web、jackson、validation、tomcat、starter。

 基本上,若是沒有特別的須要,如今就能夠直接寫Controller了!!! 

package cn.larry.spring.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

  說明:這裏有兩個新東西:@EnableAutoConfiguration 和 SpringApplication 。

  @EnableAutoConfiguration 用於自動配置。簡單的說,它會根據你的pom配置(實際上應該是根據具體的依賴)來判斷這是一個什麼應用,並建立相應的環境。

  在上面這個例子中,@EnableAutoConfiguration 會判斷出這是一個web應用,因此會建立相應的web環境。

   SpringApplication 則是用於從main方法啓動Spring應用的類。默認,它會執行如下步驟:

  1. 建立一個合適的ApplicationContext實例 (取決於classpath)。
  2. 註冊一個CommandLinePropertySource,以便將命令行參數做爲Spring properties。
  3. 刷新application context,加載全部單例beans。
  4. 激活全部CommandLineRunner beans。

  如今,直接右鍵啓動main方法便可(能夠理解爲tomcat start)。

  而後控制檯輸出信息,到瀏覽器上訪問localhost://8080/----->Hello World!

 

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

相關文章
相關標籤/搜索