add warning regarding RedisTemplate initialization

DATAREDIS-112
This commit is contained in:
Costin Leau
2013-02-05 18:32:04 +02:00
parent 3be1d7d637
commit b869bc7136
2 changed files with 14 additions and 0 deletions

View File

@@ -66,6 +66,7 @@ import org.springframework.util.ClassUtils;
public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperations<K, V> {
private boolean exposeConnection = false;
private boolean initialized = false;
private RedisSerializer<?> defaultSerializer = new JdkSerializationRedisSerializer();
private RedisSerializer keySerializer = null;
@@ -114,6 +115,8 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
if (defaultUsed) {
Assert.notNull(defaultSerializer, "default serializer null and not all serializers initialized");
}
initialized = true;
}
@@ -144,6 +147,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
* @return object returned by the action
*/
public <T> T execute(RedisCallback<T> action, boolean exposeConnection, boolean pipeline) {
Assert.isTrue(initialized, "template not initialized; call afterPropertiesSet() before using it");
Assert.notNull(action, "Callback object must not be null");
RedisConnectionFactory factory = getConnectionFactory();
@@ -177,6 +181,9 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
public <T> T execute(SessionCallback<T> session) {
Assert.isTrue(initialized, "template not initialized; call afterPropertiesSet() before using it");
Assert.notNull(session, "Callback object must not be null");
RedisConnectionFactory factory = getConnectionFactory();
// bind connection
RedisConnectionUtils.bindConnection(factory);

View File

@@ -57,6 +57,13 @@ public class TemplateTest {
return CollectionTestParams.testParams();
}
@Test(expected = IllegalArgumentException.class)
public void testTemplateNotInitialized() throws Exception {
RedisTemplate tpl = new RedisTemplate();
tpl.setConnectionFactory(template.getConnectionFactory());
tpl.exec();
}
@Test
public void testKeys() throws Exception {
assertTrue(template.keys("*") != null);