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
634d2767
Commit
634d2767
authored
May 17, 2021
by
Phillip Webb
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.4.x'
Closes gh-26580
parents
de600977
cfa26735
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
111 additions
and
32 deletions
+111
-32
Profiles.java
...ava/org/springframework/boot/context/config/Profiles.java
+80
-29
ProfilesTests.java
...rg/springframework/boot/context/config/ProfilesTests.java
+31
-3
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/Profiles.java
View file @
634d2767
...
...
@@ -26,8 +26,9 @@ import java.util.Iterator;
import
java.util.LinkedHashSet
;
import
java.util.List
;
import
java.util.Set
;
import
java.util.function.
Supplier
;
import
java.util.function.
Function
;
import
org.springframework.boot.context.properties.bind.BindResult
;
import
org.springframework.boot.context.properties.bind.Bindable
;
import
org.springframework.boot.context.properties.bind.Binder
;
import
org.springframework.boot.context.properties.source.ConfigurationPropertyName
;
...
...
@@ -61,9 +62,7 @@ public class Profiles implements Iterable<String> {
private
static
final
Bindable
<
MultiValueMap
<
String
,
String
>>
STRING_STRINGS_MAP
=
Bindable
.
of
(
ResolvableType
.
forClassWithGenerics
(
MultiValueMap
.
class
,
String
.
class
,
String
.
class
));
private
static
final
Set
<
String
>
UNSET_ACTIVE
=
Collections
.
emptySet
();
private
static
final
Set
<
String
>
UNSET_DEFAULT
=
Collections
.
singleton
(
"default"
);
private
static
final
Bindable
<
Set
<
String
>>
STRING_SET
=
Bindable
.
setOf
(
String
.
class
);
private
final
MultiValueMap
<
String
,
String
>
groups
;
...
...
@@ -86,35 +85,42 @@ public class Profiles implements Iterable<String> {
private
List
<
String
>
getActivatedProfiles
(
Environment
environment
,
Binder
binder
,
Collection
<
String
>
additionalProfiles
)
{
return
asUniqueItemList
(
get
(
environment
,
binder
,
environment:
:
getActiveProfiles
,
AbstractEnvironment
.
ACTIVE_PROFILES_PROPERTY_NAME
,
UNSET_ACTIVE
),
additionalProfiles
);
return
asUniqueItemList
(
getProfiles
(
environment
,
binder
,
Type
.
ACTIVE
),
additionalProfiles
);
}
private
List
<
String
>
getDefaultProfiles
(
Environment
environment
,
Binder
binder
)
{
return
asUniqueItemList
(
get
(
environment
,
binder
,
environment:
:
getDefaultProfiles
,
AbstractEnvironment
.
DEFAULT_PROFILES_PROPERTY_NAME
,
UNSET_DEFAULT
));
return
asUniqueItemList
(
getProfiles
(
environment
,
binder
,
Type
.
DEFAULT
));
}
private
String
[]
get
(
Environment
environment
,
Binder
binder
,
Supplier
<
String
[]>
supplier
,
String
propertyName
,
Set
<
String
>
unset
)
{
String
propertyValue
=
environment
.
getProperty
(
propertyName
);
if
(
hasExplicit
(
supplier
,
propertyValue
,
unset
))
{
return
supplier
.
get
();
private
Collection
<
String
>
getProfiles
(
Environment
environment
,
Binder
binder
,
Type
type
)
{
String
environmentPropertyValue
=
environment
.
getProperty
(
type
.
getName
());
Set
<
String
>
environmentPropertyProfiles
=
(!
StringUtils
.
hasLength
(
environmentPropertyValue
))
?
Collections
.
emptySet
()
:
StringUtils
.
commaDelimitedListToSet
(
StringUtils
.
trimAllWhitespace
(
environmentPropertyValue
));
Set
<
String
>
environmentProfiles
=
new
LinkedHashSet
<>(
Arrays
.
asList
(
type
.
get
(
environment
)));
BindResult
<
Set
<
String
>>
boundProfiles
=
binder
.
bind
(
type
.
getName
(),
STRING_SET
);
if
(
hasProgrammaticallySetProfiles
(
type
,
environmentPropertyValue
,
environmentPropertyProfiles
,
environmentProfiles
))
{
if
(!
type
.
isMergeWithEnvironmentProfiles
()
||
!
boundProfiles
.
isBound
())
{
return
environmentProfiles
;
}
return
boundProfiles
.
map
((
bound
)
->
merge
(
environmentProfiles
,
bound
)).
get
();
}
return
b
inder
.
bind
(
propertyName
,
String
[].
class
).
orElseGet
(()
->
StringUtils
.
toStringArray
(
unset
));
return
b
oundProfiles
.
orElse
(
type
.
getDefaultValue
(
));
}
private
boolean
has
Explicit
(
Supplier
<
String
[]>
supplier
,
String
propertyValue
,
Set
<
String
>
unset
)
{
Set
<
String
>
profiles
=
new
LinkedHashSet
<>(
Arrays
.
asList
(
supplier
.
get
()));
if
(!
StringUtils
.
hasLength
(
p
ropertyValue
))
{
return
!
unset
.
equals
(
p
rofiles
);
private
boolean
has
ProgrammaticallySetProfiles
(
Type
type
,
String
environmentPropertyValue
,
Set
<
String
>
environmentPropertyProfiles
,
Set
<
String
>
environmentProfiles
)
{
if
(!
StringUtils
.
hasLength
(
environmentP
ropertyValue
))
{
return
!
type
.
getDefaultValue
().
equals
(
environmentP
rofiles
);
}
if
(
unset
.
equals
(
profiles
))
{
return
false
;
}
Set
<
String
>
propertyProfiles
=
StringUtils
.
commaDelimitedListToSet
(
StringUtils
.
trimAllWhitespace
(
propertyValue
));
return
!
propertyProfiles
.
equals
(
profiles
);
return
!
environmentPropertyProfiles
.
equals
(
environmentProfiles
);
}
private
Set
<
String
>
merge
(
Set
<
String
>
environmentProfiles
,
Set
<
String
>
bound
)
{
Set
<
String
>
result
=
new
LinkedHashSet
<>(
environmentProfiles
);
result
.
addAll
(
bound
);
return
result
;
}
private
List
<
String
>
expandProfiles
(
List
<
String
>
profiles
)
{
...
...
@@ -127,7 +133,7 @@ public class Profiles implements Iterable<String> {
asReversedList
(
this
.
groups
.
get
(
current
)).
forEach
(
stack:
:
push
);
}
}
return
asUniqueItemList
(
StringUtils
.
toStringArray
(
expandedProfiles
)
);
return
asUniqueItemList
(
expandedProfiles
);
}
private
List
<
String
>
asReversedList
(
List
<
String
>
list
)
{
...
...
@@ -139,12 +145,12 @@ public class Profiles implements Iterable<String> {
return
reversed
;
}
private
List
<
String
>
asUniqueItemList
(
String
[]
array
)
{
return
asUniqueItemList
(
array
,
null
);
private
List
<
String
>
asUniqueItemList
(
Collection
<
String
>
strings
)
{
return
asUniqueItemList
(
strings
,
null
);
}
private
List
<
String
>
asUniqueItemList
(
String
[]
array
,
Collection
<
String
>
additional
)
{
LinkedHashSet
<
String
>
uniqueItems
=
new
LinkedHashSet
<>(
Arrays
.
asList
(
array
)
);
private
List
<
String
>
asUniqueItemList
(
Collection
<
String
>
strings
,
Collection
<
String
>
additional
)
{
LinkedHashSet
<
String
>
uniqueItems
=
new
LinkedHashSet
<>(
strings
);
if
(!
CollectionUtils
.
isEmpty
(
additional
))
{
uniqueItems
.
addAll
(
additional
);
}
...
...
@@ -201,4 +207,49 @@ public class Profiles implements Iterable<String> {
return
creator
.
toString
();
}
/**
* A profiles type that can be obtained.
*/
private
enum
Type
{
ACTIVE
(
AbstractEnvironment
.
ACTIVE_PROFILES_PROPERTY_NAME
,
Environment:
:
getActiveProfiles
,
true
,
Collections
.
emptySet
()),
DEFAULT
(
AbstractEnvironment
.
DEFAULT_PROFILES_PROPERTY_NAME
,
Environment:
:
getDefaultProfiles
,
false
,
Collections
.
singleton
(
"default"
));
private
final
Function
<
Environment
,
String
[]>
getter
;
private
final
boolean
mergeWithEnvironmentProfiles
;
private
final
String
name
;
private
final
Set
<
String
>
defaultValue
;
Type
(
String
name
,
Function
<
Environment
,
String
[]>
getter
,
boolean
mergeWithEnvironmentProfiles
,
Set
<
String
>
defaultValue
)
{
this
.
name
=
name
;
this
.
getter
=
getter
;
this
.
mergeWithEnvironmentProfiles
=
mergeWithEnvironmentProfiles
;
this
.
defaultValue
=
defaultValue
;
}
String
getName
()
{
return
this
.
name
;
}
String
[]
get
(
Environment
environment
)
{
return
this
.
getter
.
apply
(
environment
);
}
Set
<
String
>
getDefaultValue
()
{
return
this
.
defaultValue
;
}
boolean
isMergeWithEnvironmentProfiles
()
{
return
this
.
mergeWithEnvironmentProfiles
;
}
}
}
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ProfilesTests.java
View file @
634d2767
...
...
@@ -16,12 +16,16 @@
package
org
.
springframework
.
boot
.
context
.
config
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.List
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.boot.context.properties.bind.Binder
;
import
org.springframework.boot.context.properties.source.ConfigurationPropertySource
;
import
org.springframework.boot.context.properties.source.ConfigurationPropertySources
;
import
org.springframework.boot.context.properties.source.MapConfigurationPropertySource
;
import
org.springframework.core.env.Environment
;
import
org.springframework.mock.env.MockEnvironment
;
...
...
@@ -69,6 +73,18 @@ class ProfilesTests {
Binder
binder
=
new
Binder
(
new
MapConfigurationPropertySource
(
Collections
.
singletonMap
(
"spring.profiles.active"
,
"d,e,f"
)));
Profiles
profiles
=
new
Profiles
(
environment
,
binder
,
null
);
assertThat
(
profiles
.
getActive
()).
containsExactly
(
"a"
,
"b"
,
"c"
,
"d"
,
"e"
,
"f"
);
}
@Test
void
getActiveWhenEnvironmentProfilesAndBinderPropertyShouldReturnEnvironmentProperty
()
{
MockEnvironment
environment
=
new
MockEnvironment
();
environment
.
setProperty
(
"spring.profiles.active"
,
"a,b,c"
);
List
<
ConfigurationPropertySource
>
sources
=
new
ArrayList
<>();
ConfigurationPropertySources
.
get
(
environment
).
forEach
(
sources:
:
add
);
sources
.
add
(
new
MapConfigurationPropertySource
(
Collections
.
singletonMap
(
"spring.profiles.active"
,
"d,e,f"
)));
Binder
binder
=
new
Binder
(
sources
);
Profiles
profiles
=
new
Profiles
(
environment
,
binder
,
null
);
assertThat
(
profiles
.
getActive
()).
containsExactly
(
"a"
,
"b"
,
"c"
);
}
...
...
@@ -79,7 +95,7 @@ class ProfilesTests {
environment
.
setProperty
(
"spring.profiles.active"
,
"d,e,f"
);
Binder
binder
=
Binder
.
get
(
environment
);
Profiles
profiles
=
new
Profiles
(
environment
,
binder
,
null
);
assertThat
(
profiles
.
getActive
()).
containsExactly
(
"a"
,
"b"
,
"c"
);
assertThat
(
profiles
.
getActive
()).
containsExactly
(
"a"
,
"b"
,
"c"
,
"d"
,
"e"
,
"f"
);
}
@Test
...
...
@@ -102,7 +118,7 @@ class ProfilesTests {
environment
.
setProperty
(
"spring.profiles.active[2]"
,
"f"
);
Binder
binder
=
Binder
.
get
(
environment
);
Profiles
profiles
=
new
Profiles
(
environment
,
binder
,
null
);
assertThat
(
profiles
.
getActive
()).
containsExactly
(
"a"
,
"b"
,
"c"
);
assertThat
(
profiles
.
getActive
()).
containsExactly
(
"a"
,
"b"
,
"c"
,
"d"
,
"e"
,
"f"
);
}
@Test
...
...
@@ -150,6 +166,18 @@ class ProfilesTests {
assertThat
(
profiles
.
getDefault
()).
containsExactly
(
"a"
,
"b"
,
"c"
);
}
@Test
void
getDefaultWhenDefaultEnvironmentProfileAndBinderProperty
()
{
MockEnvironment
environment
=
new
MockEnvironment
();
environment
.
setProperty
(
"spring.profiles.default"
,
"default"
);
List
<
ConfigurationPropertySource
>
sources
=
new
ArrayList
<>();
ConfigurationPropertySources
.
get
(
environment
).
forEach
(
sources:
:
add
);
sources
.
add
(
new
MapConfigurationPropertySource
(
Collections
.
singletonMap
(
"spring.profiles.default"
,
"a,b,c"
)));
Binder
binder
=
new
Binder
(
sources
);
Profiles
profiles
=
new
Profiles
(
environment
,
binder
,
null
);
assertThat
(
profiles
.
getDefault
()).
containsExactly
(
"default"
);
}
@Test
void
getDefaultWhenNoEnvironmentProfilesAndEnvironmentProperty
()
{
MockEnvironment
environment
=
new
MockEnvironment
();
...
...
@@ -210,7 +238,7 @@ class ProfilesTests {
}
@Test
void
getDefaultWhenEnvironmentProfilesInBindNotationAndEnvironmentPropertyReturns
EnvironmentProfiles
()
{
void
getDefaultWhenEnvironmentProfilesInBindNotationAndEnvironmentPropertyReturns
Both
()
{
MockEnvironment
environment
=
new
MockEnvironment
();
environment
.
setDefaultProfiles
(
"a"
,
"b"
,
"c"
);
environment
.
setProperty
(
"spring.profiles.default[0]"
,
"d"
);
...
...
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