From 640b9d2680739915e2eb87d5d468bdd0c621832a Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 18 Nov 2013 10:54:33 +0000 Subject: [PATCH] Add RedisAutoConfiguration --- spring-boot-autoconfigure/pom.xml | 10 ++ .../redis/RedisAutoConfiguration.java | 120 ++++++++++++++++++ .../main/resources/META-INF/spring.factories | 1 + .../redis/RedisAutoConfigurationTests.java | 44 +++++++ spring-boot-dependencies/pom.xml | 12 ++ spring-boot-starters/pom.xml | 1 + .../spring-boot-starter-redis/.gitignore | 1 + .../spring-boot-starter-redis/pom.xml | 30 +++++ 8 files changed, 219 insertions(+) create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfiguration.java create mode 100644 spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfigurationTests.java create mode 100644 spring-boot-starters/spring-boot-starter-redis/.gitignore create mode 100644 spring-boot-starters/spring-boot-starter-redis/pom.xml diff --git a/spring-boot-autoconfigure/pom.xml b/spring-boot-autoconfigure/pom.xml index b1211438cb..6ec743b350 100644 --- a/spring-boot-autoconfigure/pom.xml +++ b/spring-boot-autoconfigure/pom.xml @@ -111,6 +111,16 @@ spring-data-mongodb true + + org.springframework.data + spring-data-redis + true + + + com.lambdaworks + lettuce + true + org.springframework.security spring-security-acl diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfiguration.java new file mode 100644 index 0000000000..2fab4c9fa7 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfiguration.java @@ -0,0 +1,120 @@ +/* + * Copyright 2012-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.redis; + +import java.net.UnknownHostException; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.connection.lettuce.LettuceConnection; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; +import org.springframework.data.redis.core.RedisOperations; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.StringRedisTemplate; + +/** + * {@link EnableAutoConfiguration Auto-configuration} for Spring Data's redis support. + * + * @author Dave Syer + */ +@Configuration +@ConditionalOnClass({ LettuceConnection.class, RedisOperations.class }) +public class RedisAutoConfiguration { + + @Configuration + @EnableConfigurationProperties(RedisProperties.class) + protected static class RedisConfiguration { + + @Autowired + private RedisProperties config; + + @Bean + @ConditionalOnMissingBean(RedisConnectionFactory.class) + RedisConnectionFactory redisConnectionFactory() throws UnknownHostException { + LettuceConnectionFactory factory = new LettuceConnectionFactory( + this.config.getHost(), this.config.getPort()); + if (this.config.getPassword() != null) { + factory.setPassword(this.config.getPassword()); + } + return factory; + } + + @Bean + @ConditionalOnMissingBean(RedisOperations.class) + RedisOperations redisTemplate( + RedisConnectionFactory redisConnectionFactory) + throws UnknownHostException { + RedisTemplate template = new RedisTemplate(); + template.setConnectionFactory(redisConnectionFactory); + return template; + } + + @Bean + @ConditionalOnMissingBean(StringRedisTemplate.class) + StringRedisTemplate stringRedisTemplate( + RedisConnectionFactory redisConnectionFactory) + throws UnknownHostException { + StringRedisTemplate template = new StringRedisTemplate(); + template.setConnectionFactory(redisConnectionFactory); + return template; + } + + } + + @ConfigurationProperties(name = "spring.data.redis") + public static class RedisProperties { + + private String host = "localhost"; + + private String password; + + private int port = 6379; + + public String getHost() { + return this.host; + } + + public void setHost(String host) { + this.host = host; + } + + public int getPort() { + return this.port; + } + + public void setPort(int port) { + this.port = port; + } + + public String getPassword() { + return this.password; + } + + public void setPassword(String password) { + this.password = password; + } + + } + +} diff --git a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories index 86ecbacf01..568a9337de 100644 --- a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -7,6 +7,7 @@ org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration,\ org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\ org.springframework.boot.autoconfigure.data.JpaRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.MongoRepositoriesAutoConfiguration,\ +org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\ org.springframework.boot.autoconfigure.jms.JmsTemplateAutoConfiguration,\ diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfigurationTests.java new file mode 100644 index 0000000000..c2abc1b9f8 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/redis/RedisAutoConfigurationTests.java @@ -0,0 +1,44 @@ +/* + * Copyright 2012-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.redis; + +import org.junit.Test; +import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.data.redis.core.RedisOperations; +import org.springframework.data.redis.core.StringRedisTemplate; + +import static org.junit.Assert.assertNotNull; + +/** + * @author Dave Syer + */ +public class RedisAutoConfigurationTests { + + private AnnotationConfigApplicationContext context; + + @Test + public void testDefaultRedisConfiguration() throws Exception { + this.context = new AnnotationConfigApplicationContext(); + this.context.register(RedisAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + assertNotNull(this.context.getBean("redisTemplate", RedisOperations.class)); + assertNotNull(this.context.getBean(StringRedisTemplate.class)); + } + +} diff --git a/spring-boot-dependencies/pom.xml b/spring-boot-dependencies/pom.xml index 1504c7bcb1..6a0cad82b4 100644 --- a/spring-boot-dependencies/pom.xml +++ b/spring-boot-dependencies/pom.xml @@ -24,6 +24,7 @@ 2.3 1.2 4.11 + 2.3.3 3.0.2 1.2.17 1.0.13 @@ -40,6 +41,7 @@ 2.2.2.RELEASE 1.4.2.RELEASE 1.3.2.RELEASE + 1.1.0.RELEASE 1.2.0.RELEASE 1.1.0.RELEASE 3.2.0.RC2 @@ -81,6 +83,11 @@ commons-httpclient ${commons-httpclient.version} + + com.lambdaworks + lettuce + ${lettuce.version} + javax.servlet javax.servlet-api @@ -397,6 +404,11 @@ spring-data-mongodb ${spring-data-mongo.version} + + org.springframework.data + spring-data-redis + ${spring-data-redis.version} + org.springframework.integration spring-integration-core diff --git a/spring-boot-starters/pom.xml b/spring-boot-starters/pom.xml index 49eea2e2bd..06949189a9 100644 --- a/spring-boot-starters/pom.xml +++ b/spring-boot-starters/pom.xml @@ -26,6 +26,7 @@ spring-boot-starter-mobile spring-boot-starter-actuator spring-boot-starter-parent + spring-boot-starter-redis spring-boot-starter-security spring-boot-starter-shell-remote spring-boot-starter-test diff --git a/spring-boot-starters/spring-boot-starter-redis/.gitignore b/spring-boot-starters/spring-boot-starter-redis/.gitignore new file mode 100644 index 0000000000..ea8c4bf7f3 --- /dev/null +++ b/spring-boot-starters/spring-boot-starter-redis/.gitignore @@ -0,0 +1 @@ +/target diff --git a/spring-boot-starters/spring-boot-starter-redis/pom.xml b/spring-boot-starters/spring-boot-starter-redis/pom.xml new file mode 100644 index 0000000000..b1007ba977 --- /dev/null +++ b/spring-boot-starters/spring-boot-starter-redis/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starters + 0.5.0.BUILD-SNAPSHOT + + spring-boot-starter-redis + jar + + ${basedir}/../.. + + + + ${project.groupId} + spring-boot-starter + ${project.version} + + + org.springframework.data + spring-data-redis + + + com.lambdaworks + lettuce + + +