Skip to content

Commit 297127e

Browse files
committed
Polish "Add influxDB java client auto-configuration"
Closes gh-9066
1 parent 6a70b90 commit 297127e

File tree

8 files changed

+153
-127
lines changed

8 files changed

+153
-127
lines changed

spring-boot-autoconfigure/pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,11 @@
641641
<artifactId>aspectjweaver</artifactId>
642642
<optional>true</optional>
643643
</dependency>
644+
<dependency>
645+
<groupId>org.influxdb</groupId>
646+
<artifactId>influxdb-java</artifactId>
647+
<optional>true</optional>
648+
</dependency>
644649
<dependency>
645650
<groupId>org.jooq</groupId>
646651
<artifactId>jooq</artifactId>
@@ -661,11 +666,6 @@
661666
<artifactId>quartz</artifactId>
662667
<optional>true</optional>
663668
</dependency>
664-
<dependency>
665-
<groupId>org.influxdb</groupId>
666-
<artifactId>influxdb-java</artifactId>
667-
<optional>true</optional>
668-
</dependency>
669669
<!-- Annotation processing -->
670670
<dependency>
671671
<groupId>org.springframework.boot</groupId>
Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2424
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2525
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2627
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2728
import org.springframework.context.annotation.Bean;
2829
import org.springframework.context.annotation.Configuration;
@@ -31,32 +32,32 @@
3132
* {@link EnableAutoConfiguration Auto-configuration} for InfluxDB.
3233
*
3334
* @author Sergey Kuptsov
35+
* @author Stephane Nicoll
36+
* @since 2.0.0
3437
*/
3538
@Configuration
3639
@ConditionalOnClass(InfluxDB.class)
37-
@EnableConfigurationProperties(InfluxDBProperties.class)
38-
public class InfluxDBAutoConfiguration {
40+
@EnableConfigurationProperties(InfluxDbProperties.class)
41+
public class InfluxDbAutoConfiguration {
3942

40-
private final InfluxDBProperties properties;
43+
private final InfluxDbProperties properties;
4144

42-
public InfluxDBAutoConfiguration(InfluxDBProperties properties) {
45+
public InfluxDbAutoConfiguration(InfluxDbProperties properties) {
4346
this.properties = properties;
4447
}
4548

4649
@Bean
4750
@ConditionalOnMissingBean
48-
public InfluxDB influxDB() {
49-
if (Strings.isNullOrEmpty(this.properties.getUser())) {
50-
return InfluxDBFactory.connect(
51-
this.properties.getUrl()
52-
);
51+
@ConditionalOnProperty("spring.influx.client.url")
52+
public InfluxDB influxDb() {
53+
InfluxDbProperties.Client client = this.properties.getClient();
54+
if (Strings.isNullOrEmpty(client.getUser())) {
55+
return InfluxDBFactory.connect(client.getUrl());
5356
}
5457
else {
55-
return InfluxDBFactory.connect(
56-
this.properties.getUrl(),
57-
this.properties.getUser(),
58-
this.properties.getPassword()
59-
);
58+
return InfluxDBFactory.connect(client.getUrl(), client.getUser(),
59+
client.getPassword());
6060
}
6161
}
62+
6263
}
Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,46 +22,59 @@
2222
* Configuration properties for InfluxDB.
2323
*
2424
* @author Sergey Kuptsov
25+
* @author Stephane Nicoll
26+
* @since 2.0.0
2527
*/
26-
@ConfigurationProperties(prefix = "spring.data.influx")
27-
public class InfluxDBProperties {
28-
29-
/**
30-
* The url to connect to.
31-
*/
32-
private String url;
33-
34-
/**
35-
* The username which is used to authorize against the influxDB instance.
36-
*/
37-
private String user;
38-
39-
/**
40-
* The password for the username which is used to authorize against the influxDB.
41-
*/
42-
private String password;
43-
44-
public String getUrl() {
45-
return this.url;
46-
}
28+
@ConfigurationProperties(prefix = "spring.influx")
29+
public class InfluxDbProperties {
4730

48-
public void setUrl(String url) {
49-
this.url = url;
50-
}
31+
private final Client client = new Client();
5132

52-
public String getUser() {
53-
return this.user;
33+
public Client getClient() {
34+
return this.client;
5435
}
5536

56-
public void setUser(String user) {
57-
this.user = user;
58-
}
37+
public static class Client {
5938

60-
public String getPassword() {
61-
return this.password;
62-
}
39+
/**
40+
* Url of the InfluxDB instance to connect to.
41+
*/
42+
private String url;
43+
44+
/**
45+
* Login user.
46+
*/
47+
private String user;
48+
49+
/**
50+
* Login password.
51+
*/
52+
private String password;
53+
54+
public String getUrl() {
55+
return this.url;
56+
}
57+
58+
public void setUrl(String url) {
59+
this.url = url;
60+
}
61+
62+
public String getUser() {
63+
return this.user;
64+
}
65+
66+
public void setUser(String user) {
67+
this.user = user;
68+
}
69+
70+
public String getPassword() {
71+
return this.password;
72+
}
73+
74+
public void setPassword(String password) {
75+
this.password = password;
76+
}
6377

64-
public void setPassword(String password) {
65-
this.password = password;
6678
}
79+
6780
}

spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
6262
org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,\
6363
org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,\
6464
org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,\
65+
org.springframework.boot.autoconfigure.influx.InfluxDbAutoConfiguration,\
6566
org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration,\
6667
org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\
6768
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
@@ -122,8 +123,7 @@ org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\
122123
org.springframework.boot.autoconfigure.websocket.reactive.WebSocketReactiveAutoConfiguration,\
123124
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketServletAutoConfiguration,\
124125
org.springframework.boot.autoconfigure.websocket.servlet.WebSocketMessagingAutoConfiguration,\
125-
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration,\
126-
org.springframework.boot.autoconfigure.influx.InfluxDBAutoConfiguration
126+
org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration
127127

128128
# Failure analyzers
129129
org.springframework.boot.diagnostics.FailureAnalyzer=\

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDBAutoConfigurationTest.java

Lines changed: 0 additions & 66 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.influx;
18+
19+
import org.influxdb.InfluxDB;
20+
import org.junit.After;
21+
import org.junit.Test;
22+
23+
import org.springframework.boot.test.util.TestPropertyValues;
24+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link InfluxDbAutoConfiguration}.
30+
*
31+
* @author Sergey Kuptsov
32+
* @author Stephane Nicoll
33+
*/
34+
public class InfluxDbAutoConfigurationTest {
35+
36+
private AnnotationConfigApplicationContext context;
37+
38+
@After
39+
public void tearDown() {
40+
if (this.context != null) {
41+
this.context.close();
42+
}
43+
}
44+
45+
@Test
46+
public void clientRequiresUrl() {
47+
load();
48+
assertThat(this.context.getBeansOfType(InfluxDB.class)).isEmpty();
49+
}
50+
51+
@Test
52+
public void clientCanBeCustomized() {
53+
load("spring.influx.client.url=http://localhost",
54+
"spring.influx.client.password:password",
55+
"spring.influx.client.user:user");
56+
assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1);
57+
}
58+
59+
@Test
60+
public void clientCanBeCreatedWithoutCredentials() {
61+
load("spring.influx.client.url=http://localhost");
62+
assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1);
63+
}
64+
65+
private void load(String... environment) {
66+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
67+
TestPropertyValues.of(environment).applyTo(ctx);
68+
ctx.register(InfluxDbAutoConfiguration.class);
69+
ctx.refresh();
70+
this.context = ctx;
71+
}
72+
73+
}

spring-boot-dependencies/pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
<httpclient.version>4.5.3</httpclient.version>
9494
<httpcore.version>4.4.6</httpcore.version>
9595
<infinispan.version>8.2.6.Final</infinispan.version>
96+
<influxdb-java.version>2.5</influxdb-java.version>
9697
<jackson.version>2.9.0.pr3</jackson.version>
9798
<janino.version>3.0.7</janino.version>
9899
<javassist.version>3.21.0-GA</javassist.version> <!-- Same as Hibernate -->
@@ -190,7 +191,6 @@
190191
<webjars-locator.version>0.32-1</webjars-locator.version>
191192
<wsdl4j.version>1.6.3</wsdl4j.version>
192193
<xml-apis.version>1.4.01</xml-apis.version>
193-
<influxdb-java.version>2.5</influxdb-java.version>
194194
<!-- Plugin versions -->
195195
<build-helper-maven-plugin.version>1.10</build-helper-maven-plugin.version>
196196
<exec-maven-plugin.version>1.5.0</exec-maven-plugin.version>
@@ -1857,6 +1857,11 @@
18571857
</exclusion>
18581858
</exclusions>
18591859
</dependency>
1860+
<dependency>
1861+
<groupId>org.influxdb</groupId>
1862+
<artifactId>influxdb-java</artifactId>
1863+
<version>${influxdb-java.version}</version>
1864+
</dependency>
18601865
<dependency>
18611866
<groupId>org.javassist</groupId>
18621867
<artifactId>javassist</artifactId>
@@ -2473,11 +2478,6 @@
24732478
<artifactId>xml-apis</artifactId>
24742479
<version>${xml-apis.version}</version>
24752480
</dependency>
2476-
<dependency>
2477-
<groupId>org.influxdb</groupId>
2478-
<artifactId>influxdb-java</artifactId>
2479-
<version>${influxdb-java.version}</version>
2480-
</dependency>
24812481
</dependencies>
24822482
</dependencyManagement>
24832483
<build>

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,11 @@ content into your application; rather pick only the properties that you need.
714714
spring.h2.console.settings.trace=false # Enable trace output.
715715
spring.h2.console.settings.web-allow-others=false # Enable remote access.
716716
717+
# InfluxDB ({sc-spring-boot-autoconfigure}/influx/InfluxProperties.{sc-ext}[InfluxProperties])
718+
spring.influx.client.password= # Login password.
719+
spring.influx.client.url= # Url of the InfluxDB instance to connect to.
720+
spring.influx.client.user= # Login user.
721+
717722
# JOOQ ({sc-spring-boot-autoconfigure}/jooq/JooqAutoConfiguration.{sc-ext}[JooqAutoConfiguration])
718723
spring.jooq.sql-dialect= # Sql dialect to use, auto-detected by default.
719724

0 commit comments

Comments
 (0)