Skip to content

Commit a243b3e

Browse files
committed
Merge pull request #5455 from eddumelendez/gh-5448
* pr/5455: Polish contribution Auto-configure Redis repositories
2 parents b8bc4f6 + 96b7419 commit a243b3e

File tree

9 files changed

+339
-0
lines changed

9 files changed

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

spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

+6
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@
9090
"description": "Enable Mongo repositories.",
9191
"defaultValue": true
9292
},
93+
{
94+
"name": "spring.data.redis.repositories.enabled",
95+
"type": "java.lang.Boolean",
96+
"description": "Enable Redis repositories.",
97+
"defaultValue": true
98+
},
9399
{
94100
"name": "spring.data.solr.repositories.enabled",
95101
"type": "java.lang.Boolean",

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,24 @@
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+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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.junit.After;
20+
import org.junit.Rule;
21+
import org.junit.Test;
22+
23+
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
24+
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
25+
import org.springframework.boot.autoconfigure.data.alt.redis.CityRedisRepository;
26+
import org.springframework.boot.autoconfigure.data.empty.EmptyDataPackage;
27+
import org.springframework.boot.autoconfigure.data.redis.city.City;
28+
import org.springframework.boot.autoconfigure.data.redis.city.CityRepository;
29+
import org.springframework.boot.redis.RedisTestServer;
30+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
31+
import org.springframework.context.annotation.Configuration;
32+
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
36+
/**
37+
* Tests for {@link RedisRepositoriesAutoConfiguration}.
38+
*
39+
* @author Eddú Meléndez
40+
*/
41+
public class RedisRepositoriesAutoConfigurationTests {
42+
43+
@Rule
44+
public RedisTestServer redis = new RedisTestServer();
45+
46+
private AnnotationConfigApplicationContext context
47+
= new AnnotationConfigApplicationContext();
48+
49+
@After
50+
public void close() {
51+
this.context.close();
52+
}
53+
54+
@Test
55+
public void testDefaultRepositoryConfiguration() {
56+
this.context.register(TestConfiguration.class,
57+
RedisAutoConfiguration.class, RedisRepositoriesAutoConfiguration.class,
58+
PropertyPlaceholderAutoConfiguration.class);
59+
this.context.refresh();
60+
assertThat(this.context.getBean(CityRepository.class)).isNotNull();
61+
}
62+
63+
@Test
64+
public void testNoRepositoryConfiguration() {
65+
this.context.register(EmptyConfiguration.class,
66+
RedisAutoConfiguration.class,
67+
RedisRepositoriesAutoConfiguration.class,
68+
PropertyPlaceholderAutoConfiguration.class);
69+
this.context.refresh();
70+
assertThat(this.context.getBean("redisTemplate")).isNotNull();
71+
}
72+
73+
@Test
74+
public void doesNotTriggerDefaultRepositoryDetectionIfCustomized() {
75+
this.context.register(CustomizedConfiguration.class,
76+
RedisAutoConfiguration.class,
77+
RedisRepositoriesAutoConfiguration.class,
78+
PropertyPlaceholderAutoConfiguration.class);
79+
this.context.refresh();
80+
assertThat(this.context.getBean(CityRedisRepository.class)).isNotNull();
81+
}
82+
83+
@Configuration
84+
@TestAutoConfigurationPackage(City.class)
85+
protected static class TestConfiguration {
86+
87+
}
88+
89+
@Configuration
90+
@TestAutoConfigurationPackage(EmptyDataPackage.class)
91+
protected static class EmptyConfiguration {
92+
93+
}
94+
95+
@Configuration
96+
@TestAutoConfigurationPackage(RedisRepositoriesAutoConfigurationTests.class)
97+
@EnableRedisRepositories(basePackageClasses = CityRedisRepository.class)
98+
static class CustomizedConfiguration {
99+
100+
}
101+
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
70+
}
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+
}

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

+3
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,9 @@ content into your application; rather pick only the properties that you need.
533533
spring.data.mongodb.uri=mongodb://localhost/test # Mongo database URI. When set, host and port are ignored.
534534
spring.data.mongodb.username= # Login user of the mongo server.
535535
536+
# DATA REDIS
537+
spring.data.redis.repositories.enabled=true # Enable Redis repositories.
538+
536539
# DATA REST ({sc-spring-boot-autoconfigure}/data/rest/RepositoryRestProperties.{sc-ext}[RepositoryRestProperties])
537540
spring.data.rest.base-path= # Base path to be used by Spring Data REST to expose repository resources.
538541
spring.data.rest.default-page-size= # Default size of pages.

0 commit comments

Comments
 (0)