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
b488a3d9
Commit
b488a3d9
authored
Aug 31, 2016
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '1.3.x'
parents
9874c22e
63b8e82c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
95 additions
and
3 deletions
+95
-3
OnBeanCondition.java
...amework/boot/autoconfigure/condition/OnBeanCondition.java
+2
-2
ConditionalOnMissingBeanTests.java
...utoconfigure/condition/ConditionalOnMissingBeanTests.java
+39
-0
ConditionalOnSingleCandidateTests.java
...onfigure/condition/ConditionalOnSingleCandidateTests.java
+54
-1
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnBeanCondition.java
View file @
b488a3d9
/*
/*
* Copyright 2012-201
5
the original author or authors.
* Copyright 2012-201
6
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -132,7 +132,7 @@ class OnBeanCondition extends SpringBootCondition implements ConfigurationCondit
...
@@ -132,7 +132,7 @@ class OnBeanCondition extends SpringBootCondition implements ConfigurationCondit
return
Collections
.
emptyList
();
return
Collections
.
emptyList
();
}
}
List
<
String
>
beanNames
=
new
ArrayList
<
String
>();
List
<
String
>
beanNames
=
new
ArrayList
<
String
>();
boolean
considerHierarchy
=
beans
.
getStrategy
()
==
SearchStrategy
.
ALL
;
boolean
considerHierarchy
=
beans
.
getStrategy
()
!=
SearchStrategy
.
CURRENT
;
for
(
String
type
:
beans
.
getTypes
())
{
for
(
String
type
:
beans
.
getTypes
())
{
beanNames
.
addAll
(
getBeanNamesForType
(
beanFactory
,
type
,
beanNames
.
addAll
(
getBeanNamesForType
(
beanFactory
,
type
,
context
.
getClassLoader
(),
considerHierarchy
));
context
.
getClassLoader
(),
considerHierarchy
));
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBeanTests.java
View file @
b488a3d9
...
@@ -257,6 +257,45 @@ public class ConditionalOnMissingBeanTests {
...
@@ -257,6 +257,45 @@ public class ConditionalOnMissingBeanTests {
assertThat
(
this
.
context
.
getBeansOfType
(
CustomExampleBean
.
class
)).
hasSize
(
1
);
assertThat
(
this
.
context
.
getBeansOfType
(
CustomExampleBean
.
class
)).
hasSize
(
1
);
}
}
@Test
public
void
grandparentIsConsideredWhenUsingParentsStrategy
()
{
this
.
context
.
register
(
ExampleBeanConfiguration
.
class
);
this
.
context
.
refresh
();
AnnotationConfigApplicationContext
parent
=
new
AnnotationConfigApplicationContext
();
parent
.
setParent
(
this
.
context
);
parent
.
refresh
();
AnnotationConfigApplicationContext
child
=
new
AnnotationConfigApplicationContext
();
child
.
setParent
(
parent
);
child
.
register
(
ExampleBeanConfiguration
.
class
,
OnBeanInParentsConfiguration
.
class
);
child
.
refresh
();
assertThat
(
child
.
getBeansOfType
(
ExampleBean
.
class
)).
hasSize
(
1
);
child
.
close
();
parent
.
close
();
}
@Test
public
void
currentContextIsIgnoredWhenUsingParentsStrategy
()
{
this
.
context
.
refresh
();
AnnotationConfigApplicationContext
child
=
new
AnnotationConfigApplicationContext
();
child
.
register
(
ExampleBeanConfiguration
.
class
,
OnBeanInParentsConfiguration
.
class
);
child
.
setParent
(
this
.
context
);
child
.
refresh
();
assertThat
(
child
.
getBeansOfType
(
ExampleBean
.
class
)).
hasSize
(
1
);
}
@Configuration
protected
static
class
OnBeanInParentsConfiguration
{
@Bean
@ConditionalOnMissingBean
(
search
=
SearchStrategy
.
PARENTS
)
public
ExampleBean
exampleBean2
()
{
return
new
ExampleBean
(
"test"
);
}
}
@Configuration
@Configuration
@ConditionalOnMissingBean
(
name
=
"foo"
)
@ConditionalOnMissingBean
(
name
=
"foo"
)
protected
static
class
OnBeanNameConfiguration
{
protected
static
class
OnBeanNameConfiguration
{
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnSingleCandidateTests.java
View file @
b488a3d9
...
@@ -62,6 +62,46 @@ public class ConditionalOnSingleCandidateTests {
...
@@ -62,6 +62,46 @@ public class ConditionalOnSingleCandidateTests {
assertThat
(
this
.
context
.
getBean
(
"baz"
)).
isEqualTo
(
"foo"
);
assertThat
(
this
.
context
.
getBean
(
"baz"
)).
isEqualTo
(
"foo"
);
}
}
@Test
public
void
singleCandidateInParentsOneCandidateInCurrent
()
{
load
();
AnnotationConfigApplicationContext
child
=
new
AnnotationConfigApplicationContext
();
child
.
register
(
FooConfiguration
.
class
,
OnBeanSingleCandidateInParentsConfiguration
.
class
);
child
.
setParent
(
this
.
context
);
child
.
refresh
();
assertThat
(
child
.
containsBean
(
"baz"
)).
isFalse
();
child
.
close
();
}
@Test
public
void
singleCandidateInParentsOneCandidateInParent
()
{
load
(
FooConfiguration
.
class
);
AnnotationConfigApplicationContext
child
=
new
AnnotationConfigApplicationContext
();
child
.
register
(
OnBeanSingleCandidateInParentsConfiguration
.
class
);
child
.
setParent
(
this
.
context
);
child
.
refresh
();
assertThat
(
child
.
containsBean
(
"baz"
)).
isTrue
();
assertThat
(
child
.
getBean
(
"baz"
)).
isEqualTo
(
"foo"
);
child
.
close
();
}
@Test
public
void
singleCandidateInParentsOneCandidateInGrandparent
()
{
load
(
FooConfiguration
.
class
);
AnnotationConfigApplicationContext
parent
=
new
AnnotationConfigApplicationContext
();
parent
.
setParent
(
this
.
context
);
parent
.
refresh
();
AnnotationConfigApplicationContext
child
=
new
AnnotationConfigApplicationContext
();
child
.
register
(
OnBeanSingleCandidateInParentsConfiguration
.
class
);
child
.
setParent
(
parent
);
child
.
refresh
();
assertThat
(
child
.
containsBean
(
"baz"
)).
isTrue
();
assertThat
(
child
.
getBean
(
"baz"
)).
isEqualTo
(
"foo"
);
child
.
close
();
parent
.
close
();
}
@Test
@Test
public
void
singleCandidateMultipleCandidates
()
{
public
void
singleCandidateMultipleCandidates
()
{
load
(
FooConfiguration
.
class
,
BarConfiguration
.
class
,
load
(
FooConfiguration
.
class
,
BarConfiguration
.
class
,
...
@@ -119,7 +159,9 @@ public class ConditionalOnSingleCandidateTests {
...
@@ -119,7 +159,9 @@ public class ConditionalOnSingleCandidateTests {
}
}
private
void
load
(
Class
<?>...
classes
)
{
private
void
load
(
Class
<?>...
classes
)
{
this
.
context
.
register
(
classes
);
if
(
classes
.
length
>
0
)
{
this
.
context
.
register
(
classes
);
}
this
.
context
.
refresh
();
this
.
context
.
refresh
();
}
}
...
@@ -134,6 +176,17 @@ public class ConditionalOnSingleCandidateTests {
...
@@ -134,6 +176,17 @@ public class ConditionalOnSingleCandidateTests {
}
}
@Configuration
@ConditionalOnSingleCandidate
(
value
=
String
.
class
,
search
=
SearchStrategy
.
PARENTS
)
protected
static
class
OnBeanSingleCandidateInParentsConfiguration
{
@Bean
public
String
baz
(
String
s
)
{
return
s
;
}
}
@Configuration
@Configuration
@ConditionalOnSingleCandidate
(
value
=
String
.
class
,
type
=
"java.lang.String"
)
@ConditionalOnSingleCandidate
(
value
=
String
.
class
,
type
=
"java.lang.String"
)
protected
static
class
OnBeanSingleCandidateTwoTypesConfiguration
{
protected
static
class
OnBeanSingleCandidateTwoTypesConfiguration
{
...
...
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