SpringBoot 2.0 学习计划

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/programmer_at/article/details/80877211

SpringBoot 2.0 学习计划

学习计划相关链接: https://github.com/YuanKQ/SpringBootTutorial , 学习计划的每一步都有对应的commit.

在查找配置文件相关文档时,务必要保证SpringBoot的版本号要匹配,

// 重要版本信息
Spring Boot 2.0.3
fastJson 1.2.17
redis 4.0   

Spring Core

理解Bean的作用域,生命周期,体会Bean的注入

详见《Spring实战》第三版:第1章~第3章

常见注解

@Config,@Bean

@Controller,@RestController

@RequestMapping

Spring Web

配置模板引擎

ViewResolverConfig.java

配置路由(URL映射)

UrlMappingController.java
- @RequestMapping配置Tutorial(全):
http://www.baeldung.com/spring-requestmapping
- @RequestMapping new shortcut annotation:
http://www.baeldung.com/spring-new-requestmapping-shortcuts

两者完全等价:@RequestMapping(method = { RequestMethod.GET })与 @GetMapping, @RequestMapping(method = { RequestMethod.POST}) 与 @PostMapping
- @RequestMapping可配置项的解释:
http://jinnianshilongnian.iteye.com/blog/1684403(开头导读部分)
- 生产者与消费者概念(即RequestMapping的参数”consumes”, “produces”):
http://jinnianshilongnian.iteye.com/blog/1695047

http 协议中的Header部分的规定 --》 生产者、消费者模式 --》(Server) consumes、produces

consumes,produces参数时,方法级别的映射将覆盖类级别的映射,这点要与value参数的使用区别开来。

fastJson配置

使用 Fastjson 提供的FastJsonHttpMessageConverter 来替换 Spring 默认的 HttpMessageConverter以提高@RestController, @ResponseBody, @RequestBody 注解的 JSON序列化速度

官方doc: http://static.javadoc.io/com.alibaba/fastjson/1.2.33/index.html
@JsonField解析: https://github.com/alibaba/fastjson/wiki/JSONField
sample: http://www.baeldung.com/fastjson
配置: https://www.jianshu.com/p/aabd308ac963

序列化 JSON.toJSONString():Object –> String
- private/protected字段没有加getter方法的话,该字段不会被序列化(该字段不会输出到String中)

反序列化 JSON.parseObject():String –> Object
- private/protected字段没有setter方法的话,该字段不会被反序列化(该字段的值保留为系统默认值)
- define a no-args or default constructor

验证:
- 本地 JSON.toJSONString()JSON.parseObject()
- client与server端:JSON序列化与反序列化

Spring Data

Redis

install redis

倘若是Manjaro,直接在Terminal命令输入yaourt -S redis
其他OS参见官网:http://www.baeldung.com/spring-data-redis-tutorial

Introduction to Spring Data Redis

first run Reids by the command redis-server /etc/redis.conf --port 7001

以上配置只能二选一,虽然方法2减少配置工作量,但是作用有限,当你需要使用RedisTemplate,只能使用方法1.

使用RedisTemplate实现CRUD操作

先看这个tutorial: https://www.cnblogs.com/EasonJim/p/7803067.html,对RedisTemplate常用操作有个基本了解

在 Spring Data Redis 中集成 Fastjson: https://github.com/alibaba/fastjson/wiki/%E5%9C%A8-Spring-%E4%B8%AD%E9%9B%86%E6%88%90-Fastjson

所有步骤:http://javasampleapproach.com/spring-framework/spring-data/spring-data-redis-example-spring-boot-redis-example#2Configure_Spring_Data_Redis

不要忘了在相应的repo的类中添加@Repository

Redis缓存配置

RedisCacheManager在SpringBoot2.0中有较大改动。

最简单的配置,可用于测试配置是否正确:https://blog.csdn.net/fanpeizhong/article/details/79998164
注解失效时间+主动刷新缓存:https://www.cnblogs.com/ASPNET2008/p/6511500.htmlhttps://www.cnblogs.com/ASPNET2008/p/8733087.html

@Cacheable,@CachePut, @CacheEvict, @CacheConfig介绍:https://blog.csdn.net/poorCoder_/article/details/55258253

cacheNames和key都一样,则会访问相同的缓存.

@Cacheable,@CachePut, @CacheEvict, @CacheConfig实战:https://my.oschina.net/silenceyawen/blog/1555996

其他

一旦需要同时使用@Configuration和application.properties来配置资源,就需要在SprinBoot启动类中显示声明@PropertySource({"classpath:application.properties"})

猜你喜欢

转载自blog.csdn.net/programmer_at/article/details/80877211
今日推荐