Skip to content

Commit be3a0f5

Browse files
DATAREDIS-425 - Add dedicated RedisRepositoryFactory.
We need to have a dedicated RepositoryFactory to be able to tweak query creation. Now derived queries are freshly instantiated new for every execution.
1 parent baa3d41 commit be3a0f5

File tree

4 files changed

+166
-3
lines changed

4 files changed

+166
-3
lines changed

src/main/java/org/springframework/data/redis/repository/configuration/EnableRedisRepositories.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import org.springframework.context.annotation.Import;
2828
import org.springframework.data.keyvalue.core.KeyValueOperations;
2929
import org.springframework.data.keyvalue.repository.config.QueryCreatorType;
30-
import org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFactoryBean;
3130
import org.springframework.data.redis.core.index.IndexConfiguration;
3231
import org.springframework.data.redis.repository.query.RedisQueryCreator;
32+
import org.springframework.data.redis.repository.support.RedisRepositoryFactoryBean;
3333
import org.springframework.data.repository.config.DefaultRepositoryBaseClass;
3434
import org.springframework.data.repository.query.QueryLookupStrategy;
3535
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
@@ -101,11 +101,11 @@
101101

102102
/**
103103
* Returns the {@link FactoryBean} class to be used for each repository instance. Defaults to
104-
* {@link KeyValueRepositoryFactoryBean}.
104+
* {@link RedisRepositoryFactoryBean}.
105105
*
106106
* @return
107107
*/
108-
Class<?> repositoryFactoryBeanClass() default KeyValueRepositoryFactoryBean.class;
108+
Class<?> repositoryFactoryBeanClass() default RedisRepositoryFactoryBean.class;
109109

110110
/**
111111
* Configure the repository base class to be used to create repository proxies for this particular configuration.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.redis.repository.support;
17+
18+
import java.lang.reflect.Method;
19+
20+
import org.springframework.data.keyvalue.core.KeyValueOperations;
21+
import org.springframework.data.keyvalue.repository.query.KeyValuePartTreeQuery;
22+
import org.springframework.data.keyvalue.repository.query.KeyValuePartTreeQuery.QueryInitialization;
23+
import org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFactory;
24+
import org.springframework.data.repository.core.NamedQueries;
25+
import org.springframework.data.repository.core.RepositoryMetadata;
26+
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
27+
import org.springframework.data.repository.query.EvaluationContextProvider;
28+
import org.springframework.data.repository.query.QueryLookupStrategy;
29+
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
30+
import org.springframework.data.repository.query.QueryMethod;
31+
import org.springframework.data.repository.query.RepositoryQuery;
32+
import org.springframework.data.repository.query.parser.AbstractQueryCreator;
33+
import org.springframework.util.Assert;
34+
35+
/**
36+
* {@link RepositoryFactorySupport} specific of handing Redis
37+
* {@link org.springframework.data.keyvalue.repository.KeyValueRepository}.
38+
*
39+
* @author Christoph Strobl
40+
* @since 1.7
41+
*/
42+
public class RedisRepositoryFactory extends KeyValueRepositoryFactory {
43+
44+
/**
45+
* @param keyValueOperations
46+
* @see KeyValueRepositoryFactory#KeyValueRepositoryFactory(KeyValueOperations)
47+
*/
48+
public RedisRepositoryFactory(KeyValueOperations keyValueOperations) {
49+
super(keyValueOperations);
50+
}
51+
52+
/**
53+
* @param keyValueOperations
54+
* @param queryCreator
55+
* @see KeyValueRepositoryFactory#KeyValueRepositoryFactory(KeyValueOperations, Class)
56+
*/
57+
public RedisRepositoryFactory(KeyValueOperations keyValueOperations,
58+
Class<? extends AbstractQueryCreator<?, ?>> queryCreator) {
59+
super(keyValueOperations, queryCreator);
60+
}
61+
62+
/*
63+
* (non-Javadoc)
64+
* @see org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFactory#getQueryLookupStrategy(org.springframework.data.repository.query.QueryLookupStrategy.Key, org.springframework.data.repository.query.EvaluationContextProvider)
65+
*/
66+
@Override
67+
protected QueryLookupStrategy getQueryLookupStrategy(Key key, EvaluationContextProvider evaluationContextProvider) {
68+
return new RedisQueryLookupStrategy(key, evaluationContextProvider, getKeyValueOperations(), getQueryCreator());
69+
}
70+
71+
/**
72+
* @author Christoph Strobl
73+
*/
74+
private static class RedisQueryLookupStrategy implements QueryLookupStrategy {
75+
76+
private EvaluationContextProvider evaluationContextProvider;
77+
private KeyValueOperations keyValueOperations;
78+
79+
private Class<? extends AbstractQueryCreator<?, ?>> queryCreator;
80+
81+
/**
82+
* Creates a new {@link RedisQueryLookupStrategy} for the given {@link Key}, {@link EvaluationContextProvider},
83+
* {@link KeyValueOperations} and query creator type.
84+
* <p>
85+
*
86+
* @param key
87+
* @param evaluationContextProvider must not be {@literal null}.
88+
* @param keyValueOperations must not be {@literal null}.
89+
* @param queryCreator must not be {@literal null}.
90+
*/
91+
public RedisQueryLookupStrategy(Key key, EvaluationContextProvider evaluationContextProvider,
92+
KeyValueOperations keyValueOperations, Class<? extends AbstractQueryCreator<?, ?>> queryCreator) {
93+
94+
Assert.notNull(evaluationContextProvider, "EvaluationContextProvider must not be null!");
95+
Assert.notNull(keyValueOperations, "KeyValueOperations must not be null!");
96+
Assert.notNull(queryCreator, "Query creator type must not be null!");
97+
98+
this.evaluationContextProvider = evaluationContextProvider;
99+
this.keyValueOperations = keyValueOperations;
100+
this.queryCreator = queryCreator;
101+
}
102+
103+
/*
104+
* (non-Javadoc)
105+
* @see org.springframework.data.repository.query.QueryLookupStrategy#resolveQuery(java.lang.reflect.Method, org.springframework.data.repository.core.RepositoryMetadata, org.springframework.data.repository.core.NamedQueries)
106+
*/
107+
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, NamedQueries namedQueries) {
108+
109+
QueryMethod queryMethod = new QueryMethod(method, metadata);
110+
KeyValuePartTreeQuery partTreeQuery = new KeyValuePartTreeQuery(queryMethod, evaluationContextProvider,
111+
this.keyValueOperations, this.queryCreator);
112+
partTreeQuery.setQueryIntialization(QueryInitialization.NEW);
113+
return partTreeQuery;
114+
}
115+
}
116+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.redis.repository.support;
17+
18+
import java.io.Serializable;
19+
20+
import org.springframework.beans.factory.FactoryBean;
21+
import org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFactoryBean;
22+
import org.springframework.data.repository.Repository;
23+
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
24+
25+
/**
26+
* Adapter for Springs {@link FactoryBean} interface to allow easy setup of {@link RedisRepositoryFactory} via Spring
27+
* configuration.
28+
*
29+
* @author Christoph Strobl
30+
* @param <T> The repository type.
31+
* @param <S> The repository domain type.
32+
* @param <ID> The repository id type.
33+
*/
34+
public class RedisRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> extends
35+
KeyValueRepositoryFactoryBean<T, S, ID> {
36+
37+
/*
38+
* (non-Javadoc)
39+
* @see org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport#createRepositoryFactory()
40+
*/
41+
@Override
42+
protected RepositoryFactorySupport createRepositoryFactory() {
43+
return new RedisRepositoryFactory(getOperations(), getQueryCreator());
44+
}
45+
}

src/test/java/org/springframework/data/redis/repository/RedisRepositoryIntegrationTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ public void simpleFindSouldReturnEntitiesCorrectly() {
8888

8989
assertThat(repo.findByFirstname("rand").size(), is(1));
9090
assertThat(repo.findByFirstname("rand"), hasItem(rand));
91+
92+
assertThat(repo.findByLastname("al'thor"), hasItem(rand));
9193
}
9294

9395
/**

0 commit comments

Comments
 (0)