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
55f6fb94
Commit
55f6fb94
authored
Aug 26, 2016
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '1.3.x'
parents
6df279d3
a5c6b095
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
5 deletions
+40
-5
OnBeanCondition.java
...amework/boot/autoconfigure/condition/OnBeanCondition.java
+23
-5
ConditionalOnSingleCandidateTests.java
...onfigure/condition/ConditionalOnSingleCandidateTests.java
+17
-0
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnBeanCondition.java
View file @
55f6fb94
...
@@ -94,7 +94,8 @@ class OnBeanCondition extends SpringBootCondition implements ConfigurationCondit
...
@@ -94,7 +94,8 @@ class OnBeanCondition extends SpringBootCondition implements ConfigurationCondit
return
ConditionOutcome
.
noMatch
(
return
ConditionOutcome
.
noMatch
(
"@ConditionalOnSingleCandidate "
+
spec
+
" found no beans"
);
"@ConditionalOnSingleCandidate "
+
spec
+
" found no beans"
);
}
}
else
if
(!
hasSingleAutowireCandidate
(
context
.
getBeanFactory
(),
matching
))
{
else
if
(!
hasSingleAutowireCandidate
(
context
.
getBeanFactory
(),
matching
,
spec
.
getStrategy
()
==
SearchStrategy
.
ALL
))
{
return
ConditionOutcome
.
noMatch
(
"@ConditionalOnSingleCandidate "
+
spec
return
ConditionOutcome
.
noMatch
(
"@ConditionalOnSingleCandidate "
+
spec
+
" found no primary candidate amongst the"
+
" following "
+
" found no primary candidate amongst the"
+
" following "
+
matching
);
+
matching
);
...
@@ -222,16 +223,19 @@ class OnBeanCondition extends SpringBootCondition implements ConfigurationCondit
...
@@ -222,16 +223,19 @@ class OnBeanCondition extends SpringBootCondition implements ConfigurationCondit
}
}
private
boolean
hasSingleAutowireCandidate
(
private
boolean
hasSingleAutowireCandidate
(
ConfigurableListableBeanFactory
beanFactory
,
List
<
String
>
beanNames
)
{
ConfigurableListableBeanFactory
beanFactory
,
List
<
String
>
beanNames
,
boolean
considerHierarchy
)
{
return
(
beanNames
.
size
()
==
1
return
(
beanNames
.
size
()
==
1
||
getPrimaryBeans
(
beanFactory
,
beanNames
).
size
()
==
1
);
||
getPrimaryBeans
(
beanFactory
,
beanNames
,
considerHierarchy
)
.
size
()
==
1
);
}
}
private
List
<
String
>
getPrimaryBeans
(
ConfigurableListableBeanFactory
beanFactory
,
private
List
<
String
>
getPrimaryBeans
(
ConfigurableListableBeanFactory
beanFactory
,
List
<
String
>
beanNames
)
{
List
<
String
>
beanNames
,
boolean
considerHierarchy
)
{
List
<
String
>
primaryBeans
=
new
ArrayList
<
String
>();
List
<
String
>
primaryBeans
=
new
ArrayList
<
String
>();
for
(
String
beanName
:
beanNames
)
{
for
(
String
beanName
:
beanNames
)
{
BeanDefinition
beanDefinition
=
beanFactory
.
getBeanDefinition
(
beanName
);
BeanDefinition
beanDefinition
=
findBeanDefinition
(
beanFactory
,
beanName
,
considerHierarchy
);
if
(
beanDefinition
!=
null
&&
beanDefinition
.
isPrimary
())
{
if
(
beanDefinition
!=
null
&&
beanDefinition
.
isPrimary
())
{
primaryBeans
.
add
(
beanName
);
primaryBeans
.
add
(
beanName
);
}
}
...
@@ -239,6 +243,20 @@ class OnBeanCondition extends SpringBootCondition implements ConfigurationCondit
...
@@ -239,6 +243,20 @@ class OnBeanCondition extends SpringBootCondition implements ConfigurationCondit
return
primaryBeans
;
return
primaryBeans
;
}
}
private
BeanDefinition
findBeanDefinition
(
ConfigurableListableBeanFactory
beanFactory
,
String
beanName
,
boolean
considerHierarchy
)
{
if
(
beanFactory
.
containsBeanDefinition
(
beanName
))
{
return
beanFactory
.
getBeanDefinition
(
beanName
);
}
if
(
considerHierarchy
&&
beanFactory
.
getParentBeanFactory
()
instanceof
ConfigurableListableBeanFactory
)
{
return
findBeanDefinition
(((
ConfigurableListableBeanFactory
)
beanFactory
.
getParentBeanFactory
()),
beanName
,
considerHierarchy
);
}
return
null
;
}
private
static
class
BeanSearchSpec
{
private
static
class
BeanSearchSpec
{
private
final
Class
<?>
annotationType
;
private
final
Class
<?>
annotationType
;
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnSingleCandidateTests.java
View file @
55f6fb94
...
@@ -33,6 +33,7 @@ import static org.hamcrest.CoreMatchers.isA;
...
@@ -33,6 +33,7 @@ import static org.hamcrest.CoreMatchers.isA;
* Tests for {@link ConditionalOnSingleCandidate}.
* Tests for {@link ConditionalOnSingleCandidate}.
*
*
* @author Stephane Nicoll
* @author Stephane Nicoll
* @author Andy Wilkinson
*/
*/
public
class
ConditionalOnSingleCandidateTests
{
public
class
ConditionalOnSingleCandidateTests
{
...
@@ -101,6 +102,22 @@ public class ConditionalOnSingleCandidateTests {
...
@@ -101,6 +102,22 @@ public class ConditionalOnSingleCandidateTests {
load
(
OnBeanSingleCandidateNoTypeConfiguration
.
class
);
load
(
OnBeanSingleCandidateNoTypeConfiguration
.
class
);
}
}
@Test
public
void
singleCandidateMultipleCandidatesInContextHierarchy
()
{
load
(
FooPrimaryConfiguration
.
class
,
BarConfiguration
.
class
);
AnnotationConfigApplicationContext
child
=
new
AnnotationConfigApplicationContext
();
child
.
setParent
(
this
.
context
);
child
.
register
(
OnBeanSingleCandidateConfiguration
.
class
);
try
{
child
.
refresh
();
assertThat
(
child
.
containsBean
(
"baz"
)).
isTrue
();
assertThat
(
child
.
getBean
(
"baz"
)).
isEqualTo
(
"foo"
);
}
finally
{
child
.
close
();
}
}
private
void
load
(
Class
<?>...
classes
)
{
private
void
load
(
Class
<?>...
classes
)
{
this
.
context
.
register
(
classes
);
this
.
context
.
register
(
classes
);
this
.
context
.
refresh
();
this
.
context
.
refresh
();
...
...
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