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
aeb0f0a6
Commit
aeb0f0a6
authored
Nov 25, 2013
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add random.* property source
parent
94c5203d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
0 deletions
+42
-0
ConfigFileApplicationContextInitializer.java
.../initializer/ConfigFileApplicationContextInitializer.java
+30
-0
ConfigFileApplicationContextInitializerTests.java
...ializer/ConfigFileApplicationContextInitializerTests.java
+12
-0
No files found.
spring-boot/src/main/java/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializer.java
View file @
aeb0f0a6
...
...
@@ -21,6 +21,7 @@ import java.util.Collections;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Random
;
import
java.util.Set
;
import
org.springframework.beans.PropertyValues
;
...
...
@@ -40,10 +41,12 @@ import org.springframework.core.convert.support.DefaultConversionService;
import
org.springframework.core.env.ConfigurableEnvironment
;
import
org.springframework.core.env.Environment
;
import
org.springframework.core.env.PropertySource
;
import
org.springframework.core.env.StandardEnvironment
;
import
org.springframework.core.io.DefaultResourceLoader
;
import
org.springframework.core.io.Resource
;
import
org.springframework.core.io.ResourceLoader
;
import
org.springframework.util.ClassUtils
;
import
org.springframework.util.DigestUtils
;
import
org.springframework.util.StringUtils
;
/**
...
...
@@ -106,6 +109,9 @@ public class ConfigFileApplicationContextInitializer implements
if
(
this
.
environment
instanceof
ConfigurableEnvironment
)
{
ConfigurableEnvironment
environment
=
(
ConfigurableEnvironment
)
this
.
environment
;
load
(
environment
,
new
DefaultResourceLoader
());
environment
.
getPropertySources
().
addAfter
(
StandardEnvironment
.
SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME
,
new
RandomValuePropertySource
(
"random"
));
// Set bean properties from the early environment
PropertyValues
propertyValues
=
new
PropertySourcesPropertyValues
(
environment
.
getPropertySources
());
...
...
@@ -251,4 +257,28 @@ public class ConfigFileApplicationContextInitializer implements
this
.
searchLocations
=
(
searchLocations
==
null
?
null
:
searchLocations
.
clone
());
}
private
static
class
RandomValuePropertySource
extends
PropertySource
<
Random
>
{
public
RandomValuePropertySource
(
String
name
)
{
super
(
name
,
new
Random
());
}
@Override
public
Object
getProperty
(
String
name
)
{
if
(!
name
.
startsWith
(
"random."
))
{
return
null
;
}
if
(
name
.
endsWith
(
"int"
))
{
return
getSource
().
nextInt
();
}
if
(
name
.
endsWith
(
"long"
))
{
return
getSource
().
nextLong
();
}
byte
[]
bytes
=
new
byte
[
32
];
getSource
().
nextBytes
(
bytes
);
return
DigestUtils
.
md5DigestAsHex
(
bytes
);
}
}
}
spring-boot/src/test/java/org/springframework/boot/context/initializer/ConfigFileApplicationContextInitializerTests.java
View file @
aeb0f0a6
...
...
@@ -21,11 +21,14 @@ import java.util.Map;
import
org.junit.After
;
import
org.junit.Test
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.context.support.StaticApplicationContext
;
import
org.springframework.core.env.MapPropertySource
;
import
org.springframework.core.env.SimpleCommandLinePropertySource
;
import
org.springframework.core.env.StandardEnvironment
;
import
static
org
.
hamcrest
.
Matchers
.
equalTo
;
import
static
org
.
hamcrest
.
Matchers
.
notNullValue
;
import
static
org
.
hamcrest
.
Matchers
.
nullValue
;
import
static
org
.
junit
.
Assert
.
assertThat
;
...
...
@@ -54,6 +57,15 @@ public class ConfigFileApplicationContextInitializerTests {
assertThat
(
property
,
equalTo
(
"frompropertiesfile"
));
}
@Test
public
void
randomValue
()
throws
Exception
{
StandardEnvironment
environment
=
new
StandardEnvironment
();
this
.
initializer
.
setEnvironment
(
environment
);
this
.
initializer
.
initialize
(
new
SpringApplication
(),
new
String
[
0
]);
String
property
=
environment
.
getProperty
(
"random.value"
);
assertThat
(
property
,
notNullValue
());
}
@Test
public
void
loadTwoPropertiesFiles
()
throws
Exception
{
this
.
initializer
.
setNames
(
"testproperties,moreproperties"
);
...
...
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