[TOC]java
對於作Java開發的程序員,Spring是一個繞不開的框架。如今幾乎全部的Java項目都會使用Spring做爲做爲容器。可是大概兩三年前有一個叫作Spring Boot的「傢伙」橫空出世,其風頭好像一時蓋過了Spring。全部的基於Spring開發的新項目都轉向使用Spring Boot進行開發了。那麼Spring Boot到底是一個什麼框架?和Spring又有什麼關係?本篇博客就來對Spring Boot作一個簡單介紹。程序員
咱們先來看下Spring Boot官網對這個框架的介紹:web
Spring Boot makes it easy to create stand-alone, production-grade Spring-based Applications that you can run. We take an opinionated view of the Spring platform and third-party libraries, so that you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration. You can use Spring Boot to create Java applications that can be started by using java -jar or more traditional war deployments. We also provide a command line tool that runs 「spring scripts」. Our primary goals are: • Provide a radically faster and widely accessible getting-started experience for all Spring development. • Be opinionated out of the box but get out of the way quickly as requirements start to diverge from the defaults. • Provide a range of non-functional features that are common to large classes of projects (such as embedded servers, security, metrics, health checks, and externalized configuration). • Absolutely no code generation and no requirement for XML configuration.spring
上面介紹的大概意思是:使用Spring Boot能夠很是方便地建立生產級別的Spring應用;Spring Boot提供了不少默認配置,可是程序員也能夠很是方便的提供本身的配置;Spring Boot能夠大大減小甚至不使用傳統Spring項目中的XML配置文件。mvc
本身總結下:Spring Boot其實並非一個新的Spring框架,它進行了不少自動配置,目的是讓用戶能迅速開發基於Spring的應用,儘量減小繁瑣的Spring配置,提高開發者的開發體驗和開發效率。app
Spring Boot進行自動配置的原理是:根據開發者添加的jar包依賴(classpath中的類),自動配置相應的Bean,從而大大減小開發者手動的配置。好比說開發者添加了spring-boot-starter-web
這個依賴,那麼Spring Boot就認爲你是要開發Spring MVC應用,它會自動幫你配置DispatcherServlet、HandlerMapping和HandlerMethod等一系列Spring MVC的核心組件。開發者就不須要本身在進行配置了。框架
Spring Boot另一個方便的地方是內嵌了Servlet容器,咱們開發完以後能夠直接將應用打成一個可執行的Jar包,而不是打成war包再部署到本身安裝的Servlet容器中去。ide
下面咱們用一個Spring MVC應用的列子,看看Spring Boot到底能給咱們帶來哪些方便。spring-boot
咱們使用傳統的Spring開發一個Spring MVC項目大概要作下面幾個步驟:學習
<mvc:default-servlet-handler/>
和<mvc:annotation-driven/>
等相關配置;對於新手來講,上面的流程步驟仍是不少的,頗有可能在某一步出錯。特別是好多剛剛接觸Spring的同窗對於第一步中要添加哪些依賴根本就「傻傻分不清楚」,比較影響學習的積極性。對於老手來講,上面的步驟可能都是一些重複性的操做,比較「浪費時間」。所以須要Spring Boot來解決這個問題。
使用Spring Boot開發Spring MVC咱們須要下面幾步:
添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
編寫主啓動類
編寫相關的Controller
直接打成可執行Jar包運行。
咱們能夠發現使用Spring Boot真的能夠減小不少流程,並且大大減小本身配置出錯的機率。
Spring Boot是一個可以幫助開發者迅速開發Spring應用框架。它大大減小了傳統Spring應用中的配置文件,提高開發效率和開發體驗。同時Spring Boot還提了內嵌式的Servlet容器,能夠將應用打成可執行Jar包直接運行。
一些問題
使用Spring Boot的確能夠提高開發效率。可是它封裝了太多細節,對於初學者來講Spring Boot也多是「噩夢」。出了問題不知從何查起。因此我以爲對於框架這種東西咱們仍是要知道他們的原理性的知識,這樣使用起來才能更加駕輕就熟。
若是你是剛剛接觸Spring Boot下面這些問題能夠過段時間再考慮,先將Spring Boot用熟練再說。但若是你已經使用Spring Boot有段時間了,下面這些問題仍是建議本身去琢磨琢磨:
固然上面只是我如今能想到的一些問題,本身在學習過程當中能夠去不斷髮現和總結。