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
5c28f8f6
Commit
5c28f8f6
authored
May 18, 2021
by
Phillip Webb
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.4.x'
Closes gh-26585
parents
0c8d8191
0e3ef407
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
131 additions
and
15 deletions
+131
-15
ConfigDataEnvironment.java
...gframework/boot/context/config/ConfigDataEnvironment.java
+8
-4
ConfigDataImporter.java
...ringframework/boot/context/config/ConfigDataImporter.java
+19
-7
ConfigDataResource.java
...ringframework/boot/context/config/ConfigDataResource.java
+23
-1
ConfigDataEnvironmentPostProcessorIntegrationTests.java
...g/ConfigDataEnvironmentPostProcessorIntegrationTests.java
+47
-0
ConfigDataLocationResolversTests.java
...boot/context/config/ConfigDataLocationResolversTests.java
+32
-3
spring.factories
.../spring-boot/src/test/resources/META-INF/spring.factories
+2
-0
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironment.java
View file @
5c28f8f6
...
...
@@ -233,7 +233,8 @@ class ConfigDataEnvironment {
contributors
=
processWithoutProfiles
(
contributors
,
importer
,
activationContext
);
activationContext
=
withProfiles
(
contributors
,
activationContext
);
contributors
=
processWithProfiles
(
contributors
,
importer
,
activationContext
);
applyToEnvironment
(
contributors
,
activationContext
,
importer
.
getLoadedLocations
());
applyToEnvironment
(
contributors
,
activationContext
,
importer
.
getLoadedLocations
(),
importer
.
getOptionalLocations
());
}
private
ConfigDataEnvironmentContributors
processInitial
(
ConfigDataEnvironmentContributors
contributors
,
...
...
@@ -322,9 +323,10 @@ class ConfigDataEnvironment {
}
private
void
applyToEnvironment
(
ConfigDataEnvironmentContributors
contributors
,
ConfigDataActivationContext
activationContext
,
Set
<
ConfigDataLocation
>
loadedLocations
)
{
ConfigDataActivationContext
activationContext
,
Set
<
ConfigDataLocation
>
loadedLocations
,
Set
<
ConfigDataLocation
>
optionalLocations
)
{
checkForInvalidProperties
(
contributors
);
checkMandatoryLocations
(
contributors
,
activationContext
,
loadedLocations
);
checkMandatoryLocations
(
contributors
,
activationContext
,
loadedLocations
,
optionalLocations
);
MutablePropertySources
propertySources
=
this
.
environment
.
getPropertySources
();
this
.
logger
.
trace
(
"Applying config data environment contributions"
);
for
(
ConfigDataEnvironmentContributor
contributor
:
contributors
)
{
...
...
@@ -359,7 +361,8 @@ class ConfigDataEnvironment {
}
private
void
checkMandatoryLocations
(
ConfigDataEnvironmentContributors
contributors
,
ConfigDataActivationContext
activationContext
,
Set
<
ConfigDataLocation
>
loadedLocations
)
{
ConfigDataActivationContext
activationContext
,
Set
<
ConfigDataLocation
>
loadedLocations
,
Set
<
ConfigDataLocation
>
optionalLocations
)
{
Set
<
ConfigDataLocation
>
mandatoryLocations
=
new
LinkedHashSet
<>();
for
(
ConfigDataEnvironmentContributor
contributor
:
contributors
)
{
if
(
contributor
.
isActive
(
activationContext
))
{
...
...
@@ -372,6 +375,7 @@ class ConfigDataEnvironment {
}
}
mandatoryLocations
.
removeAll
(
loadedLocations
);
mandatoryLocations
.
removeAll
(
optionalLocations
);
if
(!
mandatoryLocations
.
isEmpty
())
{
for
(
ConfigDataLocation
mandatoryLocation
:
mandatoryLocations
)
{
this
.
notFoundAction
.
handle
(
this
.
logger
,
new
ConfigDataLocationNotFoundException
(
mandatoryLocation
));
...
...
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataImporter.java
View file @
5c28f8f6
/*
* Copyright 2012-202
0
the original author or authors.
* Copyright 2012-202
1
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -51,6 +51,8 @@ class ConfigDataImporter {
private
final
Set
<
ConfigDataLocation
>
loadedLocations
=
new
HashSet
<>();
private
final
Set
<
ConfigDataLocation
>
optionalLocations
=
new
HashSet
<>();
/**
* Create a new {@link ConfigDataImporter} instance.
* @param logFactory the log factory
...
...
@@ -103,7 +105,7 @@ class ConfigDataImporter {
return
this
.
resolvers
.
resolve
(
locationResolverContext
,
location
,
profiles
);
}
catch
(
ConfigDataNotFoundException
ex
)
{
handle
(
ex
,
location
);
handle
(
ex
,
location
,
null
);
return
Collections
.
emptyList
();
}
}
...
...
@@ -115,6 +117,9 @@ class ConfigDataImporter {
ConfigDataResolutionResult
candidate
=
candidates
.
get
(
i
);
ConfigDataLocation
location
=
candidate
.
getLocation
();
ConfigDataResource
resource
=
candidate
.
getResource
();
if
(
resource
.
isOptional
())
{
this
.
optionalLocations
.
add
(
location
);
}
if
(
this
.
loaded
.
contains
(
resource
))
{
this
.
loadedLocations
.
add
(
location
);
}
...
...
@@ -128,26 +133,33 @@ class ConfigDataImporter {
}
}
catch
(
ConfigDataNotFoundException
ex
)
{
handle
(
ex
,
location
);
handle
(
ex
,
location
,
resource
);
}
}
}
return
Collections
.
unmodifiableMap
(
result
);
}
private
void
handle
(
ConfigDataNotFoundException
ex
,
ConfigDataLocation
location
)
{
private
void
handle
(
ConfigDataNotFoundException
ex
,
ConfigDataLocation
location
,
ConfigDataResource
resource
)
{
if
(
ex
instanceof
ConfigDataResourceNotFoundException
)
{
ex
=
((
ConfigDataResourceNotFoundException
)
ex
).
withLocation
(
location
);
}
getNotFoundAction
(
location
).
handle
(
this
.
logger
,
ex
);
getNotFoundAction
(
location
,
resource
).
handle
(
this
.
logger
,
ex
);
}
private
ConfigDataNotFoundAction
getNotFoundAction
(
ConfigDataLocation
location
)
{
return
(!
location
.
isOptional
())
?
this
.
notFoundAction
:
ConfigDataNotFoundAction
.
IGNORE
;
private
ConfigDataNotFoundAction
getNotFoundAction
(
ConfigDataLocation
location
,
ConfigDataResource
resource
)
{
if
(
location
.
isOptional
()
||
(
resource
!=
null
&&
resource
.
isOptional
()))
{
return
ConfigDataNotFoundAction
.
IGNORE
;
}
return
this
.
notFoundAction
;
}
Set
<
ConfigDataLocation
>
getLoadedLocations
()
{
return
this
.
loadedLocations
;
}
Set
<
ConfigDataLocation
>
getOptionalLocations
()
{
return
this
.
optionalLocations
;
}
}
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataResource.java
View file @
5c28f8f6
/*
* Copyright 2012-202
0
the original author or authors.
* Copyright 2012-202
1
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -27,4 +27,26 @@ package org.springframework.boot.context.config;
*/
public
abstract
class
ConfigDataResource
{
private
final
boolean
optional
;
/**
* Create a new non-optional {@link ConfigDataResource} instance.
*/
public
ConfigDataResource
()
{
this
(
false
);
}
/**
* Create a new {@link ConfigDataResource} instance.
* @param optional if the resource is optional
* @since 2.4.6
*/
protected
ConfigDataResource
(
boolean
optional
)
{
this
.
optional
=
optional
;
}
boolean
isOptional
()
{
return
this
.
optional
;
}
}
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentPostProcessorIntegrationTests.java
View file @
5c28f8f6
...
...
@@ -46,6 +46,7 @@ import org.springframework.boot.context.properties.bind.Binder;
import
org.springframework.boot.context.properties.source.ConfigurationProperty
;
import
org.springframework.boot.context.properties.source.ConfigurationPropertyName
;
import
org.springframework.boot.origin.Origin
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.env.ConfigurableEnvironment
;
...
...
@@ -581,6 +582,12 @@ class ConfigDataEnvironmentPostProcessorIntegrationTests {
+
StringUtils
.
cleanPath
(
location
.
getAbsolutePath
())));
}
@Test
void
runWhenResolvedIsOptionalDoesNotThrowException
()
{
ApplicationContext
context
=
this
.
application
.
run
(
"--spring.config.location=test:optionalresult"
);
assertThat
(
context
.
getEnvironment
().
containsProperty
(
"spring"
)).
isFalse
();
}
@Test
@Disabled
(
"Disabled until spring.profiles suppport is dropped"
)
void
runWhenUsingInvalidPropertyThrowsException
()
{
...
...
@@ -752,4 +759,44 @@ class ConfigDataEnvironmentPostProcessorIntegrationTests {
}
static
class
LocationResolver
implements
ConfigDataLocationResolver
<
TestConfigDataResource
>
{
@Override
public
boolean
isResolvable
(
ConfigDataLocationResolverContext
context
,
ConfigDataLocation
location
)
{
return
location
.
hasPrefix
(
"test:"
);
}
@Override
public
List
<
TestConfigDataResource
>
resolve
(
ConfigDataLocationResolverContext
context
,
ConfigDataLocation
location
)
throws
ConfigDataLocationNotFoundException
,
ConfigDataResourceNotFoundException
{
return
Collections
.
singletonList
(
new
TestConfigDataResource
(
location
));
}
}
static
class
Loader
implements
ConfigDataLoader
<
TestConfigDataResource
>
{
@Override
public
ConfigData
load
(
ConfigDataLoaderContext
context
,
TestConfigDataResource
resource
)
throws
IOException
,
ConfigDataResourceNotFoundException
{
if
(
resource
.
isOptional
())
{
return
null
;
}
MapPropertySource
propertySource
=
new
MapPropertySource
(
"loaded"
,
Collections
.
singletonMap
(
"spring"
,
"boot"
));
return
new
ConfigData
(
Collections
.
singleton
(
propertySource
));
}
}
static
class
TestConfigDataResource
extends
ConfigDataResource
{
TestConfigDataResource
(
ConfigDataLocation
location
)
{
super
(
location
.
toString
().
contains
(
"optionalresult"
));
}
}
}
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataLocationResolversTests.java
View file @
5c28f8f6
...
...
@@ -168,8 +168,27 @@ class ConfigDataLocationResolversTests {
.
satisfies
((
ex
)
->
assertThat
(
ex
.
getLocation
()).
isEqualTo
(
location
));
}
@Test
void
resolveWhenOptional
()
{
ConfigDataLocationResolvers
resolvers
=
new
ConfigDataLocationResolvers
(
this
.
logFactory
,
this
.
bootstrapContext
,
this
.
binder
,
this
.
resourceLoader
,
Arrays
.
asList
(
OptionalResourceTestResolver
.
class
.
getName
()));
ConfigDataLocation
location
=
ConfigDataLocation
.
of
(
"OptionalResourceTestResolver:test"
);
List
<
ConfigDataResolutionResult
>
resolved
=
resolvers
.
resolve
(
this
.
context
,
location
,
null
);
assertThat
(
resolved
.
get
(
0
).
getResource
().
isOptional
()).
isTrue
();
}
static
class
TestResolver
implements
ConfigDataLocationResolver
<
TestConfigDataResource
>
{
private
final
boolean
optionalResource
;
TestResolver
()
{
this
(
false
);
}
TestResolver
(
boolean
optionalResource
)
{
this
.
optionalResource
=
optionalResource
;
}
@Override
public
boolean
isResolvable
(
ConfigDataLocationResolverContext
context
,
ConfigDataLocation
location
)
{
String
name
=
getClass
().
getName
();
...
...
@@ -180,13 +199,13 @@ class ConfigDataLocationResolversTests {
@Override
public
List
<
TestConfigDataResource
>
resolve
(
ConfigDataLocationResolverContext
context
,
ConfigDataLocation
location
)
{
return
Collections
.
singletonList
(
new
TestConfigDataResource
(
this
,
location
,
false
));
return
Collections
.
singletonList
(
new
TestConfigDataResource
(
this
.
optionalResource
,
this
,
location
,
false
));
}
@Override
public
List
<
TestConfigDataResource
>
resolveProfileSpecific
(
ConfigDataLocationResolverContext
context
,
ConfigDataLocation
location
,
Profiles
profiles
)
{
return
Collections
.
singletonList
(
new
TestConfigDataResource
(
this
,
location
,
true
));
return
Collections
.
singletonList
(
new
TestConfigDataResource
(
this
.
optionalResource
,
this
,
location
,
true
));
}
}
...
...
@@ -249,6 +268,14 @@ class ConfigDataLocationResolversTests {
}
static
class
OptionalResourceTestResolver
extends
TestResolver
{
OptionalResourceTestResolver
()
{
super
(
true
);
}
}
static
class
TestConfigDataResource
extends
ConfigDataResource
{
private
final
TestResolver
resolver
;
...
...
@@ -257,7 +284,9 @@ class ConfigDataLocationResolversTests {
private
final
boolean
profileSpecific
;
TestConfigDataResource
(
TestResolver
resolver
,
ConfigDataLocation
location
,
boolean
profileSpecific
)
{
TestConfigDataResource
(
boolean
optional
,
TestResolver
resolver
,
ConfigDataLocation
location
,
boolean
profileSpecific
)
{
super
(
optional
);
this
.
resolver
=
resolver
;
this
.
location
=
location
;
this
.
profileSpecific
=
profileSpecific
;
...
...
spring-boot-project/spring-boot/src/test/resources/META-INF/spring.factories
View file @
5c28f8f6
...
...
@@ -3,9 +3,11 @@ org.springframework.boot.context.config.TestPropertySourceLoader1,\
org.springframework.boot.context.config.TestPropertySourceLoader2
org.springframework.boot.context.config.ConfigDataLocationResolver=\
org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessorIntegrationTests.LocationResolver,\
org.springframework.boot.context.config.TestConfigDataBootstrap.LocationResolver,\
org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessorImportCombinedWithProfileSpecificIntegrationTests.LocationResolver
org.springframework.boot.context.config.ConfigDataLoader=\
org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessorIntegrationTests.Loader,\
org.springframework.boot.context.config.TestConfigDataBootstrap.Loader,\
org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessorImportCombinedWithProfileSpecificIntegrationTests.Loader
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