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

4.0 KiB

Home | Next: Default serialization

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 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 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
stringRedisTemplate StringRedisTemplate String serialization, see chapter 3
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
Redis repositories @RedisHash repositories Enabled with enableKeyspaceEvents = OFF. See chapter 6
RedisCacheManager when @EnableCaching is on See chapter 9

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) 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).
  • --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

Home | Next: Default serialization