Files
spring-boot-demo/redis/docs/01-what-boot-gives-you.md

56 lines
4.0 KiB
Markdown

[Home](../README.md) | Next: [Default serialization](02-default-serialization.md)
# 1. What Boot gives you
Add `spring-boot-starter-data-redis` and start the application. With no configuration at all you
already have a connection factory, two templates, a Pub/Sub listener container and, if you declare
an interface, a repository. This chapter lists exactly what, because every surprise in the later
chapters comes from one of these defaults.
## What the starter brings
[output/17-dependencies.txt](output/17-dependencies.txt) is the starter's own pom plus a filtered
`mvn dependency:tree`. Three things arrive: `spring-boot-starter`, `spring-boot-data-redis` (the
auto-configuration) and `spring-messaging`. Under `spring-boot-data-redis` come `spring-data-redis`
4.1.1 and the Lettuce client 7.5.2. There is no Jedis and **no JSON library**.
That last point matters: Jackson is not a transitive dependency of the Redis starter. This module
adds `spring-boot-starter-jackson` (Jackson 3, package `tools.jackson`) itself, and that is the only
reason `tools.jackson.core:jackson-databind:3.1.5` is in the tree. Note also that Jackson 3 still
uses the Jackson 2 annotations artifact (`com.fasterxml.jackson.core:jackson-annotations:2.21`).
## What the auto-configuration registers
[output/16-serializers-javap.txt](output/16-serializers-javap.txt) reads the Boot jar with `javap`.
Boot 4 moved the classes to `org.springframework.boot.data.redis.autoconfigure`, and properties live
under `spring.data.redis.*` (`spring.data.redis.port` is what the tests set).
| Bean | Type | Notes |
|---|---|---|
| `redisTemplate` | `RedisTemplate<Object, Object>` | JDK serialization, see [chapter 2](02-default-serialization.md) |
| `stringRedisTemplate` | `StringRedisTemplate` | String serialization, see [chapter 3](03-string-template-and-two-keyspaces.md) |
| `redisMessageListenerContainer` | `RedisMessageListenerContainer` | Created when there is a single `RedisConnectionFactory` and no bean of that name. A second definition, `redisMessageListenerContainerVirtualThreads`, is annotated `@ConditionalOnThreading`. Also turns on `@EnableRedisListeners`. See [chapter 7](07-pub-sub.md) |
| Redis repositories | `@RedisHash` repositories | Enabled with `enableKeyspaceEvents = OFF`. See [chapter 6](06-hash-ttl-and-keyspace-events.md) |
| `RedisCacheManager` | when `@EnableCaching` is on | See [chapter 9](09-redis-as-cache.md) |
The templates and the listener container are `@ConditionalOnMissingBean` (the container by name), so the
way to change a default is to declare your own bean. That is also how you lose the default without noticing: a bean of the same *name* replaces
Boot's, a bean of a different name sits next to it. `JsonRedisConfig` in this module names its
templates `jsonRedisTemplate` and `userRedisTemplate` for that reason.
## The setup used in this repository
- The tests start a throwaway `redis-server` on **6390** ([LocalRedis](../src/test/java/com/ankurm/redis/LocalRedis.java)) and stop it when the JVM exits.
- They call `FLUSHALL`, so `LocalRedis` refuses to run against a server that is already listening.
- Every transcript line starting with `$ redis-cli` was produced by running the real `redis-cli` from the test ([Transcript](../src/test/java/com/ankurm/redis/Transcript.java)).
- `--no-raw` makes `redis-cli` print binary bytes as `\xNN` escapes and `(nil)` for a missing key. Without it, a non-terminal `redis-cli` prints raw bytes and a bare blank line for nil.
- `| sort` after a `KEYS` or `SMEMBERS` command means the transcript sorted the lines, because Redis returns them in no particular order.
## Going deeper
- [Spring Boot reference: NoSQL, Redis](https://docs.spring.io/spring-boot/reference/data/nosql.html#data.nosql.redis)
- [Spring Data Redis reference](https://docs.spring.io/spring-data/redis/reference/)
- Article: [The Spring Cache Abstraction](https://ankurm.com/spring-cache-abstraction-cacheable-cacheevict-self-invocation-trap/)
[Home](../README.md) | Next: [Default serialization](02-default-serialization.md)