Spring Boot 微框架

Spring Boot 介绍

Spring 框架非常好用,但是会有大量的 XML 配置文件以及复杂的 Bean 依赖关系。而 Spring Boot 就是为此诞生的!

Spring Boot 则采用了“约定优先配置” (convention over configuration)的思想来摆脱 Spring 框架中各类繁复纷杂的配置。

Spring Boot 使开发独立的,产品级别的基于 Spring 的应用变得非常简单,你只需 “just run” 。 我们为 Spring 平台及第三方库提供开箱即用的设置,这样你就可以有条不紊地开始。多数 Spring Boot 应用需要很少的 Spring 配置。

简单的 Demo

Maven 中引入依赖

1
2
3
4
5
6
7
8
9
10
11
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

新建

这个类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
```
package hello;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration
public class SampleController {

@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}

public static void main(String[] args) throws Exception {
SpringApplication.run(SampleController.class, args);
}
}

然后启动 Main 函数,这个简单的 Demo 就启动起来了。

访问地址为:

1
2
3
4
5
6
7
8
9

## 自定义 Spring Boot
官方自定义 Spring 的链接 [http://start.spring.io/](http://start.spring.io/ "官方自定义 Spring")

界面截图:
[![Spring Boot 定制化](http://blog.qiji.tech/wp-content/uploads/2016/05/Spring-Boot-定制化.jpg)](http://blog.qiji.tech/wp-content/uploads/2016/05/Spring-Boot-定制化.jpg)

## 默认结构
[![Spring Boot 结构图](http://blog.qiji.tech/wp-content/uploads/2016/05/Spring-Boot-结构图.jpg)](http://blog.qiji.tech/wp-content/uploads/2016/05/Spring-Boot-结构图.jpg)

src/
main/
java/
Application.java (带 Main 函数的启动类 )
resources/
templates/
index.html (thymeleaf 模板文件的默认路径)
application.properties (默认配置文件)
banner (Spring Boot 的彩蛋,可以配置启动图案)
pom.xml

1
2
3
4
5
6

## 配合 thymeleaf 模板
thymeleaf 是个优秀的模板,美工可以静态效果,程序员可以看带数据的效果。
>Thymeleaf 最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个 Web 应用。

Maven 引入 thymeleaf 依赖


org.springframework.boot
spring-boot-starter-thymeleaf

1
2

在 @Controller 类中就直接返回 String 就行了,代码如下所示:

@Controller
public class IndexController {

@Autowired
private FArticleRepository fArticleRepository;

@RequestMapping(method = GET)
public String index(Model model) {
    model.addAttribute("fArticles",fArticleRepository.findAll());
    return "index";
}

}

1
2

此处返回 index ,对应的就是 ```resources/templates/index.html

thymeleaf 中基本表达式

页瘦需要引入

1
2
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">

使用范例如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<table>
<thead>
<tr>
<th th:text="#{msgs.headers.name}">Name</th>
<th th:text="#{msgs.headers.price}">Price</th>
</tr>
</thead>
<tbody>
<tr th:each="prod: ${allProducts}">
<td th:text="${prod.name}">Oranges</td>
<td th:text="${#numbers.formatDecimal(prod.price, 1, 2)}">0.99</td>
</tr>
</tbody>
</table>

通过标签中的

属性来填充该标签的一段内容,通过 ```${}``` 等表达式进行渲染。
1
2
3
4
5
6
7
8
9
通过标签中的 ```th:each``` 属性来进行遍历,上述的 prod 就是循环变量。
更多更详细的使用方式,自行 Google。


## spring-boot-devtools 热加载工具

spring-boot-devtools 是一个为开发者服务的一个模块,其中最重要的功能就是自动应用代码更改到最新的 App 上面去。我们修改代码或者配置文件的时候,应用可以自动重启。这有助于提高我们的开发效率。

推荐 Spring Boot 版本为 1.3.5.RELEASE,Maven 引入


org.springframework.boot
spring-boot-devtools
true

```

然后就可以愉快的开发啦~!

参考资料