Skip to content

Commit 15f27d2

Browse files
committed
Use configuration resolver to replace standard resolver
Closes spring-projectsgh-28687
1 parent 12537c7 commit 15f27d2

File tree

5 files changed

+121
-7
lines changed

5 files changed

+121
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2012-2023 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+
* https://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.context;
18+
19+
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
20+
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
21+
import org.springframework.core.env.ConfigurablePropertyResolver;
22+
import org.springframework.core.env.MutablePropertySources;
23+
24+
/**
25+
* Placeholder configurer that resolves using the optimized
26+
* {@link ConfigurablePropertyResolver}.
27+
*
28+
* @author Guirong Hu
29+
*/
30+
class ConfigurationPropertySourcesPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer {
31+
32+
// @Override
33+
protected ConfigurablePropertyResolver createPropertyResolver(MutablePropertySources propertySources) {
34+
return ConfigurationPropertySources.createPropertyResolver(propertySources);
35+
}
36+
37+
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/PropertyPlaceholderAutoConfiguration.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,7 +40,7 @@ public class PropertyPlaceholderAutoConfiguration {
4040
@Bean
4141
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
4242
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
43-
return new PropertySourcesPlaceholderConfigurer();
43+
return new ConfigurationPropertySourcesPlaceholderConfigurer();
4444
}
4545

4646
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2012-2023 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+
* https://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.context;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.springframework.beans.BeansException;
21+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
22+
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
23+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.core.env.ConfigurablePropertyResolver;
27+
import org.springframework.core.env.MutablePropertySources;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
/**
32+
* Tests for {@link ConfigurationPropertySourcesPlaceholderConfigurer}.
33+
*
34+
* @author Guirong Hu
35+
*/
36+
class ConfigurationPropertySourcesPlaceholderConfigurerTests {
37+
38+
@Test
39+
void propertyResolverIsOptimizedForPropertyPlaceholder() {
40+
ConfigurablePropertyResolver expected = ConfigurationPropertySources
41+
.createPropertyResolver(new MutablePropertySources());
42+
43+
new ApplicationContextRunner().withUserConfiguration(GetPropertyResolverPlaceholderConfigurerConfig.class)
44+
.run((context) -> assertThat(
45+
context.getBean(GetPropertyResolverPlaceholderConfigurer.class).getPropertyResolver())
46+
.hasSameClassAs(expected));
47+
}
48+
49+
@Configuration(proxyBeanMethods = false)
50+
static class GetPropertyResolverPlaceholderConfigurerConfig {
51+
52+
@Bean
53+
static GetPropertyResolverPlaceholderConfigurer getPropertyResolverPlaceholderConfigurer() {
54+
return new GetPropertyResolverPlaceholderConfigurer();
55+
}
56+
57+
}
58+
59+
static class GetPropertyResolverPlaceholderConfigurer extends ConfigurationPropertySourcesPlaceholderConfigurer {
60+
61+
private ConfigurablePropertyResolver propertyResolver;
62+
63+
@Override
64+
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
65+
ConfigurablePropertyResolver propertyResolver) throws BeansException {
66+
this.propertyResolver = propertyResolver;
67+
super.processProperties(beanFactoryToProcess, propertyResolver);
68+
}
69+
70+
public ConfigurablePropertyResolver getPropertyResolver() {
71+
return this.propertyResolver;
72+
}
73+
74+
}
75+
76+
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/PropertyPlaceholderAutoConfigurationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ static class PlaceholdersOverride {
113113

114114
@Bean
115115
static PropertySourcesPlaceholderConfigurer morePlaceholders() {
116-
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
116+
PropertySourcesPlaceholderConfigurer configurer = new ConfigurationPropertySourcesPlaceholderConfigurer();
117117
configurer
118118
.setProperties(StringUtils.splitArrayElementsIntoProperties(new String[] { "fruit=orange" }, "="));
119119
configurer.setLocalOverride(true);

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,11 +20,12 @@
2020
import java.nio.charset.StandardCharsets;
2121
import java.util.function.BiConsumer;
2222

23+
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
2324
import org.springframework.boot.system.ApplicationPid;
2425
import org.springframework.core.env.ConfigurableEnvironment;
26+
import org.springframework.core.env.ConfigurablePropertyResolver;
2527
import org.springframework.core.env.Environment;
2628
import org.springframework.core.env.PropertyResolver;
27-
import org.springframework.core.env.PropertySourcesPropertyResolver;
2829
import org.springframework.util.Assert;
2930

3031
/**
@@ -162,8 +163,8 @@ protected void apply(LogFile logFile, PropertyResolver resolver) {
162163

163164
private PropertyResolver getPropertyResolver() {
164165
if (this.environment instanceof ConfigurableEnvironment configurableEnvironment) {
165-
PropertySourcesPropertyResolver resolver = new PropertySourcesPropertyResolver(
166-
configurableEnvironment.getPropertySources());
166+
ConfigurablePropertyResolver resolver = ConfigurationPropertySources
167+
.createPropertyResolver(configurableEnvironment.getPropertySources());
167168
resolver.setConversionService(((ConfigurableEnvironment) this.environment).getConversionService());
168169
resolver.setIgnoreUnresolvableNestedPlaceholders(true);
169170
return resolver;

0 commit comments

Comments
 (0)