diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfiguration.java index e64f047e95..e1cf2ec787 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 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. @@ -45,10 +45,12 @@ public class InfluxDbAutoConfiguration { @Bean @ConditionalOnMissingBean @ConditionalOnProperty("spring.influx.url") - public InfluxDB influxDb(InfluxDbProperties properties, - ObjectProvider builder) { - return new InfluxDBImpl(properties.getUrl(), properties.getUser(), properties.getPassword(), + public InfluxDB influxDb(InfluxDbProperties properties, ObjectProvider builder, + ObjectProvider customizers) { + InfluxDB influxDb = new InfluxDBImpl(properties.getUrl(), properties.getUser(), properties.getPassword(), determineBuilder(builder.getIfAvailable())); + customizers.orderedStream().forEach((customizer) -> customizer.customize(influxDb)); + return influxDb; } private static OkHttpClient.Builder determineBuilder(InfluxDbOkHttpClientBuilderProvider builder) { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbCustomizer.java new file mode 100644 index 0000000000..759eda4c8a --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbCustomizer.java @@ -0,0 +1,37 @@ +/* + * Copyright 2012-2021 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 + * + * https://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.influx; + +import org.influxdb.InfluxDB; + +/** + * Callback interface that can be implemented by beans wishing to further customize + * {@code InfluxDB} whilst retaining default auto-configuration. + * + * @author EddĂș MelĂ©ndez + * @since 2.5.0 + */ +@FunctionalInterface +public interface InfluxDbCustomizer { + + /** + * Customize the {@link InfluxDB}. + * @param influxDB the influxDB instance to customize + */ + void customize(InfluxDB influxDB); + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java index a3b22d8a5c..556cb6d3c1 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 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. @@ -46,21 +46,21 @@ class InfluxDbAutoConfigurationTests { @Test void influxDbRequiresUrl() { - this.contextRunner.run((context) -> assertThat(context.getBeansOfType(InfluxDB.class)).isEmpty()); + this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(InfluxDB.class)); } @Test void influxDbCanBeCustomized() { this.contextRunner - .withPropertyValues("spring.influx.url=http://localhost", "spring.influx.password:password", - "spring.influx.user:user") - .run(((context) -> assertThat(context.getBeansOfType(InfluxDB.class)).hasSize(1))); + .withPropertyValues("spring.influx.url=http://localhost", "spring.influx.user=user", + "spring.influx.password=password") + .run((context) -> assertThat(context).hasSingleBean(InfluxDB.class)); } @Test void influxDbCanBeCreatedWithoutCredentials() { this.contextRunner.withPropertyValues("spring.influx.url=http://localhost").run((context) -> { - assertThat(context.getBeansOfType(InfluxDB.class)).hasSize(1); + assertThat(context).hasSingleBean(InfluxDB.class); int readTimeout = getReadTimeoutProperty(context); assertThat(readTimeout).isEqualTo(10_000); }); @@ -70,12 +70,23 @@ class InfluxDbAutoConfigurationTests { void influxDbWithOkHttpClientBuilderProvider() { this.contextRunner.withUserConfiguration(CustomOkHttpClientBuilderProviderConfig.class) .withPropertyValues("spring.influx.url=http://localhost").run((context) -> { - assertThat(context.getBeansOfType(InfluxDB.class)).hasSize(1); + assertThat(context).hasSingleBean(InfluxDB.class); int readTimeout = getReadTimeoutProperty(context); assertThat(readTimeout).isEqualTo(40_000); }); } + @Test + void influxDbWithCustomizer() { + this.contextRunner.withBean(InfluxDbCustomizer.class, () -> (influxDb) -> influxDb.setDatabase("test")) + .withPropertyValues("spring.influx.url=http://localhost", "spring.influx.database=sample-db") + .run((context) -> { + assertThat(context).hasSingleBean(InfluxDB.class); + InfluxDB influxDb = context.getBean(InfluxDB.class); + assertThat(influxDb).hasFieldOrPropertyWithValue("database", "test"); + }); + } + private int getReadTimeoutProperty(AssertableApplicationContext context) { InfluxDB influxDB = context.getBean(InfluxDB.class); Retrofit retrofit = (Retrofit) ReflectionTestUtils.getField(influxDB, "retrofit"); diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc index eae98d701b..ff9a68ee94 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc @@ -4922,6 +4922,8 @@ If the connection to InfluxDB requires a user and password, you can set the `spr InfluxDB relies on OkHttp. If you need to tune the http client `InfluxDB` uses behind the scenes, you can register an `InfluxDbOkHttpClientBuilderProvider` bean. +If you need more control over the configuration, consider registering a `InfluxDbCustomizer` bean. + [[boot-features-caching]]