Align couchbase more with other data-* support

pull/746/head
Dave Syer 11 years ago
parent 02d6879b52
commit e433eb20a6

@ -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);
}
}

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.boot.autoconfigure.data; package org.springframework.boot.autoconfigure.couchbase;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
@ -40,6 +40,8 @@ public class CouchbaseProperties {
private String password = ""; private String password = "";
private CouchbaseClient client;
public String getHost() { public String getHost() {
return this.host; return this.host;
} }
@ -65,8 +67,17 @@ public class CouchbaseProperties {
} }
public CouchbaseClient createClient() throws URISyntaxException, IOException { public CouchbaseClient createClient() throws URISyntaxException, IOException {
return new CouchbaseClient(Arrays.asList(new URI("http://" + getHost() if (this.client == null) {
+ ":8091/pools")), getBucket(), getPassword()); 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();
}
} }
} }

@ -16,24 +16,15 @@
package org.springframework.boot.autoconfigure.data; 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.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.autoconfigure.couchbase.CouchbaseProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; 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.CouchbaseRepository;
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories; import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
import org.springframework.data.couchbase.repository.support.CouchbaseRepositoryFactoryBean;
import com.couchbase.client.CouchbaseClient;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Couchbase * {@link EnableAutoConfiguration Auto-configuration} for Spring Data's Couchbase
@ -45,29 +36,9 @@ import com.couchbase.client.CouchbaseClient;
* @see EnableCouchbaseRepositories * @see EnableCouchbaseRepositories
*/ */
@Configuration @Configuration
@ConditionalOnClass({ CouchbaseClient.class, CouchbaseRepository.class }) @ConditionalOnClass({ CouchbaseRepository.class })
@ConditionalOnMissingBean(CouchbaseRepositoryFactoryBean.class)
@Import(CouchbaseRepositoriesAutoConfigureRegistrar.class) @Import(CouchbaseRepositoriesAutoConfigureRegistrar.class)
@EnableConfigurationProperties(CouchbaseProperties.class)
public class CouchbaseRepositoriesAutoConfiguration { 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);
}
} }

@ -13,6 +13,7 @@ org.springframework.boot.autoconfigure.data.JpaRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.data.MongoRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.MongoRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\ org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\ org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\
org.springframework.boot.autoconfigure.data.CouchbaseRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.CouchbaseRepositoriesAutoConfiguration,\
org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration,\ org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\

@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.data;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage; 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.City;
import org.springframework.boot.autoconfigure.data.couchbase.CityRepository; import org.springframework.boot.autoconfigure.data.couchbase.CityRepository;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -42,7 +43,7 @@ public class CouchbaseRepositoriesAutoConfigurationTests {
@Test @Test
public void testDefaultRepositoryConfiguration() throws Exception { public void testDefaultRepositoryConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration.class, this.context.register(TestConfiguration.class, CouchbaseAutoConfiguration.class,
CouchbaseRepositoriesAutoConfiguration.class, CouchbaseRepositoriesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
@ -53,7 +54,7 @@ public class CouchbaseRepositoriesAutoConfigurationTests {
@Test @Test
public void testNoRepositoryConfiguration() throws Exception { public void testNoRepositoryConfiguration() throws Exception {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
this.context.register(EmptyConfiguration.class, this.context.register(EmptyConfiguration.class, CouchbaseAutoConfiguration.class,
CouchbaseRepositoriesAutoConfiguration.class, CouchbaseRepositoriesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class); PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();

Loading…
Cancel
Save