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
56447555
Commit
56447555
authored
Nov 26, 2013
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow for commandLineArgs to be already present in Environment
parent
3f1cfbf2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
13 deletions
+55
-13
SpringApplication.java
...main/java/org/springframework/boot/SpringApplication.java
+19
-6
SpringApplicationTests.java
...java/org/springframework/boot/SpringApplicationTests.java
+36
-7
No files found.
spring-boot/src/main/java/org/springframework/boot/SpringApplication.java
View file @
56447555
...
...
@@ -51,9 +51,11 @@ import org.springframework.context.support.GenericApplicationContext;
import
org.springframework.core.GenericTypeResolver
;
import
org.springframework.core.annotation.AnnotationAwareOrderComparator
;
import
org.springframework.core.env.CommandLinePropertySource
;
import
org.springframework.core.env.CompositePropertySource
;
import
org.springframework.core.env.ConfigurableEnvironment
;
import
org.springframework.core.env.Environment
;
import
org.springframework.core.env.MapPropertySource
;
import
org.springframework.core.env.MutablePropertySources
;
import
org.springframework.core.env.PropertySource
;
import
org.springframework.core.env.SimpleCommandLinePropertySource
;
import
org.springframework.core.env.StandardEnvironment
;
...
...
@@ -375,13 +377,24 @@ public class SpringApplication {
* @param args run arguments
*/
protected
void
addPropertySources
(
ConfigurableEnvironment
environment
,
String
[]
args
)
{
MutablePropertySources
sources
=
environment
.
getPropertySources
();
if
(
this
.
defaultProperties
!=
null
&&
!
this
.
defaultProperties
.
isEmpty
())
{
environment
.
getPropertySources
().
addLast
(
new
MapPropertySource
(
"defaultProperties"
,
this
.
defaultProperties
));
}
if
(
this
.
addCommandLineProperties
)
{
environment
.
getPropertySources
().
addFirst
(
new
SimpleCommandLinePropertySource
(
args
));
sources
.
addLast
(
new
MapPropertySource
(
"defaultProperties"
,
this
.
defaultProperties
));
}
if
(
this
.
addCommandLineProperties
&&
args
.
length
>
0
)
{
String
name
=
CommandLinePropertySource
.
COMMAND_LINE_PROPERTY_SOURCE_NAME
;
if
(
sources
.
contains
(
name
))
{
PropertySource
<?>
source
=
sources
.
get
(
name
);
CompositePropertySource
composite
=
new
CompositePropertySource
(
name
);
composite
.
addPropertySource
(
new
SimpleCommandLinePropertySource
(
name
+
"-"
+
args
.
hashCode
(),
args
));
composite
.
addPropertySource
(
source
);
sources
.
replace
(
name
,
composite
);
}
else
{
sources
.
addFirst
(
new
SimpleCommandLinePropertySource
(
args
));
}
}
}
...
...
spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java
View file @
56447555
...
...
@@ -17,6 +17,7 @@
package
org
.
springframework
.
boot
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.Set
;
import
java.util.concurrent.atomic.AtomicReference
;
...
...
@@ -43,6 +44,7 @@ import org.springframework.context.event.ContextRefreshedEvent;
import
org.springframework.context.support.StaticApplicationContext
;
import
org.springframework.core.Ordered
;
import
org.springframework.core.env.CommandLinePropertySource
;
import
org.springframework.core.env.CompositePropertySource
;
import
org.springframework.core.env.ConfigurableEnvironment
;
import
org.springframework.core.env.Environment
;
import
org.springframework.core.env.MapPropertySource
;
...
...
@@ -58,6 +60,7 @@ import static org.hamcrest.Matchers.instanceOf;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
org
.
hamcrest
.
Matchers
.
sameInstance
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
...
...
@@ -244,14 +247,41 @@ public class SpringApplicationTests {
@Test
public
void
commandLinePropertySource
()
throws
Exception
{
SpringApplication
application
=
new
SpringApplication
(
ExampleConfig
.
class
);
application
.
setWebEnvironment
(
false
);
ConfigurableEnvironment
environment
=
new
StandardEnvironment
();
application
.
setEnvironment
(
environment
);
application
.
run
(
"--foo=bar"
);
assertTrue
(
hasPropertySource
(
environment
,
CommandLinePropertySource
.
class
,
"commandLineArgs"
));
}
@Test
public
void
commandLinePropertySourceEnhancesEnvironment
()
throws
Exception
{
SpringApplication
application
=
new
SpringApplication
(
ExampleConfig
.
class
);
application
.
setWebEnvironment
(
false
);
ConfigurableEnvironment
environment
=
new
StandardEnvironment
();
environment
.
getPropertySources
().
addFirst
(
new
MapPropertySource
(
"commandLineArgs"
,
Collections
.<
String
,
Object
>
singletonMap
(
"foo"
,
"original"
)));
application
.
setEnvironment
(
environment
);
application
.
run
(
"--foo=bar"
,
"--bar=foo"
);
assertTrue
(
hasPropertySource
(
environment
,
CompositePropertySource
.
class
,
"commandLineArgs"
));
assertEquals
(
"foo"
,
environment
.
getProperty
(
"bar"
));
// New command line properties take precedence
assertEquals
(
"bar"
,
environment
.
getProperty
(
"foo"
));
}
@Test
public
void
emptyCommandLinePropertySourceNotAdded
()
throws
Exception
{
SpringApplication
application
=
new
SpringApplication
(
ExampleConfig
.
class
);
application
.
setWebEnvironment
(
false
);
ConfigurableEnvironment
environment
=
new
StandardEnvironment
();
application
.
setEnvironment
(
environment
);
application
.
run
();
assertThat
(
hasPropertySource
(
environment
,
CommandLinePropertySource
.
class
,
"commandLineArgs"
),
equalTo
(
true
));
assertFalse
(
hasPropertySource
(
environment
,
PropertySource
.
class
,
"commandLineArgs"
));
}
@Test
...
...
@@ -261,10 +291,9 @@ public class SpringApplicationTests {
application
.
setAddCommandLineProperties
(
false
);
ConfigurableEnvironment
environment
=
new
StandardEnvironment
();
application
.
setEnvironment
(
environment
);
application
.
run
();
assertThat
(
hasPropertySource
(
environment
,
MapPropertySource
.
class
,
"commandLineArgs"
),
equalTo
(
false
));
application
.
run
(
"--foo=bar"
);
assertFalse
(
hasPropertySource
(
environment
,
PropertySource
.
class
,
"commandLineArgs"
));
}
@Test
...
...
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