Skip to content

Commit 2ae1435

Browse files
committed
Polish
1 parent 24f09e2 commit 2ae1435

File tree

5 files changed

+149
-16
lines changed

5 files changed

+149
-16
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/EnvironmentInfoContributor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.actuate.info;
1818

19+
import org.springframework.boot.bind.PropertySourcesBinder;
1920
import org.springframework.core.env.ConfigurableEnvironment;
2021

2122
/**

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/InfoPropertiesInfoContributor.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import java.util.Map;
2121
import java.util.Properties;
2222

23+
import org.springframework.boot.bind.PropertySourcesBinder;
2324
import org.springframework.boot.info.InfoProperties;
24-
import org.springframework.core.env.MutablePropertySources;
2525
import org.springframework.core.env.PropertySource;
2626
import org.springframework.util.StringUtils;
2727

@@ -84,9 +84,7 @@ protected Map<String, Object> generateContent() {
8484
* @return the raw content
8585
*/
8686
protected Map<String, Object> extractContent(PropertySource<?> propertySource) {
87-
MutablePropertySources propertySources = new MutablePropertySources();
88-
propertySources.addFirst(propertySource);
89-
return new PropertySourcesBinder(propertySources).extractAll("");
87+
return new PropertySourcesBinder(propertySource).extractAll("");
9088
}
9189

9290
/**

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/EndpointAutoConfigurationTests.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,13 @@
4949
import org.springframework.boot.autoconfigure.info.ProjectInfoProperties;
5050
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
5151
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
52-
import org.springframework.boot.bind.PropertiesConfigurationFactory;
52+
import org.springframework.boot.bind.PropertySourcesBinder;
5353
import org.springframework.boot.test.EnvironmentTestUtils;
5454
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5555
import org.springframework.context.annotation.Bean;
5656
import org.springframework.context.annotation.Configuration;
5757
import org.springframework.core.annotation.Order;
58+
import org.springframework.core.env.PropertiesPropertySource;
5859
import org.springframework.core.io.Resource;
5960
import org.springframework.core.io.support.PropertiesLoaderUtils;
6061
import org.springframework.validation.BindException;
@@ -262,18 +263,15 @@ public InfoContributor myAnotherContributor(ProjectInfoProperties properties)
262263

263264
private static class GitFullInfoContributor implements InfoContributor {
264265

265-
private final Map<String, Object> content;
266+
private Map<String, Object> content = new LinkedHashMap<String, Object>();
266267

267268
GitFullInfoContributor(Resource location) throws BindException, IOException {
268-
this.content = new LinkedHashMap<String, Object>();
269269
if (location.exists()) {
270-
PropertiesConfigurationFactory<Map<String, Object>> factory = new PropertiesConfigurationFactory<Map<String, Object>>(
271-
this.content);
272-
factory.setTargetName("git");
273270
Properties gitInfoProperties = PropertiesLoaderUtils
274271
.loadProperties(location);
275-
factory.setProperties(gitInfoProperties);
276-
factory.bindPropertiesToTarget();
272+
PropertiesPropertySource gitPropertySource =
273+
new PropertiesPropertySource("git", gitInfoProperties);
274+
this.content = new PropertySourcesBinder(gitPropertySource).extractAll("git");
277275
}
278276
}
279277

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.actuate.info;
17+
package org.springframework.boot.bind;
1818

1919
import java.util.LinkedHashMap;
2020
import java.util.Map;
2121

22-
import org.springframework.boot.bind.PropertiesConfigurationFactory;
22+
import org.springframework.core.convert.ConversionService;
2323
import org.springframework.core.env.ConfigurableEnvironment;
24+
import org.springframework.core.env.Environment;
25+
import org.springframework.core.env.MutablePropertySources;
26+
import org.springframework.core.env.PropertySource;
2427
import org.springframework.core.env.PropertySources;
2528
import org.springframework.util.StringUtils;
2629
import org.springframework.validation.BindException;
@@ -33,20 +36,50 @@
3336
*/
3437
public class PropertySourcesBinder {
3538

36-
private final PropertySources propertySources;
39+
private PropertySources propertySources;
3740

41+
private ConversionService conversionService;
42+
43+
/**
44+
* Create a new instance.
45+
* @param propertySources the {@link PropertySources} to use
46+
*/
3847
public PropertySourcesBinder(PropertySources propertySources) {
3948
this.propertySources = propertySources;
4049
}
4150

51+
/**
52+
* Create a new instance from a single {@link PropertySource}.
53+
* @param propertySource the {@link PropertySource} to use
54+
*/
55+
public PropertySourcesBinder(PropertySource<?> propertySource) {
56+
this(createPropertySources(propertySource));
57+
}
58+
59+
/**
60+
* Create a new instance using the {@link Environment} as the property sources.
61+
* @param environment the environment
62+
*/
4263
public PropertySourcesBinder(ConfigurableEnvironment environment) {
4364
this(environment.getPropertySources());
4465
}
4566

46-
public final PropertySources getPropertySources() {
67+
public void setPropertySources(PropertySources propertySources) {
68+
this.propertySources = propertySources;
69+
}
70+
71+
public PropertySources getPropertySources() {
4772
return this.propertySources;
4873
}
4974

75+
public void setConversionService(ConversionService conversionService) {
76+
this.conversionService = conversionService;
77+
}
78+
79+
public ConversionService getConversionService() {
80+
return this.conversionService;
81+
}
82+
5083
/**
5184
* Extract the keys using the specified {@code prefix}. The
5285
* prefix won't be included.
@@ -74,6 +107,9 @@ public void bindTo(String prefix, Object target) {
74107
if (StringUtils.hasText(prefix)) {
75108
factory.setTargetName(prefix);
76109
}
110+
if (this.conversionService != null) {
111+
factory.setConversionService(this.conversionService);
112+
}
77113
factory.setPropertySources(this.propertySources);
78114
try {
79115
factory.bindPropertiesToTarget();
@@ -83,4 +119,10 @@ public void bindTo(String prefix, Object target) {
83119
}
84120
}
85121

122+
private static PropertySources createPropertySources(PropertySource<?> propertySource) {
123+
MutablePropertySources propertySources = new MutablePropertySources();
124+
propertySources.addLast(propertySource);
125+
return propertySources;
126+
}
127+
86128
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2012-2016 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.bind;
18+
19+
import java.util.Map;
20+
21+
import org.junit.Test;
22+
23+
import org.springframework.boot.testutil.EnvironmentTestUtils;
24+
import org.springframework.core.env.StandardEnvironment;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link PropertySourcesBinder}.
30+
*
31+
* @author Stephane Nicoll
32+
*/
33+
public class PropertySourcesBinderTests {
34+
35+
private StandardEnvironment env = new StandardEnvironment();
36+
37+
@Test
38+
public void extractAllWithPrefix() {
39+
EnvironmentTestUtils.addEnvironment(this.env, "foo.first=1", "foo.second=2");
40+
Map<String, Object> content = new PropertySourcesBinder(this.env).extractAll("foo");
41+
assertThat(content.get("first")).isEqualTo("1");
42+
assertThat(content.get("second")).isEqualTo("2");
43+
assertThat(content).hasSize(2);
44+
}
45+
46+
@Test
47+
@SuppressWarnings("unchecked")
48+
public void extractNoPrefix() {
49+
EnvironmentTestUtils.addEnvironment(this.env, "foo.ctx.first=1", "foo.ctx.second=2");
50+
Map<String, Object> content = new PropertySourcesBinder(this.env).extractAll("");
51+
assertThat(content.get("foo")).isInstanceOf(Map.class);
52+
Map<String, Object> foo = (Map<String, Object>) content.get("foo");
53+
assertThat(content.get("foo")).isInstanceOf(Map.class);
54+
Map<String, Object> ctx = (Map<String, Object>) foo.get("ctx");
55+
assertThat(ctx.get("first")).isEqualTo("1");
56+
assertThat(ctx.get("second")).isEqualTo("2");
57+
assertThat(ctx).hasSize(2);
58+
assertThat(foo).hasSize(1);
59+
}
60+
61+
@Test
62+
public void bindToSimplePojo() {
63+
EnvironmentTestUtils.addEnvironment(this.env, "test.name=foo", "test.counter=42");
64+
TestBean bean = new TestBean();
65+
new PropertySourcesBinder(this.env).bindTo("test", bean);
66+
assertThat(bean.getName()).isEqualTo("foo");
67+
assertThat(bean.getCounter()).isEqualTo(42);
68+
}
69+
70+
71+
private static class TestBean {
72+
private String name;
73+
74+
private Integer counter;
75+
76+
public String getName() {
77+
return this.name;
78+
}
79+
80+
public void setName(String name) {
81+
this.name = name;
82+
}
83+
84+
public Integer getCounter() {
85+
return this.counter;
86+
}
87+
88+
public void setCounter(Integer counter) {
89+
this.counter = counter;
90+
}
91+
92+
}
93+
94+
}

0 commit comments

Comments
 (0)