From e433eb20a6135f8f53be461badd22c68d13cd080 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 28 Apr 2014 10:50:43 +0100 Subject: [PATCH] Align couchbase more with other data-* support --- .../couchbase/CouchbaseAutoConfiguration.java | 70 +++++++++++++++++++ .../CouchbaseProperties.java | 17 ++++- ...ouchbaseRepositoriesAutoConfiguration.java | 37 ++-------- .../main/resources/META-INF/spring.factories | 1 + ...aseRepositoriesAutoConfigurationTests.java | 5 +- 5 files changed, 92 insertions(+), 38 deletions(-) create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfiguration.java rename spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/{data => couchbase}/CouchbaseProperties.java (81%) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfiguration.java new file mode 100644 index 0000000000..9bb7d3651b --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseAutoConfiguration.java @@ -0,0 +1,70 @@ +/* + * Copyright 2012-2014 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.couchbase; + +import java.io.IOException; +import java.net.URISyntaxException; + +import javax.annotation.PreDestroy; + +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.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.couchbase.core.CouchbaseTemplate; +import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories; + +import com.couchbase.client.CouchbaseClient; + +/** + * {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Couchbase + * Repositories. + * + * @author Michael Nitschinger + * @since 1.1.0 + * @see CouchbaseProperties + * @see EnableCouchbaseRepositories + */ +@Configuration +@ConditionalOnClass({ CouchbaseClient.class, CouchbaseTemplate.class }) +@EnableConfigurationProperties(CouchbaseProperties.class) +public class CouchbaseAutoConfiguration { + + @Autowired + private CouchbaseProperties properties; + + @PreDestroy + public void close() throws URISyntaxException, IOException { + this.properties.closeClient(); + } + + @Bean + @ConditionalOnMissingBean(CouchbaseClient.class) + public CouchbaseClient couchbaseClient() throws URISyntaxException, IOException { + return this.properties.createClient(); + } + + @Bean + @ConditionalOnMissingBean(CouchbaseTemplate.class) + public CouchbaseTemplate couchbaseTemplate(CouchbaseClient couchbaseClient) { + return new CouchbaseTemplate(couchbaseClient); + } + +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/CouchbaseProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseProperties.java similarity index 81% rename from spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/CouchbaseProperties.java rename to spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseProperties.java index 8bdc5d1558..e8a23a43c2 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/CouchbaseProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseProperties.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.autoconfigure.data; +package org.springframework.boot.autoconfigure.couchbase; import java.io.IOException; import java.net.URI; @@ -40,6 +40,8 @@ public class CouchbaseProperties { private String password = ""; + private CouchbaseClient client; + public String getHost() { return this.host; } @@ -65,8 +67,17 @@ public class CouchbaseProperties { } public CouchbaseClient createClient() throws URISyntaxException, IOException { - return new CouchbaseClient(Arrays.asList(new URI("http://" + getHost() - + ":8091/pools")), getBucket(), getPassword()); + if (this.client == null) { + this.client = new CouchbaseClient(Arrays.asList(new URI("http://" + getHost() + + ":8091/pools")), getBucket(), getPassword()); + } + return this.client; + } + + public void closeClient() { + if (this.client != null) { + this.client.shutdown(); + } } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/CouchbaseRepositoriesAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/CouchbaseRepositoriesAutoConfiguration.java index e28151a56a..dd3542ceba 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/CouchbaseRepositoriesAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/CouchbaseRepositoriesAutoConfiguration.java @@ -16,24 +16,15 @@ package org.springframework.boot.autoconfigure.data; -import java.io.IOException; -import java.net.URISyntaxException; - -import javax.annotation.PreDestroy; - -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.EnableConfigurationProperties; -import org.springframework.context.annotation.Bean; +import org.springframework.boot.autoconfigure.couchbase.CouchbaseProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; -import org.springframework.data.couchbase.core.CouchbaseTemplate; import org.springframework.data.couchbase.repository.CouchbaseRepository; import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories; - -import com.couchbase.client.CouchbaseClient; +import org.springframework.data.couchbase.repository.support.CouchbaseRepositoryFactoryBean; /** * {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Couchbase @@ -45,29 +36,9 @@ import com.couchbase.client.CouchbaseClient; * @see EnableCouchbaseRepositories */ @Configuration -@ConditionalOnClass({ CouchbaseClient.class, CouchbaseRepository.class }) +@ConditionalOnClass({ CouchbaseRepository.class }) +@ConditionalOnMissingBean(CouchbaseRepositoryFactoryBean.class) @Import(CouchbaseRepositoriesAutoConfigureRegistrar.class) -@EnableConfigurationProperties(CouchbaseProperties.class) public class CouchbaseRepositoriesAutoConfiguration { - @Autowired - private CouchbaseProperties properties; - - @PreDestroy - public void close() throws URISyntaxException, IOException { - couchbaseClient().shutdown(); - } - - @Bean - @ConditionalOnMissingBean(CouchbaseClient.class) - CouchbaseClient couchbaseClient() throws URISyntaxException, IOException { - return this.properties.createClient(); - } - - @Bean - @ConditionalOnMissingBean(CouchbaseTemplate.class) - CouchbaseTemplate couchbaseTemplate(CouchbaseClient couchbaseClient) { - return new CouchbaseTemplate(couchbaseClient); - } - } 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 439277105d..0b74387da5 100644 --- a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -13,6 +13,7 @@ org.springframework.boot.autoconfigure.data.JpaRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.MongoRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\ org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\ +org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\ org.springframework.boot.autoconfigure.data.CouchbaseRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\ diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/CouchbaseRepositoriesAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/CouchbaseRepositoriesAutoConfigurationTests.java index cdd9bbd8da..104247da08 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/CouchbaseRepositoriesAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/CouchbaseRepositoriesAutoConfigurationTests.java @@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.data; import org.junit.Test; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage; +import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration; import org.springframework.boot.autoconfigure.data.couchbase.City; import org.springframework.boot.autoconfigure.data.couchbase.CityRepository; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -42,7 +43,7 @@ public class CouchbaseRepositoriesAutoConfigurationTests { @Test public void testDefaultRepositoryConfiguration() throws Exception { this.context = new AnnotationConfigApplicationContext(); - this.context.register(TestConfiguration.class, + this.context.register(TestConfiguration.class, CouchbaseAutoConfiguration.class, CouchbaseRepositoriesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); @@ -53,7 +54,7 @@ public class CouchbaseRepositoriesAutoConfigurationTests { @Test public void testNoRepositoryConfiguration() throws Exception { this.context = new AnnotationConfigApplicationContext(); - this.context.register(EmptyConfiguration.class, + this.context.register(EmptyConfiguration.class, CouchbaseAutoConfiguration.class, CouchbaseRepositoriesAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh();