写在前面的话:
发表此系列博客,纯属个人学习记录,其中所用到的知识点,可能与大部分其他类型博客稍有异处,都为个人学习观点,可为借鉴,如有不正确之处,也请指教,刚刚注册博客,写作经验不足,语言组织可能并不得体,描述也可能不会那么生动, 多以代码示例,不喜请放开喷。
官网介绍:
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
通过Spring boot ,你可以很容易的创建单独的应用程序,并在在spring的基础之上直接运行,平台发布了完整的文档,可以通过其快速的开始使用,大多数的Spring boot应用只需要很少的配置即可.
特点(摘自官网):
1.可以创建独立的应用
2.直接嵌入Tomcat,Jetty,Undertow,无需打包war文件
3.使用只需要非常简单的Maven配置
4.自动注入配置
5.提供生成环境的健康检查配置等
6.几乎无需代码生成,无XML配置
1.创建Maven工程,项目结构如下:
上面已经提到可以是一个单独的应用,并且嵌入tomcat等,所以并不需要web工程.
2.pom.xml (version:1.38)
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.vic</groupId>
<artifactId>vic</artifactId>
<version>0.1.0</version>
<properties>
<java.version>1.7</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.8.RELEASE</version>
</parent>
<dependencies>
<!-- spring boot -->
<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>
3.com.vic包下新建Application.java
package com.vic;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
public class Application {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
4.运行main函数,浏览器输入localhost:8080,显示Hello world!
解释:
@RestController:表示当前是一个rest风格Controller,相当于spring mvc中的@Controller 和@ResponseBody结合
@EnableAutoConfiguration:启动启用配置,如果pom.xml中依赖于spring-boot-starter-web,那么将以web方式启动,并且自动嵌入tomcat和spring mvc,此注解为web程序的依据, 既然嵌入spring mvc,那么spring mvc的注解在这里都是可以通用的。
@RequestMapping :请求路径。
SpringApplication.run(Application.calss,args):启动spring boot 应用。