Spring boot 教程
目录
Spring boot 教程....................................................................................................................................................................1
Spring boot helloworld ....................................................................................................................................................2
1
Spring boot 返回 json 数据 ............................................................................................................................................4
2
3
Spring boot 使用其他 json 转换框架 ...........................................................................................................................8
Spring boot 全局异常捕捉 .............................................................................................................................................9
4
Spring boot JPA 连接数据库........................................................................................................................................ 10
5
6
Spring boot 配置 JPA................................................................................................................................................... 11
Spring boot 整合 JPA 保存数据 ................................................................................................................................... 13
7
Spring boot 使用 JdbcTemplate 保存数据 ................................................................................................................. 15
8
9
Spring boot 常用配置 ...................................................................................................................................................18
Spring boot 静态资源处理 .......................................................................................................................................19
10
Srping boot 实现任务调度 ......................................................................................................................................21
11
12
Spring boot 普通类调用 Bean................................................................................................................................. 21
spring boot 使用模板引擎 .......................................................................................................................................25
13
Spring boot 集成 JSP.................................................................................................................................................28
14
15
Spring boot 集成 servlet.........................................................................................................................................33
Spring boot 集成 Fliter 和 Linstener ........................................................................................................................ 37
16
Spring boot 拦截器 HandlerInterceptor.................................................................................................................. 39
17
Spring boot 系统启动任务 CommandLineRunner ..................................................................................................42
18
19
Spring boot 集成 Junit 单元测试 .............................................................................................................................43
Spring boot 读取系统环境变量 ...............................................................................................................................45
20
Spring boot 使用自定义 properties .........................................................................................................................48
21
22
Spring boot 改变默认包扫描...................................................................................................................................50
Spring boot 自定义启动 Banner .............................................................................................................................51
23
Spring boot 导入 spring XML 配置文件 .................................................................................................................. 52
24
25
Spring boot 热部署...................................................................................................................................................54
Spring boot 监控和管理生产环境...........................................................................................................................56
26
Spring boot starter 详解 ...........................................................................................................................................59
27
28
Spring boot 依赖的版本...........................................................................................................................................61
Spring boot 文件上传 ...............................................................................................................................................62
29
Spring boot 集成 redis 缓存 ....................................................................................................................................69
30
31
Spring boot 之 spring cache................................................................................................................................... 85
Spring boot 集成 EHCache ...................................................................................................................................... 92
32
33
Spring boot 分布式 Session 共享 ......................................................................................................................... 107
1 / 108
1 Spring boot helloworld
1.1 介绍
自从 structs2 出现上次的漏洞以后,对 spring 的关注度开始越来越浓。
以前 spring 开发需要配置一大堆的 xml,后台 spring 加入了 annotaion,使得 xml 配置简化了很多,当
然还是有些配置需要使用 xml,比如申明 component scan 等。
Spring 开了一个新的 model spring boot,主要思想是降低 spring 的入门,使得新手可以以最快的速度
让程序在 spring 框架下跑起来。
那么如何写 Hello world 呢?
Hello 之步骤:
(1)新建一个 Maven Java 工程
(2)在 pom.xml 文件中添加 Spring Boot Maven 依赖
(3)编写启动类
(4)运行程序
1.2 Hello 之 New
这个步骤很简单,相比大家都会,小编在此为了文档的完整性,稍作简单说明:
首先使用 IDE(Eclipse,MyEclipse)工具新建一个 Maven 工程,可以是 Maven Java Project,也可以是 Maven Web
Project,随便取一个工程名称。我使用的是 STS,工程名是 spring-boot-hello1。
1.3 Hello 之 Maven
第二步,在 pom.xml 中引入 spring-boot-start-parent,spring 官方的解释叫什么 stater poms,
它可以提供 dependency management,也就是说依赖管理,引入以后在申明其它 dependency 的时候就不需要
version 了,后面可以看到。
org.springframework.boot
spring-boot-starter-parent
1.3.3.RELEASE
1.4 Hello 之 maven web
spring-boot-starter-web,spring 官方解释说 spring-boot-start-web 包含了 spring webmvc 和 tomcat 等 web
第 三 步 , 因 为 我 们 开 发 的 是 web 工 程 , 所 以 需 要 在 pom.xml 中 引 入
开发的特性。
org.springframework.boot
spring-boot-starter-web
2 / 108
1.5 Hello 之 Maven Run Application
如果我们要直接 Main 启动 spring,那么以下 plugin 必须要添加,否则是无法启动的。如果使用
maven 的 spring-boot:run 的话是不需要此配置的。(我在测试的时候,如果不配置下面的 plugin 也是直接在 Main
中运行的。)
org.springframework.boot
spring-boot-maven-plugin
1.6 Hello 之 coding
第四步,真正的程序开始啦,我们需要一个启动类,然后在启动类申明让 spring boot 自动给我
们配置 spring 需要的配置,比如:@SpringBootApplication,为了可以尽快让程序跑起来,我们简单写一个通过
浏览器访问 hello world 字样的例子:
@RestController
@SpringBootApplication
public class App {
@RequestMapping("/")
public String hello(){
return "Hello world!";
}
3 / 108
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
其 中 @SpringBootApplication 申 明 让 spring boot 自 动 给 程 序 进 行 必 要 的 配 置 , 等 价 于 以 默 认 属 性 使 用
@Configuration,@EnableAutoConfiguration 和@ComponentScan
@RestController 返回 json 字符串的数据,直接可以编写 RESTFul 的接口;
1.7 Hello 之 Run
第五步,就是运行我们的 Application 了,我们先介绍第一种运行方式。第一种方式特别简单:
右键 Run As -> Java Application。之后打开浏览器输入地址:http://127.0.0.1:8080/ 就可以看到 Hello world!
了。第二种方式右键 project – Run as – Maven build – 在 Goals 里输入 spring-boot:run ,然后 Apply,
最后点击 Run。
1.8 Hello 之 Error
顺利的情况下当然是皆大欢喜了,但是程序吧往往会给你开个小玩笑。那么我们要注意什么呢?
主要是 jdk 的版本之类的,请看官方说明:
2 Spring boot 返回 json 数据
在做如下操作之前,我们对之前的 Hello 进行简单的修改,我们新建一个包 com.hpit.test.web 然后新建一个类
HelloControoler, 然后修改 App.java 类,主要是的这个类就是一个单纯的启动类。
主要代码如下:
App.java
iackage com.hpit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*/
4 / 108
//其中@SpringBootApplication 申明让 spring boot 自动给程序进行必要的配置,等价于以默认属性使用
//@Configuration,@EnableAutoConfiguration 和@ComponentScan
//@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
com.hpit.test.web.HelloController :
package com.hpit.test.web;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController// 标记为:restful
public class HelloController {
@RequestMapping("/")
public String hello(){
return"Hello world!";
}
}
运行代码和之前是一样的效果的。
我们在编写接口的时候,时常会有需求返回 json 数据,那么在 spring boot 应该怎么操作呢?主要是在 class
中加入注解@RestController,。
返回 JSON 之步骤:
(1)编写一个实体类 Demo
(2)编写 DemoController;
(3)在 DemoController 加上@RestController 和@RequestMapping 注解;
(4)测试
具体代码如下:
com.hpit.test.bean.Demo :
package com.hpit.test.bean;
/**
* 测试实体类.
* @author Administrator
*
*/
public class Demo {
5 / 108
private longid;//主键.
private String name;//测试名称.
public long getId() {
returnid;
}
public void setId(longid) {
this.id = id;
}
public String getName() {
returnname;
}
publicvoid setName(String name) {
this.name = name;
}
}
com.hpit.test.web.DemoController:
package com.hpit.test.web;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hpit.test.bean.Demo;
/**
* 测试.
* @author Administrator
*
*/
@RestController
@RequestMapping("/demo")
public class DemoController {
/**
* 返回 demo 数据:
* 请求地址:http://127.0.0.1:8080/demo/getDemo
* @return
*/
@RequestMapping("/getDemo")
public Demo getDemo(){
6 / 108
Demo demo = new Demo();
demo.setId(1);
demo.setName("Zjs");
return demo;
}
}
那么在浏览器访问地址:http://127.0.0.1:8080/demo/getDemo 返回如下数据:
{
id:
1,
name:
"Zjs"
}
是不是很神奇呢,其实 Spring Boot 也是引用了 JSON 解析包 Jackson,那么自然我们就可以在 Demo 对象上使用
Jackson 提供的 json 属性的注解,对时间进行格式化,对一些字段进行忽略等等。
Spring boot 热部署
在编写代码的时候,你会发现我们只是简单把打印信息改变了下,就需要重新部署,如果是这样的编码方式,那
么我们估计一天下来之后就真的是打几个 Hello World 之后就下班了。那么如何解决热部署的问题呢?那就是
springloaded,加入如下配置:
org.springframework.boot
spring-boot-maven-plugin
org.springframework
springloaded
1.2.4.RELEASE
repackage
exec
如果是使用 spring-boot:run 的话,那么到此配置结束,现在你就可以体验 coding…coding 的爽了。
如果使用的 run as – java application 的话,那么还需要做一些处理哦:
把 spring-loader-1.2.4.RELEASE.jar 下载下来,放到项目的 lib 目录中,然后把 IDEA 的 run 参数里 VM 参数设
置为:
-javaagent:.\lib\springloaded-1.2.4.RELEASE.jar -noverify
然后启动就可以了,这样在 run as 的时候,也能进行热部署了。
7 / 108
3 Spring boot 使用其他 json 转换框架
个人使用比较习惯的 json 框架是 fastjson,所以 spring boot 默认的 json 使用起来就很陌生了,所以很自然我
就想我能不能使用 fastjson 进行 json 解析呢?
com.alibaba
fastjson
1.2.15
这里要说下很重要的话,官方文档说的 1.2.10 以后,会有两个方法支持 HttpMessageconvert,一个是
FastJsonHttpMessageConverter,支持 4.2 以下的版本,一个是 FastJsonHttpMessageConverter4 支持 4.2 以上
的版本,具体有什么区别暂时没有深入研究。这里也就是说:低版本的就不支持了,所以这里最低要求就是
1.2.10+。
配置 fastjon
支持两种方法:
第一种方法:
(1)启动类继承 extends WebMvcConfigurerAdapter
(2)覆盖方法 configureMessageConverters
第二种方法:
(1)在 App.java 启动类中,注入 Bean : HttpMessageConverters
具体代码如下:
代码:App.java
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
//如果想集成其他的json框架需要继承WebMvcConfigurerAdapter,并重写configureMessageConverters
@SpringBootApplication
public class App extends WebMvcConfigurerAdapter {
// 第一种方式,重写configureMessageConverters,并将FastJsonConverter设置到系统中
@Override
public void configureMessageConverters(List
> converters) {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
converter.setFeatures(SerializerFeature.PrettyFormat);
converters.add(converter);
super.configureMessageConverters(converters);
}
// 第二种方法:注入beanHttpMessageConverters
8 / 108