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来取代自动配置的默认功能