spring boot web 入門


① 新建一個maven項目。

② pom中parent設為 spring-boot-starter-parent 。建議使用最新的 RELEASE 版本。否則可能需要設置 <repositories/> <pluginRepositories/> 。

③ 添加應用需要的starter模塊,作為示例,我們僅添加web starter模塊。

  這里需要解釋下starter模塊,簡單的說,就是一系列的依賴包組合。例如web starter模塊,就是包含了Spring Boot預定義的一些Web開發的常用依賴:

○ spring-web, spring-webmvc ================>>Spring WebMvc框架
○ tomcat-embed-* ================>> 內嵌Tomcat容器
○ jackson ================>> 處理json數據
○ spring-* ================>> Spring框架
○ spring-boot-autoconfigure ================>> Spring Boot提供的自動配置功能

換句話說,當你添加了相應的starter模塊,就相當於添加了相應的所有必須的依賴包。

<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>

 

 

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 用於自動配置。簡單的說,它會根據你的pom配置(實際上應該是根據具體的依賴)來判斷這是一個什么應用,並創建相應的環境。

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

 

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

  1. 創建一個合適的ApplicationContext實例 (取決於classpath)。
  2. 注冊一個CommandLinePropertySource,以便將命令行參數作為Spring properties。
  3. 刷新application context,加載所有單例beans。
  4. 激活所有CommandLineRunner beans。

默認,直接使用SpringApplication 的靜態方法run()即可。但也可以創建實例,並自行配置需要的設置。

默認訪問地址: http://localhost:8080/

按照之前的web項目習慣,你可能會問,怎么沒有項目路徑?

這就是Spring Boot的默認設置了,將項目路徑直接設為根路徑。

當然,我們也可以設置自己的項目路徑 -- 在classpath下的 application.properties 或者 application.yaml 文件中設置即可。

# application.properties
# Server settings (ServerProperties)
server.port=8080
server.address=127.0.0.1
#server.sessionTimeout=30
server.contextPath=/aaa

# Tomcat specifics
#server.tomcat.accessLogEnabled=false
server.tomcat.protocolHeader=x-forwarded-proto
server.tomcat.remoteIpHeader=x-forwarded-for
server.tomcat.basedir=
server.tomcat.backgroundProcessorDelay=30

上面, server.contextPath=/aaa 就是設置了項目路徑。所以現在需要訪問 http://localhost:8080/aaa/ 才行。

 

分析

OK,當目前為止,已經成功運行並訪問了一個 SpringMVC 應用。簡單的不能再簡單了!

再來看一下啟動時的信息:

第 9 行,啟動SampleController。
第10行,查找active profile,無,設為default。
第11行,刷新上下文。
第12行,初始化tomcat,設置端口8080,設置訪問方式為http。
第13行,啟動tomcat服務。
第14行,啟動Servlet引擎。
第15行,Spring內嵌的WebApplicationContext 初始化開始。
第16行,Spring內嵌的WebApplicationContext 初始化完成。
第17行,映射servlet,將 dispatcherServlet 映射到 [/] 。
第18行,映射filter,將 characterEncodingFilter 映射到 [/*] 。
第19行,映射filter,將 hiddenHttpMethodFilter 映射到 [/*] 。
第20行,映射filter,將 httpPutFormContentFilter 映射到 [/*] 。
第21行,映射filter,將 requestContextFilter 映射到 [/*] 。
第22行,查找 @ControllerAdvice。
第23行,映射路徑 "{[/]}" 到 cn.larry.spring.controller.SampleController.home()。
第24行,映射路徑 "{[/error]}" 到 org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)。
第25行,映射路徑 "{[/error],produces=[text/html]}" 到 org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)。
第26行,略。 第27行,略。 第28行,略。 第29行,略。
第30行,tomcat啟動完畢。
第31行,SampleController啟動耗費的時間。
第32行,初始化 dispatcherServlet 。
第33行,dispatcherServlet 的初始化已啟動。
第34行,dispatcherServlet 的初始化已完成。
第35行,收到shutdown關閉請求。
第36行,關閉AnnotationConfigEmbeddedWebApplicationContext。
第37行,略。

從上面的啟動信息中可以明顯看到SpringMVC的加載過程,特別需要注意的是這種默認方式下加載的幾個 filter 。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM