22
22
import org .springframework .beans .TypeConverter ;
23
23
import org .springframework .beans .factory .BeanFactory ;
24
24
import org .springframework .beans .factory .BeanFactoryAware ;
25
- import org .springframework .beans .factory .config .ConfigurableBeanFactory ;
26
25
import org .springframework .core .convert .ConversionService ;
27
26
import org .springframework .core .convert .support .DefaultConversionService ;
28
27
import org .springframework .lang .Nullable ;
@@ -42,8 +41,7 @@ public class GenericToStringSerializer<T> implements RedisSerializer<T>, BeanFac
42
41
43
42
private final Class <T > type ;
44
43
private final Charset charset ;
45
-
46
- private Converter converter = new Converter (new DefaultConversionService ());
44
+ private Converter converter ;
47
45
48
46
public GenericToStringSerializer (Class <T > type ) {
49
47
this (type , StandardCharsets .UTF_8 );
@@ -55,55 +53,64 @@ public GenericToStringSerializer(Class<T> type, Charset charset) {
55
53
56
54
this .type = type ;
57
55
this .charset = charset ;
56
+ this .converter = new Converter (DefaultConversionService .getSharedInstance ());
58
57
}
59
58
59
+ /**
60
+ * Set the {@link ConversionService} to be used.
61
+ *
62
+ * @param conversionService the conversion service to be used, must not be {@literal null}.
63
+ */
60
64
public void setConversionService (ConversionService conversionService ) {
61
65
62
- Assert .notNull (conversionService , "non null conversion service required" );
66
+ Assert .notNull (conversionService , "ConversionService must not be null" );
67
+
63
68
converter = new Converter (conversionService );
64
69
}
65
70
71
+ /**
72
+ * Set the {@link TypeConverter} to be used.
73
+ *
74
+ * @param typeConverter the conversion service to be used, must not be {@literal null}.
75
+ */
66
76
public void setTypeConverter (TypeConverter typeConverter ) {
67
77
68
- Assert .notNull (typeConverter , "non null type converter required" );
78
+ Assert .notNull (typeConverter , "TypeConverter must not be null" );
79
+
69
80
converter = new Converter (typeConverter );
70
81
}
71
82
72
83
@ Override
73
- public T deserialize (@ Nullable byte [] bytes ) {
84
+ public byte [] serialize (@ Nullable T value ) {
74
85
75
- if (bytes == null ) {
86
+ if (value == null ) {
76
87
return null ;
77
88
}
78
89
79
- String string = new String ( bytes , charset );
80
- return converter . convert ( string , type );
90
+ String string = converter . convert ( value , String . class );
91
+ return string . getBytes ( charset );
81
92
}
82
93
83
94
@ Override
84
- public byte [] serialize (@ Nullable T object ) {
85
- if (object == null ) {
95
+ public T deserialize (@ Nullable byte [] bytes ) {
96
+
97
+ if (bytes == null ) {
86
98
return null ;
87
99
}
88
- String string = converter .convert (object , String .class );
89
- return string .getBytes (charset );
100
+
101
+ String string = new String (bytes , charset );
102
+ return converter .convert (string , type );
90
103
}
91
104
105
+ @ Override
92
106
public void setBeanFactory (BeanFactory beanFactory ) throws BeansException {
93
-
94
- // TODO: This code can never happen...
95
- if (converter == null && beanFactory instanceof ConfigurableBeanFactory ) {
96
- ConfigurableBeanFactory cFB = (ConfigurableBeanFactory ) beanFactory ;
97
- ConversionService conversionService = cFB .getConversionService ();
98
-
99
- converter = (conversionService != null ? new Converter (conversionService )
100
- : new Converter (cFB .getTypeConverter ()));
101
- }
107
+ // no-op
102
108
}
103
109
104
- private class Converter {
105
- private final ConversionService conversionService ;
106
- private final TypeConverter typeConverter ;
110
+ private final static class Converter {
111
+
112
+ private final @ Nullable ConversionService conversionService ;
113
+ private final @ Nullable TypeConverter typeConverter ;
107
114
108
115
public Converter (ConversionService conversionService ) {
109
116
this .conversionService = conversionService ;
@@ -115,11 +122,11 @@ public Converter(TypeConverter typeConverter) {
115
122
this .typeConverter = typeConverter ;
116
123
}
117
124
125
+ @ Nullable
118
126
<E > E convert (Object value , Class <E > targetType ) {
119
- if (conversionService != null ) {
120
- return conversionService .convert (value , targetType );
121
- }
122
- return typeConverter .convertIfNecessary (value , targetType );
127
+
128
+ return conversionService != null ? conversionService .convert (value , targetType )
129
+ : typeConverter .convertIfNecessary (value , targetType );
123
130
}
124
131
}
125
132
}
0 commit comments