Skip to content

Commit 5a4fb4b

Browse files
committed
Prefer Java configuration over XML.
Closes #2416
1 parent bc983fe commit 5a4fb4b

File tree

6 files changed

+105
-15
lines changed

6 files changed

+105
-15
lines changed

src/main/asciidoc/index.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ ifdef::backend-epub3[:front-cover-image: image:epub-cover.png[Front Cover,1050,1
77
:spring-data-commons-docs: https://raw.githubusercontent.com/spring-projects/spring-data-commons/master/src/main/asciidoc
88
:spring-framework-javadoc: https://docs.spring.io/spring-framework/docs/{springVersion}/javadoc-api
99
:spring-framework-reference: https://docs.spring.io/spring-framework/docs/{springVersion}/reference/html
10+
:store: Redis
1011

1112
(C) 2011-2022 The original authors.
1213

src/main/asciidoc/reference/pipelining.adoc

+1-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Note that the value returned from the `RedisCallback` is required to be `null`,
2626

2727
[TIP]
2828
====
29-
The Lettuce driver supports fine grained flush control that allows to either flush commands as they appear, buffer or send them at connection close.
29+
The Lettuce driver supports fine-grained flush control that allows to either flush commands as they appear, buffer or send them at connection close.
3030
3131
[source,java]
3232
----
@@ -35,5 +35,3 @@ factory.setPipeliningFlushPolicy(PipeliningFlushPolicy.buffered(3)); <1>
3535
----
3636
<1> Buffer locally and flush after every 3rd command.
3737
====
38-
39-
include::version-note.adoc[]

src/main/asciidoc/reference/redis-messaging.adoc

+33-5
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ Consider the following interface definition:
6565
----
6666
public interface MessageDelegate {
6767
void handleMessage(String message);
68-
void handleMessage(Map message); void handleMessage(byte[] message);
68+
void handleMessage(Map message);
69+
void handleMessage(byte[] message);
6970
void handleMessage(Serializable message);
7071
// pass the channel/pattern as well
7172
void handleMessage(Serializable message, String channel);
@@ -83,10 +84,36 @@ public class DefaultMessageDelegate implements MessageDelegate {
8384

8485
Notice how the above implementation of the `MessageDelegate` interface (the above `DefaultMessageDelegate` class) has *no* Redis dependencies at all. It truly is a POJO that we make into an MDP with the following configuration:
8586

86-
[source,xml]
87+
====
88+
.Java
89+
[source,java,role="primary"]
90+
----
91+
@Configuration
92+
class MyConfig {
93+
94+
// …
95+
96+
@Bean
97+
DefaultMessageDelegate listener() {
98+
return new DefaultMessageDelegate();
99+
}
100+
101+
@Bean
102+
RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory, DefaultMessageDelegate listener) {
103+
104+
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
105+
container.setConnectionFactory(connectionFactory);
106+
container.addMessageListener(new MessageListenerAdapter(listener, "handleMessage"), ChannelTopic.of("chatroom"));
107+
return container;
108+
}
109+
}
110+
----
111+
112+
.XML
113+
[source,xml,role="secondary"]
87114
----
88115
<?xml version="1.0" encoding="UTF-8"?>
89-
<beans xmlns="http://www.springframework.org/schema/beans"
116+
<beans xmlns="http://www.springframework.org/schema/beans"
90117
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
91118
xmlns:redis="http://www.springframework.org/schema/redis"
92119
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
@@ -100,12 +127,13 @@ Notice how the above implementation of the `MessageDelegate` interface (the abov
100127
101128
<bean id="listener" class="redisexample.DefaultMessageDelegate"/>
102129
...
103-
<beans>
130+
</beans>
104131
----
132+
====
105133

106134
NOTE: The listener topic can be either a channel (for example, `topic="chatroom"`) or a pattern (for example, `topic="*room"`)
107135

108-
The preceding example uses the Redis namespace to declare the message listener container and automatically register the POJOs as listeners. The full blown beans definition follows:
136+
The preceding example uses the Redis namespace to declare the message listener container and automatically register the POJOs as listeners. The full-blown beans definition follows:
109137

110138
[source,xml]
111139
----

src/main/asciidoc/reference/redis-transactions.adoc

-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ System.out.println("Number of items added to set: " + txResults.get(0));
2525
`RedisTemplate` uses its value, hash key, and hash value serializers to deserialize all results of `exec` before returning.
2626
There is an additional `exec` method that lets you pass a custom serializer for transaction results.
2727

28-
include::version-note.adoc[]
29-
3028
[[tx.spring]]
3129
== @Transactional Support
3230

src/main/asciidoc/reference/redis.adoc

+70-4
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,30 @@ Once configured, the template is thread-safe and can be reused across multiple i
391391

392392
For cases where you need a certain template view, declare the view as a dependency and inject the template. The container automatically performs the conversion, eliminating the `opsFor[X]` calls, as shown in the following example:
393393

394-
[source,xml]
394+
====
395+
.Java
396+
[source,java,role="primary"]
397+
----
398+
@Configuration
399+
class MyConfig {
400+
401+
@Bean
402+
LettuceConnectionFactory redisConnectionFactory() {
403+
return new LettuceConnectionFactory();
404+
}
405+
406+
@Bean
407+
RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
408+
409+
RedisTemplate<String, String> template = new RedisTemplate<>();
410+
template.setConnectionFactory(redisConnectionFactory);
411+
return template;
412+
}
413+
}
414+
----
415+
416+
.XML
417+
[source,xml,role="secondary"]
395418
----
396419
<?xml version="1.0" encoding="UTF-8"?>
397420
<beans xmlns="http://www.springframework.org/schema/beans"
@@ -406,6 +429,7 @@ For cases where you need a certain template view, declare the view as a dependen
406429
407430
</beans>
408431
----
432+
====
409433

410434
[source,java]
411435
----
@@ -430,7 +454,30 @@ public class Example {
430454

431455
Since it is quite common for the keys and values stored in Redis to be `java.lang.String`, the Redis modules provides two extensions to `RedisConnection` and `RedisTemplate`, respectively the `StringRedisConnection` (and its `DefaultStringRedisConnection` implementation) and `StringRedisTemplate` as a convenient one-stop solution for intensive String operations. In addition to being bound to `String` keys, the template and the connection use the `StringRedisSerializer` underneath, which means the stored keys and values are human-readable (assuming the same encoding is used both in Redis and your code). The following listings show an example:
432456

433-
[source,xml]
457+
====
458+
.Java
459+
[source,java,role="primary"]
460+
----
461+
@Configuration
462+
class MyConfig {
463+
464+
@Bean
465+
LettuceConnectionFactory redisConnectionFactory() {
466+
return new LettuceConnectionFactory();
467+
}
468+
469+
@Bean
470+
StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
471+
472+
StringRedisTemplate template = new StringRedisTemplate();
473+
template.setConnectionFactory(redisConnectionFactory);
474+
return template;
475+
}
476+
}
477+
----
478+
479+
.XML
480+
[source,xml,role="secondary"]
434481
----
435482
<?xml version="1.0" encoding="UTF-8"?>
436483
<beans xmlns="http://www.springframework.org/schema/beans"
@@ -441,9 +488,10 @@ Since it is quite common for the keys and values stored in Redis to be `java.lan
441488
<bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory"/>
442489
443490
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" p:connection-factory-ref="redisConnectionFactory"/>
444-
...
491+
445492
</beans>
446493
----
494+
====
447495

448496
[source,java]
449497
----
@@ -662,7 +710,24 @@ Package `org.springframework.data.redis.support` offers various reusable compone
662710

663711
The atomic counters make it easy to wrap Redis key incrementation while the collections allow easy management of Redis keys with minimal storage exposure or API leakage. In particular, the `RedisSet` and `RedisZSet` interfaces offer easy access to the set operations supported by Redis, such as `intersection` and `union`. `RedisList` implements the `List`, `Queue`, and `Deque` contracts (and their equivalent blocking siblings) on top of Redis, exposing the storage as a FIFO (First-In-First-Out), LIFO (Last-In-First-Out) or capped collection with minimal configuration. The following example shows the configuration for a bean that uses a `RedisList`:
664712

665-
[source,xml]
713+
====
714+
.Java
715+
[source,java,role="primary"]
716+
----
717+
@Configuration
718+
class MyConfig {
719+
720+
// …
721+
722+
@Bean
723+
RedisList<String> stringRedisTemplate(RedisTemplate<String, String> redisTemplate) {
724+
return new DefaultRedisList<>(template, "queue-key");
725+
}
726+
}
727+
----
728+
729+
.XML
730+
[source,xml,role="secondary"]
666731
----
667732
<?xml version="1.0" encoding="UTF-8"?>
668733
<beans xmlns="http://www.springframework.org/schema/beans"
@@ -677,6 +742,7 @@ The atomic counters make it easy to wrap Redis key incrementation while the coll
677742
678743
</beans>
679744
----
745+
====
680746

681747
The following example shows a Java configuration example for a `Deque`:
682748

src/main/asciidoc/reference/version-note.adoc

-1
This file was deleted.

0 commit comments

Comments
 (0)