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
f038da1f
Commit
f038da1f
authored
Jan 10, 2014
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Throw an exception if a config file that exists cannot be parsed
Fixes gh-209 in a general way
parent
cc996ce7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
84 additions
and
10 deletions
+84
-10
ConfigFileApplicationListener.java
.../boot/context/listener/ConfigFileApplicationListener.java
+46
-10
ConfigFileApplicationListenerTests.java
.../context/listener/ConfigFileApplicationListenerTests.java
+38
-0
No files found.
spring-boot/src/main/java/org/springframework/boot/context/listener/ConfigFileApplicationListener.java
View file @
f038da1f
...
...
@@ -104,6 +104,8 @@ public class ConfigFileApplicationListener implements
private
PropertySourceAnnotations
propertySourceAnnotations
=
new
PropertySourceAnnotations
();
private
PropertySourceLoaderFactory
propertySourceLoaderFactory
=
new
DefaultPropertySourceLoaderFactory
();
/**
* Binds the early {@link Environment} to the {@link SpringApplication}. This makes it
* possible to set {@link SpringApplication} properties dynamically, like the sources
...
...
@@ -284,12 +286,8 @@ public class ConfigFileApplicationListener implements
return
null
;
}
List
<
PropertySourceLoader
>
loaders
=
new
ArrayList
<
PropertySourceLoader
>();
loaders
.
add
(
new
PropertiesPropertySourceLoader
());
if
(
ClassUtils
.
isPresent
(
"org.yaml.snakeyaml.Yaml"
,
null
))
{
loaders
.
add
(
YamlPropertySourceLoader
.
springProfileAwareLoader
(
environment
.
getActiveProfiles
()));
}
List
<
PropertySourceLoader
>
loaders
=
this
.
propertySourceLoaderFactory
.
getLoaders
(
environment
);
Resource
resource
=
resourceLoader
.
getResource
(
location
);
String
name
=
this
.
propertySourceAnnotations
.
name
(
location
);
...
...
@@ -334,13 +332,23 @@ public class ConfigFileApplicationListener implements
if
(
this
.
cached
.
containsKey
(
key
))
{
return
this
.
cached
.
get
(
key
);
}
boolean
satisfied
=
true
;
for
(
PropertySourceLoader
loader
:
loaders
)
{
if
(
resource
!=
null
&&
resource
.
exists
()
&&
loader
.
supports
(
resource
))
{
PropertySource
<?>
propertySource
=
loader
.
load
(
name
,
resource
);
this
.
cached
.
put
(
key
,
propertySource
);
return
propertySource
;
if
(
resource
!=
null
&&
resource
.
exists
())
{
if
(
loader
.
supports
(
resource
))
{
PropertySource
<?>
propertySource
=
loader
.
load
(
name
,
resource
);
this
.
cached
.
put
(
key
,
propertySource
);
return
propertySource
;
}
else
{
satisfied
=
false
;
}
}
}
if
(!
satisfied
)
{
throw
new
IllegalStateException
(
"No supported loader found for configuration resource: "
+
resource
);
}
return
null
;
}
...
...
@@ -368,6 +376,14 @@ public class ConfigFileApplicationListener implements
this
.
searchLocations
=
(
searchLocations
==
null
?
null
:
searchLocations
.
clone
());
}
/**
* @param propertySourceLoaderFactory the factory to set
*/
public
void
setPropertySourceLoaderFactory
(
PropertySourceLoaderFactory
propertySourceLoaderFactory
)
{
this
.
propertySourceLoaderFactory
=
propertySourceLoaderFactory
;
}
private
static
class
RandomValuePropertySource
extends
PropertySource
<
Random
>
{
public
RandomValuePropertySource
(
String
name
)
{
...
...
@@ -444,4 +460,24 @@ public class ConfigFileApplicationListener implements
}
}
public
static
interface
PropertySourceLoaderFactory
{
List
<
PropertySourceLoader
>
getLoaders
(
Environment
environment
);
}
private
static
class
DefaultPropertySourceLoaderFactory
implements
PropertySourceLoaderFactory
{
@Override
public
List
<
PropertySourceLoader
>
getLoaders
(
Environment
environment
)
{
ArrayList
<
PropertySourceLoader
>
loaders
=
new
ArrayList
<
PropertySourceLoader
>();
loaders
.
add
(
new
PropertiesPropertySourceLoader
());
if
(
ClassUtils
.
isPresent
(
"org.yaml.snakeyaml.Yaml"
,
null
))
{
loaders
.
add
(
YamlPropertySourceLoader
.
springProfileAwareLoader
(
environment
.
getActiveProfiles
()));
}
return
loaders
;
}
}
}
spring-boot/src/test/java/org/springframework/boot/context/listener/ConfigFileApplicationListenerTests.java
View file @
f038da1f
...
...
@@ -18,20 +18,27 @@ package org.springframework.boot.context.listener;
import
java.util.Arrays
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
org.junit.After
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.rules.ExpectedException
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.SpringApplicationEnvironmentAvailableEvent
;
import
org.springframework.boot.config.PropertySourceLoader
;
import
org.springframework.boot.context.listener.ConfigFileApplicationListener.PropertySourceLoaderFactory
;
import
org.springframework.boot.test.EnvironmentTestUtils
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Profile
;
import
org.springframework.context.annotation.PropertySource
;
import
org.springframework.core.env.Environment
;
import
org.springframework.core.env.MapPropertySource
;
import
org.springframework.core.env.SimpleCommandLinePropertySource
;
import
org.springframework.core.env.StandardEnvironment
;
import
org.springframework.core.io.Resource
;
import
static
org
.
hamcrest
.
Matchers
.
contains
;
import
static
org
.
hamcrest
.
Matchers
.
equalTo
;
...
...
@@ -57,6 +64,9 @@ public class ConfigFileApplicationListenerTests {
private
ConfigFileApplicationListener
initializer
=
new
ConfigFileApplicationListener
();
@Rule
public
ExpectedException
expected
=
ExpectedException
.
none
();
@After
public
void
cleanup
()
{
System
.
clearProperty
(
"my.property"
);
...
...
@@ -198,6 +208,34 @@ public class ConfigFileApplicationListenerTests {
assertThat
(
this
.
environment
.
getPropertySources
().
contains
(
location
),
is
(
true
));
}
@Test
public
void
unsupportedResource
()
throws
Exception
{
this
.
initializer
.
setPropertySourceLoaderFactory
(
new
PropertySourceLoaderFactory
()
{
@Override
public
List
<
PropertySourceLoader
>
getLoaders
(
Environment
environment
)
{
return
Arrays
.<
PropertySourceLoader
>
asList
(
new
PropertySourceLoader
()
{
@Override
public
boolean
supports
(
Resource
resource
)
{
return
false
;
}
@Override
public
org
.
springframework
.
core
.
env
.
PropertySource
<?>
load
(
String
name
,
Resource
resource
)
{
return
null
;
}
});
}
});
this
.
expected
.
expect
(
IllegalStateException
.
class
);
this
.
expected
.
expectMessage
(
"No supported loader"
);
this
.
initializer
.
onApplicationEvent
(
this
.
event
);
}
@Test
public
void
specificResourceDefaultsToFile
()
throws
Exception
{
String
location
=
"src/test/resources/specificlocation.properties"
;
...
...
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