前言
我們知道spring Boot 是 Spring 産品中一個新的子項目,緻力于簡便快捷地搭建基于 Spring 的獨立可運作的應用。大多數的 Spring Boot 應用隻需要非常少的 Spring 配置,并且這些都統一配置在application.yml中,極好的解決了各種配置檔案的困擾。
說到這裡,如果你覺得看英文文檔很吃力,推薦一本老師推薦的書《JavaEE開發的颠覆者:Spring Boot實戰》,這本書先是介紹Spring主流的一些架構,然後分别demo講解Spring boot的各個依賴庫,每個都不算太深,可以算是針對官網英文導讀的一次翻譯實作,對于入門我感覺是夠了,畢竟我就是這樣過來的。至于學習的深入,我個人是主張實戰出真知,是以入門了就拿到項目中多用,這是個要長期總結的過程。
Spring Boot Starter 依賴庫
上篇文章提過Spring Boot對Maven及Gradle等建構工具支援力度非常大。其内置一個’Starter POM’,對項目建構進行了高度封裝,最大化簡化項目建構的配置。作為一個微服務架構,Boot的很大一部分價值在于它能夠無縫地為基于Maven和Gradle的項目提供各種建構工具。通過使用Spring Boot插件,就能夠利用該架構的能力,将項目打包為一個輕量級的、可運作的部署包,而除此之外幾乎不需要進行任何額外的配置。
作為一個架構,SpringBoot中内建了一些聚合子產品,通常稱為“啟動者”。這些啟動子產品中是一些類庫的已知的、良好的、具備互操作性的版本的組合,這些類庫能夠為應用程式提供某些方面的功能。Boot能夠通過應用程式的配置對這些類庫的進行設定,這也為整個開發周期中帶來了配置勝于約定的便利性,達到開箱即用的微服務效果。下面就列出幾個經典的Spring Boot Starter pom依賴庫:
- spring-boot-starter:Spring Boot核心Starter,包含自動配置、日志、yaml配置檔案的支援
- spring-boot-starter-amqb:使用spring-rabbit來支援AMQP
- spring-boot-starter-web:支援全棧web開發,裡面包括了Tomcat和Spring-webmvc。
- spring-boot-starter-data-jpa:對JPA的支援,包含spring-data-jpa、spring-orm和Hibernate
- spring-boot-starter-thymeleaf:對Thymeleaf模版引擎的支援,包含于Spring整合的配置
- spring-boot-starter-ws: 提供對Spring Web Services的支援
- spring-boot-starter-cloud-connectors:對雲平台(Cloud Foundry、Heroku)提供的服務簡化連接配接方式
- spring-boot-starter-test:提供對常用測試架構的支援,包括JUnit,Hamcrest以及Mockito等。
- spring-boot-starter-actuator:支援産品環境下的一些功能,比如管理應用、名額度量及監控等。
- spring-boot-starter-jetty:支援jetty容器。
- spring-boot-starter-log4j:引入預設的log架構(logback)
SpringBoot項目搭建過程
在官網導讀,每個demo除了講解怎麼搭建外,還提供了GitHub的下載下傳連接配接,支援直接clone運作體驗。因為Spring Boot是新寵,它的JDK官方要求1.8以上版本,同時,Maven要3.0+或者Gradle2.3+,其他IDE沒有限制,下面說下應用大緻實作步驟,隻是步驟,以Maven為例:
1、建立項目,添加依賴包。
一般情況下可以通過IDE建立Maven項目,自動生成各種路徑的檔案夾,當然也可以指令行(mkdir -p src/main/Java/hello)手動一個個建立,然後修改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>
- <!-- spring boot基本環境 -->
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.3.1.RELEASE</version>
- </parent>
- <groupId>spring.boot</groupId>
- <artifactId>cloud-simple-helloword</artifactId>
- <version>0.0.1</version>
- <packaging>jar</packaging>
- <name>cloud-simple-helloword</name>
- <dependencies>
- <!--web應用基本環境配置 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
2、編寫控制器和Make the application executable
- 控制器controller主要用于響應使用者的情況,由注解@RestController指定;同時類或類下子方法可以通過注解@RequestMapping("/xxx")指定請求路徑。
- src/main/java/hello/GreetingController.java
- package hello;
- import java.util.concurrent.atomic.AtomicLong;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- public class GreetingController {
- private static final String template = "Hello, %s!";
- private final AtomicLong counter = new AtomicLong();
- @RequestMapping("/greeting")
- public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
- return new Greeting(counter.incrementAndGet(),
- String.format(template, name));
- }
- }
2、應用執行器由注解@SpringBootApplication實作,這個注解預設是內建了@Configuration、@EnableAutoConfiguration、@EnableWebMvc、@ComponentScan四個注解的功能,每個的功能從英文翻譯上就能了解。
- src/main/java/hello/Application.java
- 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);
3、部署spring boot應用
要部署運作spring boot應用,首選要打包spring boot應用,你在pom檔案中看到的spring-boot-maven-plugin插件就是打包spring boot應用的。除了IDE的main Run方法,啟動運作應用還有兩種方式,一種是在工程目錄執行mvn spring-boot:run啟動,另一種則是先打成jar包,然後運作以Java方式運作jar,操作如下:
- 1)mvn clean package
- 2)java -jar target/gs-rest-service-0.1.0.jar
對應的Gradle依賴執行:./gradlew bootRun 另一種打jar操作:
- 1)./gradlew build
- 2)java -jar build/libs/gs-rest-service-0.1.0.jar
注意Maven和Gradle生成的jar路徑不同。如此,你就看到一個基于jar包的web應用啟動了。
4、監控
Spring boot提供的一些開箱即用的應用非常容易使用,比如監控,你隻需要在pom檔案中引入:
- <dependency>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
- {"status":"UP","diskSpace":{"status":"UP","total":161067397120,"free":91618398208,"threshold":10485760}}