Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
e5f80787
Commit
e5f80787
authored
Apr 03, 2018
by
Madhura Bhave
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support dots in System environment properties
Fixes gh-12728
parent
d14cd2cb
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
18 deletions
+43
-18
SpringConfigurationPropertySource.java
.../properties/source/SpringConfigurationPropertySource.java
+31
-18
ConfigurationPropertiesTests.java
...boot/context/properties/ConfigurationPropertiesTests.java
+12
-0
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySource.java
View file @
e5f80787
...
...
@@ -16,6 +16,9 @@
package
org
.
springframework
.
boot
.
context
.
properties
.
source
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Random
;
import
java.util.function.Function
;
...
...
@@ -74,7 +77,7 @@ class SpringConfigurationPropertySource implements ConfigurationPropertySource {
Assert
.
notNull
(
propertySource
,
"PropertySource must not be null"
);
Assert
.
notNull
(
mapper
,
"Mapper must not be null"
);
this
.
propertySource
=
propertySource
;
this
.
mapper
=
new
ExceptionSwallowingPropertyMapper
(
mapper
)
;
this
.
mapper
=
mapper
;
this
.
containsDescendantOf
=
(
containsDescendantOf
!=
null
?
containsDescendantOf
:
(
n
)
->
ConfigurationPropertyState
.
UNKNOWN
);
}
...
...
@@ -156,9 +159,10 @@ class SpringConfigurationPropertySource implements ConfigurationPropertySource {
private
static
PropertyMapper
getPropertyMapper
(
PropertySource
<?>
source
)
{
if
(
source
instanceof
SystemEnvironmentPropertySource
&&
hasSystemEnvironmentName
(
source
))
{
return
SystemEnvironmentPropertyMapper
.
INSTANCE
;
return
new
DelegatingPropertyMapper
(
SystemEnvironmentPropertyMapper
.
INSTANCE
,
DefaultPropertyMapper
.
INSTANCE
);
}
return
DefaultPropertyMapper
.
INSTANCE
;
return
new
DelegatingPropertyMapper
(
DefaultPropertyMapper
.
INSTANCE
)
;
}
private
static
boolean
hasSystemEnvironmentName
(
PropertySource
<?>
source
)
{
...
...
@@ -207,35 +211,44 @@ class SpringConfigurationPropertySource implements ConfigurationPropertySource {
}
/**
* {@link PropertyMapper} that swallows exceptions when the mapping fails.
* {@link PropertyMapper} that delegates to other {@link PropertyMapper}s.
* It also swallows exceptions when the mapping fails.
*/
private
static
class
ExceptionSwallow
ingPropertyMapper
implements
PropertyMapper
{
private
static
class
Delegat
ingPropertyMapper
implements
PropertyMapper
{
private
final
PropertyMapper
mapper
;
private
final
PropertyMapper
[]
mappers
;
ExceptionSwallowingPropertyMapper
(
PropertyMapper
mapper
)
{
this
.
mapper
=
mapper
;
DelegatingPropertyMapper
(
PropertyMapper
...
mappers
)
{
this
.
mapper
s
=
mappers
;
}
@Override
public
PropertyMapping
[]
map
(
ConfigurationPropertyName
configurationPropertyName
)
{
try
{
return
this
.
mapper
.
map
(
configurationPropertyName
);
}
catch
(
Exception
ex
)
{
return
NO_MAPPINGS
;
List
<
PropertyMapping
>
mappings
=
new
ArrayList
<>();
for
(
PropertyMapper
mapper
:
this
.
mappers
)
{
try
{
mappings
.
addAll
(
Arrays
.
asList
(
mapper
.
map
(
configurationPropertyName
)));
}
catch
(
Exception
ex
)
{
}
}
return
mappings
.
toArray
(
new
PropertyMapping
[]
{});
}
@Override
public
PropertyMapping
[]
map
(
String
propertySourceName
)
{
try
{
return
this
.
mapper
.
map
(
propertySourceName
);
}
catch
(
Exception
ex
)
{
return
NO_MAPPINGS
;
List
<
PropertyMapping
>
mappings
=
new
ArrayList
<>();
for
(
PropertyMapper
mapper
:
this
.
mappers
)
{
try
{
mappings
.
addAll
(
Arrays
.
asList
(
mapper
.
map
(
propertySourceName
)));
}
catch
(
Exception
ex
)
{
}
}
return
mappings
.
toArray
(
new
PropertyMapping
[]
{});
}
}
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java
View file @
e5f80787
...
...
@@ -461,6 +461,18 @@ public class ConfigurationPropertiesTests {
assertThat
(
bean
.
getMap
().
get
(
"foo"
)).
containsOnly
(
entry
(
"bar"
,
"baz"
));
}
@Test
public
void
loadWhenDotsInSystemEnvironmentPropertiesShouldBind
()
{
this
.
context
.
getEnvironment
().
getPropertySources
()
.
addLast
(
new
SystemEnvironmentPropertySource
(
StandardEnvironment
.
SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME
,
Collections
.
singletonMap
(
"com.example.bar"
,
"baz"
)));
load
(
SimplePrefixedProperties
.
class
);
SimplePrefixedProperties
bean
=
this
.
context
.
getBean
(
SimplePrefixedProperties
.
class
);
assertThat
(
bean
.
getBar
()).
isEqualTo
(
"baz"
);
}
@Test
public
void
loadWhenOverridingPropertiesShouldBind
()
{
MutablePropertySources
sources
=
this
.
context
.
getEnvironment
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment