Spring Boot主要用於建立和啓動新的基於Spring框架的項目。開發人員能夠藉助它很是方便的建立出基於spring框架的應用,而且在spring boot的應用中,只須要配置少許的配置文件便可。
java
Spring Boot主要特性通常包含以下:web
建立獨立的spring應用spring
Spring Boot內嵌了tomcat或者jetty服務器,無需部署war包tomcat
提供推薦的pom依賴以簡化maven配置服務器
儘量的根據項目依賴自動配置spring框架app
自帶生產環境的監控功能框架
簡化代碼和xml配置maven
接下來,咱們開始一個Spring Boot的hello world演示,首先建立一個maven項目,並在pom文件中加入以下配置spring-boot
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.0.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--添加依賴主要適用於生產環境的功能,如性能指標和監測等功能。--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
而後建立一個Application類來處理用戶請求性能
package com.test.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.actuate.metrics.CounterService; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * Created by xiaojun.zhang on 2016/3/24. */ @RestController @EnableAutoConfiguration public class Application { @Autowired private CounterService counterService; @RequestMapping("/home") public String home(@RequestParam String id,@RequestParam int age){ System.out.println(id); System.out.println(age); counterService.increment("myAppCounter"); return "welcome to this page!"; } public static void main(String[] args){ SpringApplication.run(Application.class,args); } }
這樣,直接運行Application 類,便可完成項目啓動,訪問http://localhost:8080/home?id=1&age=12 可看到頁面響應信息,這裏啓動一個web應用並不須要額外安裝tomcat服務器,也不須要部署war包
Spring Boot的自動配置功能@EnableAutoConfiguration 是沒有侵入性的,咱們也能夠自定義其餘的bean來取代自動配置的默認功能