Skip to content

Commit 4fa5cb5

Browse files
committed
Auto-configure Redis repositories
See gh-5448
1 parent b8bc4f6 commit 4fa5cb5

File tree

7 files changed

+274
-0
lines changed

7 files changed

+274
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2012-2016 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+
17+
package org.springframework.boot.autoconfigure.data.redis;
18+
19+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
20+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.context.annotation.Import;
25+
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
26+
import org.springframework.data.redis.repository.support.RedisRepositoryFactoryBean;
27+
import redis.clients.jedis.Jedis;
28+
29+
/**
30+
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Redis
31+
* Repositories.
32+
*
33+
* @author Eddú Meléndez
34+
* @see EnableRedisRepositories
35+
* @since 1.4.0
36+
*/
37+
@Configuration
38+
@ConditionalOnClass({ Jedis.class, EnableRedisRepositories.class })
39+
@ConditionalOnProperty(prefix = "spring.data.redis.repositories", name = "enabled", havingValue = "true", matchIfMissing = true)
40+
@ConditionalOnMissingBean(RedisRepositoryFactoryBean.class)
41+
@Import(RedisRepositoriesAutoConfigureRegistrar.class)
42+
public class RedisRepositoriesAutoConfiguration {
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2012-2016 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+
17+
package org.springframework.boot.autoconfigure.data.redis;
18+
19+
import java.lang.annotation.Annotation;
20+
21+
import org.springframework.boot.autoconfigure.data.AbstractRepositoryConfigurationSourceSupport;
22+
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
23+
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
24+
import org.springframework.data.redis.repository.configuration.RedisRepositoryConfigurationExtension;
25+
import org.springframework.data.repository.config.RepositoryConfigurationExtension;
26+
27+
/**
28+
* {@link ImportBeanDefinitionRegistrar} used to auto-configure Spring Data Redis
29+
* Repositories.
30+
* @author Eddú Meléndez
31+
* @since 1.4.0
32+
*/
33+
public class RedisRepositoriesAutoConfigureRegistrar extends
34+
AbstractRepositoryConfigurationSourceSupport {
35+
@Override
36+
protected Class<? extends Annotation> getAnnotation() {
37+
return EnableRedisRepositories.class;
38+
}
39+
40+
@Override
41+
protected Class<?> getConfiguration() {
42+
return EnableCassandraRepositoriesConfiguration.class;
43+
}
44+
45+
@Override
46+
protected RepositoryConfigurationExtension getRepositoryConfigurationExtension() {
47+
return new RedisRepositoryConfigurationExtension();
48+
}
49+
50+
@EnableRedisRepositories
51+
private static class EnableCassandraRepositoriesConfiguration {
52+
}
53+
54+
}

spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\
3333
org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\
3434
org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\
3535
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\
36+
org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\
3637
org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\
3738
org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\
3839
org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2012-2016 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+
17+
package org.springframework.boot.autoconfigure.data.alt.redis;
18+
19+
import org.springframework.boot.autoconfigure.data.redis.city.City;
20+
import org.springframework.data.repository.Repository;
21+
22+
public interface CityRedisRepository extends Repository<City, Long> {
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2012-2016 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+
17+
package org.springframework.boot.autoconfigure.data.redis;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import org.junit.After;
22+
import org.junit.Test;
23+
24+
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
25+
import org.springframework.boot.autoconfigure.data.redis.city.CityRepository;
26+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
27+
28+
/**
29+
* Tests for {@link RedisRepositoriesAutoConfiguration}.
30+
*
31+
* @author Eddú Meléndez
32+
*/
33+
public class RedisRepositoriesAutoConfigurationTests {
34+
35+
private AnnotationConfigApplicationContext context;
36+
37+
@After
38+
public void close() {
39+
this.context.close();
40+
}
41+
42+
@Test
43+
public void testDefaultRepositoryConfiguration() throws Exception {
44+
this.context = new AnnotationConfigApplicationContext();
45+
this.context.register(RedisAutoConfiguration.class,
46+
RedisRepositoriesAutoConfiguration.class,
47+
PropertyPlaceholderAutoConfiguration.class);
48+
this.context.refresh();
49+
assertThat(this.context.getBean(CityRepository.class)).isNotNull();
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2012-2016 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+
17+
package org.springframework.boot.autoconfigure.data.redis.city;
18+
19+
import java.io.Serializable;
20+
21+
import org.springframework.data.annotation.Id;
22+
import org.springframework.data.redis.core.RedisHash;
23+
24+
@RedisHash("cities")
25+
public class City implements Serializable {
26+
27+
private static final long serialVersionUID = 1L;
28+
29+
@Id
30+
private Long id;
31+
32+
private String name;
33+
34+
private String state;
35+
36+
private String country;
37+
38+
private String map;
39+
40+
protected City() {
41+
}
42+
43+
public City(String name, String country) {
44+
super();
45+
this.name = name;
46+
this.country = country;
47+
}
48+
49+
public String getName() {
50+
return this.name;
51+
}
52+
53+
public String getState() {
54+
return this.state;
55+
}
56+
57+
public String getCountry() {
58+
return this.country;
59+
}
60+
61+
public String getMap() {
62+
return this.map;
63+
}
64+
65+
@Override
66+
public String toString() {
67+
return getName() + "," + getState() + "," + getCountry();
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2012-2016 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+
17+
package org.springframework.boot.autoconfigure.data.redis.city;
18+
19+
import org.springframework.data.domain.Page;
20+
import org.springframework.data.domain.Pageable;
21+
import org.springframework.data.repository.Repository;
22+
23+
public interface CityRepository extends Repository<City, Long> {
24+
25+
Page<City> findAll(Pageable pageable);
26+
27+
Page<City> findByNameLikeAndCountryLikeAllIgnoringCase(String name, String country,
28+
Pageable pageable);
29+
30+
City findByNameAndCountryAllIgnoringCase(String name, String country);
31+
32+
}

0 commit comments

Comments
 (0)