一. What: Spring Boot是什么? 以1.4.3.RELEASE为例,官方介绍为:http://docs.spring.io/spring-boot/docs/1.4.3.RELEASE/reference/html/getting-started-introducing-spring-boot.html 。
简单来说,使用Spring Boot能够非常方便,快速地开发一个基于Spring框架,可独立运行的应用程序。
具体来说,Spring Boot集成了一些在项目中常用的组件,比如:嵌入式服务器,安全等。而且在使用Spring Boot开发应用程序时,完全不需要XML配置。
二. Why: 为什么Spring Boot能如此易用? //TODO: 随后再详细介绍
三. How: 如何建立一个简单的Spring Boot应用程序?
环境要求: 1. JDK: Although you can use Spring Boot with Java 6 or 7, we generally recommend Java 8 if at all possible. 2. Maven: Spring Boot is compatible with Apache Maven 3.2 or above.
示例项目:
1. 新建一个maven项目,并在pom.xml中添加如下依赖配置:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5
6 <groupId>org.chench</groupId>
7 <artifactId>springboot-hellworld</artifactId>
8 <version>1.0.0</version>
9 <packaging>jar</packaging>
10
11 <name>springboot-hellworld</name>
12 <url>http://maven.apache.org</url>
13
14 <!-- 使用spring boot框架:声明spring boot为父依赖 -->
15 <parent>
16 <groupId>org.springframework.boot</groupId>
17 <artifactId>spring-boot-starter-parent</artifactId>
18 <version>1.4.3.RELEASE</version>
19 </parent>
20
21 <properties>
22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23 </properties>
24
25 <dependencies>
26 <!-- 开发web应用 -->
27 <dependency>
28 <groupId>org.springframework.boot</groupId>
29 <artifactId>spring-boot-starter-web</artifactId>
30 </dependency>
31
32 <dependency>
33 <groupId>junit</groupId>
34 <artifactId>junit</artifactId>
35 <scope>test</scope>
36 </dependency>
37 </dependencies>
38
39 <build>
40 <plugins>
41 <!-- 打包插件: 可将项目打包为独立执行的jar文件 -->
42 <plugin>
43 <groupId>org.springframework.boot</groupId>
44 <artifactId>spring-boot-maven-plugin</artifactId>
45 </plugin>
46 </plugins>
47 </build>
48
49 </project>
2. 新建HelloWordApplication:
1 @RestController
2 @EnableAutoConfiguration
3 public class HelloWordApplication {
4
5 @RequestMapping("/")
6 public String helloworld() {
7 return "hello,world";
8 }
9
10 public static void main(String[] args) {
11 SpringApplication.run(HelloWordApplication.class, args);
12 }
13 }
3.运行HelloWordApplication: 如果是在eclipse等开发环境中,直接运行: "Run As" -> "Java Application" 。 或者,直接将项目打包为可独立执行的jar文件: mvn clean package,将生成文件: springboot-hellworld-version.jar 。 执行如下命令启动项目: java -jar springboot-hellworld-version.jar
启动之后,在浏览器地址栏输入: http://localhost:8080/
完毕!