Spring 框架對於不少 Java 開發人員來講都不陌生。Spring 框架包含幾十個不一樣的子項目,涵蓋應用開發的不一樣方面。如此多的子項目和組件,一方面方便了開發人員的使用,另一個方面也帶來了使用方面的問題。每一個子項目都有必定的學習曲線。開發人員須要瞭解這些子項目和組件的具體細節,才能知道如何把這些子項目整合起來造成一個完整的解決方案。在如何使用這些組件上,並無相關的最佳實踐提供指導。對於新接觸 Spring 框架的開發人員來講,並不知道如何更好的使用這些組件。Spring 框架的另一個常見問題是要快速建立一個能夠運行的應用比較麻煩。Spring Boot 是 Spring 框架的一個新的子項目,用於建立 Spring 4.0 項目。它能夠自動配置 Spring 的各類組件,並不依賴代碼生成和 XML 配置文件。Spring Boot 也提供了對於常見場景的推薦組件配置。Spring Boot 能夠大大提高使用 Spring 框架時的開發效率,對於快速開發一個可運行的非大型的項目很是合適。java
從 Spring Boot 項目名稱中的 Boot 能夠看出來,Spring Boot 的做用在於建立和啓動新的基於 Spring 框架的項目。它的目的是幫助開發人員很容易的建立出獨立運行和產品級別的基於 Spring 框架的應用。Spring Boot 會選擇最適合的 Spring 子項目和第三方開源庫進行整合。大部分 Spring Boot 應用只須要很是少的配置就能夠快速運行起來。git
Spring Boot 包含的特性以下:github
* 建立能夠獨立運行的 Spring 應用。 * 直接嵌入 Tomcat 或 Jetty 服務器,不須要部署 WAR 文件。 * 儘量的根據項目依賴來自動配置 Spring 框架。 * 不須要傳統的Sring項目繁多的 XML 配置文件。
經過 Spring Boot,建立新的 Spring 應用變得很是容易,並且建立出的 Spring 應用符合通用的最佳實踐。web
官方文檔上有這樣一段介紹:spring
Learn what you can do with Spring Boot ?
Spring Boot offers a fast way to build applications. It looks at your classpath and at beans you have configured, makes reasonable assumptions about what you’re missing, and adds it. With Spring Boot you can focus more on business features and less on infrastructure.
For example:數組
- Got Spring MVC? There are several specific beans you almost always need, and Spring Boot adds them automatically. A Spring MVC app also needs a servlet container, so Spring Boot automatically configures embedded Tomcat.
- Got Jetty? If so, you probably do NOT want Tomcat, but instead embedded Jetty. Spring Boot handles that for you.
- Got Thymeleaf? There are a few beans that must always be added to your application context; Spring Boot adds them for you.
These are just a few examples of the automatic configuration Spring Boot provides. At the same time, Spring Boot doesn’t get in your way. For example, if Thymeleaf is on your path, Spring Boot adds a SpringTemplateEngine to your application context automatically. But if you define your own SpringTemplateEngine with your own settings, then Spring Boot won’t add one. This leaves you in control with little effort on your part.
Spring Boot doesn't generate code or make edits to your files. Instead, when you start up your application, Spring Boot dynamically wires up beans and settings and applies them to your application context.springbootSpring Boot doesn't generate code or make edits to your files. Instead, when you start up your application, Spring Boot dynamically wires up beans and settings and applies them to your application context.服務器
1.在IDEA中建立一個項目,和一個helloworld模塊:mvc
目錄結構如圖:app
在pom.xml文件中引入Spring-boot的依賴:
<!-- SpringBoot 應用都要繼承 spring-boot-starter-parent --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.RELEASE</version> </parent> <modelVersion>4.0.0</modelVersion> <packaging>jar</packaging> <!-- SpringBoot Web 應用須要添加以下依賴 --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!-- SpringBoot 的maven管理插件 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.lzumetal.springboot.helloworld.Application</mainClass> </configuration> </plugin> </plugins> </build>
再建立兩個類:
第一個是Application.java,用來啓動SpringBoot應用:
package com.lzumtal.springboot.helloworld; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Aplication.class, args); } }
第二個類HelloController.java用來響應web請求:
package com.lzumetal.springboot.helloworld.controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping("/home") String home() { return "Hello World!"; } @RequestMapping("/hello/{name}") String hello(@PathVariable("name") String name) { return "hello " + name + "!!!"; } }
2.運行
運行Aplication中的main()方法,控制檯打印啓動信息:
在瀏覽中訪問:http://localhost:8080/home
訪問:http://localhost:8080/hello/spring-boot
這樣示例就成功運行了。
1.@SpringbootApplication
註解:
Spring Boot默認是掃描@SpringBootApplication
註解的類(即啓動類)的同包以及子包下的類。
使用@SpringbootApplication
註解 能夠解決根類或者配置類頭上註解過多的問題,一個@SpringbootApplication
至關於@Configuration
,@EnableAutoConfiguratio
n和@ComponentScan
並具備他們的默認屬性值
@SpringBootApplication is a convenience annotation that adds all of the following:
- @Configuration tags the class as a source of bean definitions for the application context.
- @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.
- Normally you would add @EnableWebMvc for a Spring MVC app, but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviors such as setting up a DispatcherServlet.
- @ComponentScan tells Spring to look for other components, configurations, and services in the hello package, allowing it to find the controllers.
2.@EnableAutoConfiguration
註解(開啓自動配置):@EnableAutoConfiguration
註解的做用在於讓 Spring Boot 根據應用所聲明的依賴來對 Spring 框架進行自動配置,這就減小了開發人員的工做量。
The second class-level annotation is @EnableAutoConfiguration. This annotation tells Spring Boot to 「guess」 how you will want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration will assume that you are developing a web application and setup Spring accordingly.
Starters and Auto-Configuration
Auto-configuration is designed to work well with 「Starters」, but the two concepts are not directly tied. You are free to pick-and-choose jar dependencies outside of the starters and Spring Boot will still do its best to auto-configure your application.
第二個類級別註解是@EnableAutoConfiguration
。這個註解告訴Spring Boot「猜想」將如何配置Spring,它是基於添加的jar依賴。 因爲spring-boot-starter-web添加了Tomcat和Spring MVC,所以自動配置將假設正在開發一個Web應用程序並相應地設置Spring。
啓動器和自動配置(Starters & Auto-Configuration):
自動配置旨在與「Starters」配合使用,但這兩個概念不直接綁定。能夠自由選擇和選擇起始者之外的jar依賴,Spring Boot仍將盡力自動配置應用程序。
3.main()方法
The main() method uses Spring Boot’s SpringApplication.run() method to launch an application. Did you notice that there wasn’t a single line of XML? No web.xml file either. This web application is 100% pure Java and you didn’t have to deal with configuring any plumbing or infrastructure.
「main」方法應用程序的最後一部分是主(main)方法。 這只是一個遵循Java約定的應用程序入口點的標準方法。main方法經過調用run來委託Spring Boot SpringApplication類。SpringApplication將引導應用程序,啓動Spring,從而啓動自動配置Tomcat Web服務器。須要傳遞Example.class做爲run方法的參數來告訴SpringApplication,這是主要的Spring組件。args數組也被傳遞以暴露任何命令行參數。
4.繼承 spring-boot-starter-parent
做用是Spring-Boot爲咱們自動添加相關的依賴,若是不想使用Spring Boot中的默認版本,能夠在pom.xml文件的properites標籤中指定咱們本身想要引入的版本,這樣就能夠覆蓋默認的版本。
好比想使用不一樣版本的Spring Data,具體以下:
<properties> <spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version> </properties>
在properties標籤中還能夠指定JDK編譯的版本和項目的編碼格式:
<properties> <!-- 指定項目編碼爲 UTF-8 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- 使用 java 1.8 --> <java.version>1.8</java.version> </properties>
5.spring-boot-maven-plugin 插件
能夠爲SpringBoog項目提供Maven的操做方式,Spring Boot Maven plugin的5個Goals
* spring-boot:repackage,默認goal。在mvn package以後,再次打包可執行的jar/war,同時保留mvn package生成的jar/war爲.origin * spring-boot:run,運行Spring Boot應用 * spring-boot:start,在mvn integration-test階段,進行Spring Boot應用生命週期的管理 * spring-boot:stop,在mvn integration-test階段,進行Spring Boot應用生命週期的管理 * spring-boot:build-info,生成Actuator使用的構建信息文件build-info.properties
這裏介紹一下 spring-boot:repackage 和 spring-boot:run 這兩個。spring-boot:repackage
:能夠將項目打包成fat jar(executable jar)後,命令是:mvm package spring-boot:repackage
,或者直接使用 mvm package
,(若是使用 mvn spring-boot:repackage
則會報錯)。以後咱們就能夠直接 經過 java -jar 的命令來啓動這個 SpringBoot 項目(試了一下即便沒有配置 mainClass 也是成功的)
mvn spring-boot:run
:在 pom.xml 文件所在的目錄下運行該命令便可啓動 SpringBoot 項目,和在 Application.java 中運行main()
方法的結果是同樣的
The Spring Boot Maven plugin provides many convenient features:
- It collects all the jars on the classpath and builds a single, runnable "über-jar", which makes it more convenient to execute and transport your service.
- It searches for the public static void main() method to flag as a runnable class.
- It provides a built-in dependency resolver that sets the version number to match Spring Boot dependencies. You can override any version you wish, but it will default to Boot’s chosen set of versions.
本文示例代碼已上傳到github: https://github.com/liaosilzu2...