mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Allow to override Couchbase's CustomConversions
Closes gh-7700
This commit is contained in:
parent
73a45797c0
commit
851ce2286f
@ -30,6 +30,7 @@ import org.springframework.data.couchbase.config.AbstractCouchbaseDataConfigurat
|
||||
import org.springframework.data.couchbase.config.BeanNames;
|
||||
import org.springframework.data.couchbase.config.CouchbaseConfigurer;
|
||||
import org.springframework.data.couchbase.core.CouchbaseTemplate;
|
||||
import org.springframework.data.couchbase.core.convert.CustomConversions;
|
||||
import org.springframework.data.couchbase.core.mapping.Document;
|
||||
import org.springframework.data.couchbase.core.query.Consistency;
|
||||
import org.springframework.data.couchbase.repository.support.IndexManager;
|
||||
@ -81,6 +82,13 @@ class SpringBootCouchbaseDataConfiguration extends AbstractCouchbaseDataConfigur
|
||||
return super.couchbaseTemplate();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ConditionalOnMissingBean(name = BeanNames.COUCHBASE_CUSTOM_CONVERSIONS)
|
||||
@Bean(name = BeanNames.COUCHBASE_CUSTOM_CONVERSIONS)
|
||||
public CustomConversions customConversions() {
|
||||
return super.customConversions();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ConditionalOnMissingBean(name = BeanNames.COUCHBASE_INDEX_MANAGER)
|
||||
@Bean(name = BeanNames.COUCHBASE_INDEX_MANAGER)
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.data.couchbase;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.After;
|
||||
@ -23,17 +24,22 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.couchbase.CouchbaseProperties;
|
||||
import org.springframework.boot.autoconfigure.couchbase.CouchbaseTestConfigurer;
|
||||
import org.springframework.boot.autoconfigure.data.couchbase.city.City;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration;
|
||||
import org.springframework.boot.test.util.EnvironmentTestUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.data.couchbase.config.AbstractCouchbaseDataConfiguration;
|
||||
import org.springframework.data.couchbase.config.BeanNames;
|
||||
import org.springframework.data.couchbase.config.CouchbaseConfigurer;
|
||||
import org.springframework.data.couchbase.core.CouchbaseTemplate;
|
||||
import org.springframework.data.couchbase.core.convert.CustomConversions;
|
||||
import org.springframework.data.couchbase.core.mapping.CouchbaseMappingContext;
|
||||
import org.springframework.data.couchbase.core.mapping.event.ValidatingCouchbaseEventListener;
|
||||
import org.springframework.data.couchbase.core.query.Consistency;
|
||||
@ -119,6 +125,14 @@ public class CouchbaseDataAutoConfigurationTests {
|
||||
assertThat(initialEntitySet).containsOnly(City.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customConversions() {
|
||||
load(CustomConversionsConfig.class);
|
||||
CouchbaseTemplate template = this.context.getBean(CouchbaseTemplate.class);
|
||||
assertThat(template.getConverter().getConversionService()
|
||||
.canConvert(CouchbaseProperties.class, Boolean.class)).isTrue();
|
||||
}
|
||||
|
||||
private void load(Class<?> config, String... environment) {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(context, environment);
|
||||
@ -147,6 +161,17 @@ public class CouchbaseDataAutoConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import(CouchbaseTestConfigurer.class)
|
||||
static class CustomConversionsConfig {
|
||||
|
||||
@Bean(BeanNames.COUCHBASE_CUSTOM_CONVERSIONS)
|
||||
public CustomConversions myCustomConversions() {
|
||||
return new CustomConversions(Collections.singletonList(new MyConverter()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EntityScan("org.springframework.boot.autoconfigure.data.couchbase.city")
|
||||
@Import(CustomCouchbaseConfiguration.class)
|
||||
@ -154,4 +179,12 @@ public class CouchbaseDataAutoConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
static class MyConverter implements Converter<CouchbaseProperties, Boolean> {
|
||||
|
||||
@Override
|
||||
public Boolean convert(CouchbaseProperties value) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3789,10 +3789,7 @@ http://docs.spring.io/spring-data/couchbase/docs/current/reference/html/[referen
|
||||
|
||||
You can inject an auto-configured `CouchbaseTemplate` instance as you would with any
|
||||
other Spring Bean as long as a _default_ `CouchbaseConfigurer` is available (that
|
||||
happens when you enable the couchbase support as explained above). If you want to
|
||||
bypass the auto-configuration for Spring Data Couchbase, provide your own
|
||||
`org.springframework.data.couchbase.config.AbstractCouchbaseDataConfiguration`
|
||||
implementation.
|
||||
happens when you enable the couchbase support as explained above).
|
||||
|
||||
|
||||
[source,java,indent=0]
|
||||
@ -3812,8 +3809,35 @@ implementation.
|
||||
}
|
||||
----
|
||||
|
||||
If you add a `@Bean` of your own of type `CouchbaseTemplate` named `couchbaseTemplate` it
|
||||
will replace the default.
|
||||
There are a few beans that you can define in your own configuration to override those
|
||||
provided by the auto-configuration:
|
||||
|
||||
* A `CouchbaseTemplate` `@Bean` with name `couchbaseTemplate`
|
||||
* An `IndexManager` `@Bean` with name `couchbaseIndexManager`
|
||||
* A `CustomConversions` `@Bean` with name `couchbaseCustomConversions`
|
||||
|
||||
To avoid hard-coding those names in your own config, you can reuse `BeanNames` provided
|
||||
by Spring Data Couchbase. For instance, you can customize the converters to use as
|
||||
follows:
|
||||
|
||||
[source,java,indent=0]
|
||||
----
|
||||
@Configuration
|
||||
public class SomeConfiguration {
|
||||
|
||||
@Bean(BeanNames.COUCHBASE_CUSTOM_CONVERSIONS)
|
||||
public CustomConversions myCustomConversions() {
|
||||
return new CustomConversions(...);
|
||||
}
|
||||
|
||||
// ...
|
||||
|
||||
}
|
||||
----
|
||||
|
||||
TIP: If you want to fully bypass the auto-configuration for Spring Data Couchbase, provide
|
||||
your own `org.springframework.data.couchbase.config.AbstractCouchbaseDataConfiguration`
|
||||
implementation.
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user