Skip to content

Commit 2fc2ec4

Browse files
eddumelendezsnicoll
authored andcommitted
Auto-configure Redis repositories
Closes gh-5448
1 parent b8bc4f6 commit 2fc2ec4

File tree

7 files changed

+329
-0
lines changed

7 files changed

+329
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.EnableAutoConfiguration;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.context.annotation.Import;
27+
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
28+
import org.springframework.data.redis.repository.support.RedisRepositoryFactoryBean;
29+
30+
/**
31+
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Redis
32+
* Repositories.
33+
*
34+
* @author Eddú Meléndez
35+
* @see EnableRedisRepositories
36+
* @since 1.4.0
37+
*/
38+
@Configuration
39+
@ConditionalOnClass({ Jedis.class, EnableRedisRepositories.class })
40+
@ConditionalOnProperty(prefix = "spring.data.redis.repositories", name = "enabled", havingValue = "true", matchIfMissing = true)
41+
@ConditionalOnMissingBean(RedisRepositoryFactoryBean.class)
42+
@Import(RedisRepositoriesAutoConfigureRegistrar.class)
43+
public class RedisRepositoriesAutoConfiguration {
44+
}
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 EnableRedisRepositoriesConfiguration.class;
43+
}
44+
45+
@Override
46+
protected RepositoryConfigurationExtension getRepositoryConfigurationExtension() {
47+
return new RedisRepositoryConfigurationExtension();
48+
}
49+
50+
@EnableRedisRepositories
51+
private static class EnableRedisRepositoriesConfiguration {
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,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,104 @@
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+
48+
@After
49+
public void close() {
50+
this.context.close();
51+
}
52+
53+
@Test
54+
public void testDefaultRepositoryConfiguration() {
55+
this.context = new AnnotationConfigApplicationContext();
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 = new AnnotationConfigApplicationContext();
66+
this.context.register(EmptyConfiguration.class,
67+
RedisAutoConfiguration.class,
68+
RedisRepositoriesAutoConfiguration.class,
69+
PropertyPlaceholderAutoConfiguration.class);
70+
this.context.refresh();
71+
assertThat(this.context.getBean("redisTemplate")).isNotNull();
72+
}
73+
74+
@Test
75+
public void doesNotTriggerDefaultRepositoryDetectionIfCustomized() {
76+
this.context = new AnnotationConfigApplicationContext();
77+
this.context.register(CustomizedConfiguration.class,
78+
RedisAutoConfiguration.class,
79+
RedisRepositoriesAutoConfiguration.class,
80+
PropertyPlaceholderAutoConfiguration.class);
81+
this.context.refresh();
82+
assertThat(this.context.getBean(CityRedisRepository.class)).isNotNull();
83+
}
84+
85+
@Configuration
86+
@TestAutoConfigurationPackage(City.class)
87+
protected static class TestConfiguration {
88+
89+
}
90+
91+
@Configuration
92+
@TestAutoConfigurationPackage(EmptyDataPackage.class)
93+
protected static class EmptyConfiguration {
94+
95+
}
96+
97+
@Configuration
98+
@TestAutoConfigurationPackage(RedisRepositoriesAutoConfigurationTests.class)
99+
@EnableRedisRepositories(basePackageClasses = CityRedisRepository.class)
100+
static class CustomizedConfiguration {
101+
102+
}
103+
104+
}
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+
}

0 commit comments

Comments
 (0)