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
1bd1ffdb
Commit
1bd1ffdb
authored
Oct 23, 2018
by
Phillip Webb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish
parent
34156b21
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
67 additions
and
84 deletions
+67
-84
BeanTypeRegistry.java
...mework/boot/autoconfigure/condition/BeanTypeRegistry.java
+23
-23
ConditionalOnMissingBeanTests.java
...utoconfigure/condition/ConditionalOnMissingBeanTests.java
+44
-61
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/BeanTypeRegistry.java
View file @
1bd1ffdb
...
@@ -83,27 +83,6 @@ final class BeanTypeRegistry implements SmartInitializingSingleton {
...
@@ -83,27 +83,6 @@ final class BeanTypeRegistry implements SmartInitializingSingleton {
this
.
beanFactory
=
beanFactory
;
this
.
beanFactory
=
beanFactory
;
}
}
/**
* Factory method to get the {@link BeanTypeRegistry} for a given {@link BeanFactory}.
* @param beanFactory the source bean factory
* @return the {@link BeanTypeRegistry} for the given bean factory
*/
static
BeanTypeRegistry
get
(
ListableBeanFactory
beanFactory
)
{
Assert
.
isInstanceOf
(
DefaultListableBeanFactory
.
class
,
beanFactory
);
DefaultListableBeanFactory
listableBeanFactory
=
(
DefaultListableBeanFactory
)
beanFactory
;
Assert
.
isTrue
(
listableBeanFactory
.
isAllowEagerClassLoading
(),
"Bean factory must allow eager class loading"
);
if
(!
listableBeanFactory
.
containsLocalBean
(
BEAN_NAME
))
{
BeanDefinition
bd
=
BeanDefinitionBuilder
.
genericBeanDefinition
(
BeanTypeRegistry
.
class
,
()
->
new
BeanTypeRegistry
(
(
DefaultListableBeanFactory
)
beanFactory
))
.
getBeanDefinition
();
listableBeanFactory
.
registerBeanDefinition
(
BEAN_NAME
,
bd
);
}
return
listableBeanFactory
.
getBean
(
BEAN_NAME
,
BeanTypeRegistry
.
class
);
}
/**
/**
* Return the names of beans matching the given type (including subclasses), judging
* Return the names of beans matching the given type (including subclasses), judging
* from either bean definitions or the value of {@link FactoryBean#getObjectType()} in
* from either bean definitions or the value of {@link FactoryBean#getObjectType()} in
...
@@ -113,7 +92,7 @@ final class BeanTypeRegistry implements SmartInitializingSingleton {
...
@@ -113,7 +92,7 @@ final class BeanTypeRegistry implements SmartInitializingSingleton {
* @return the names of beans (or objects created by FactoryBeans) matching the given
* @return the names of beans (or objects created by FactoryBeans) matching the given
* object type (including subclasses), or an empty set if none
* object type (including subclasses), or an empty set if none
*/
*/
Set
<
String
>
getNamesForType
(
Class
<?>
type
)
{
public
Set
<
String
>
getNamesForType
(
Class
<?>
type
)
{
updateTypesIfNecessary
();
updateTypesIfNecessary
();
return
this
.
beanTypes
.
entrySet
().
stream
()
return
this
.
beanTypes
.
entrySet
().
stream
()
.
filter
((
entry
)
->
entry
.
getValue
()
!=
null
.
filter
((
entry
)
->
entry
.
getValue
()
!=
null
...
@@ -131,7 +110,7 @@ final class BeanTypeRegistry implements SmartInitializingSingleton {
...
@@ -131,7 +110,7 @@ final class BeanTypeRegistry implements SmartInitializingSingleton {
* @return the names of beans (or objects created by FactoryBeans) annotated with the
* @return the names of beans (or objects created by FactoryBeans) annotated with the
* given annotation, or an empty set if none
* given annotation, or an empty set if none
*/
*/
Set
<
String
>
getNamesForAnnotation
(
Class
<?
extends
Annotation
>
annotation
)
{
public
Set
<
String
>
getNamesForAnnotation
(
Class
<?
extends
Annotation
>
annotation
)
{
updateTypesIfNecessary
();
updateTypesIfNecessary
();
return
this
.
beanTypes
.
entrySet
().
stream
()
return
this
.
beanTypes
.
entrySet
().
stream
()
.
filter
((
entry
)
->
entry
.
getValue
()
!=
null
&&
AnnotationUtils
.
filter
((
entry
)
->
entry
.
getValue
()
!=
null
&&
AnnotationUtils
...
@@ -352,4 +331,25 @@ final class BeanTypeRegistry implements SmartInitializingSingleton {
...
@@ -352,4 +331,25 @@ final class BeanTypeRegistry implements SmartInitializingSingleton {
return
null
;
return
null
;
}
}
/**
* Factory method to get the {@link BeanTypeRegistry} for a given {@link BeanFactory}.
* @param beanFactory the source bean factory
* @return the {@link BeanTypeRegistry} for the given bean factory
*/
static
BeanTypeRegistry
get
(
ListableBeanFactory
beanFactory
)
{
Assert
.
isInstanceOf
(
DefaultListableBeanFactory
.
class
,
beanFactory
);
DefaultListableBeanFactory
listableBeanFactory
=
(
DefaultListableBeanFactory
)
beanFactory
;
Assert
.
isTrue
(
listableBeanFactory
.
isAllowEagerClassLoading
(),
"Bean factory must allow eager class loading"
);
if
(!
listableBeanFactory
.
containsLocalBean
(
BEAN_NAME
))
{
BeanDefinition
bd
=
BeanDefinitionBuilder
.
genericBeanDefinition
(
BeanTypeRegistry
.
class
,
()
->
new
BeanTypeRegistry
(
(
DefaultListableBeanFactory
)
beanFactory
))
.
getBeanDefinition
();
listableBeanFactory
.
registerBeanDefinition
(
BEAN_NAME
,
bd
);
}
return
listableBeanFactory
.
getBean
(
BEAN_NAME
,
BeanTypeRegistry
.
class
);
}
}
}
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBeanTests.java
View file @
1bd1ffdb
/*
/*
* Copyright 2012-201
7
the original author or authors.
* Copyright 2012-201
8
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.
...
@@ -23,6 +23,7 @@ import java.lang.annotation.RetentionPolicy;
...
@@ -23,6 +23,7 @@ import java.lang.annotation.RetentionPolicy;
import
java.lang.annotation.Target
;
import
java.lang.annotation.Target
;
import
java.util.Date
;
import
java.util.Date
;
import
org.junit.After
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.beans.factory.FactoryBean
;
import
org.springframework.beans.factory.FactoryBean
;
...
@@ -61,40 +62,38 @@ public class ConditionalOnMissingBeanTests {
...
@@ -61,40 +62,38 @@ public class ConditionalOnMissingBeanTests {
private
final
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
();
private
final
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
();
@After
public
void
cleanup
()
{
this
.
context
.
close
();
}
@Test
@Test
public
void
testNameOnMissingBeanCondition
()
{
public
void
testNameOnMissingBeanCondition
()
{
this
.
context
.
register
(
FooConfiguration
.
class
,
OnBeanNameConfiguration
.
class
);
load
(
FooConfiguration
.
class
,
OnBeanNameConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
containsBean
(
"bar"
)).
isFalse
();
assertThat
(
this
.
context
.
containsBean
(
"bar"
)).
isFalse
();
assertThat
(
this
.
context
.
getBean
(
"foo"
)).
isEqualTo
(
"foo"
);
assertThat
(
this
.
context
.
getBean
(
"foo"
)).
isEqualTo
(
"foo"
);
}
}
@Test
@Test
public
void
testNameOnMissingBeanConditionReverseOrder
()
{
public
void
testNameOnMissingBeanConditionReverseOrder
()
{
this
.
context
.
register
(
OnBeanNameConfiguration
.
class
,
FooConfiguration
.
class
);
load
(
OnBeanNameConfiguration
.
class
,
FooConfiguration
.
class
);
this
.
context
.
refresh
();
// Ideally this would be false, but the ordering is a problem
// FIXME: ideally this would be false, but the ordering is a problem
assertThat
(
this
.
context
.
containsBean
(
"bar"
)).
isTrue
();
assertThat
(
this
.
context
.
containsBean
(
"bar"
)).
isTrue
();
assertThat
(
this
.
context
.
getBean
(
"foo"
)).
isEqualTo
(
"foo"
);
assertThat
(
this
.
context
.
getBean
(
"foo"
)).
isEqualTo
(
"foo"
);
}
}
@Test
@Test
public
void
testNameAndTypeOnMissingBeanCondition
()
{
public
void
testNameAndTypeOnMissingBeanCondition
()
{
this
.
context
.
register
(
FooConfiguration
.
class
,
load
(
FooConfiguration
.
class
,
OnBeanNameAndTypeConfiguration
.
class
);
OnBeanNameAndTypeConfiguration
.
class
);
// Arguably this should be true, but as things are implemented the conditions
this
.
context
.
refresh
();
// specified in the different attributes of @ConditionalOnBean are combined with
/*
// logical OR (not AND) so if any of them match the condition is true.
* Arguably this should be true, but as things are implemented the conditions
* specified in the different attributes of @ConditionalOnBean are combined with
* logical OR (not AND) so if any of them match the condition is true.
*/
assertThat
(
this
.
context
.
containsBean
(
"bar"
)).
isFalse
();
assertThat
(
this
.
context
.
containsBean
(
"bar"
)).
isFalse
();
}
}
@Test
@Test
public
void
hierarchyConsidered
()
{
public
void
hierarchyConsidered
()
{
this
.
context
.
register
(
FooConfiguration
.
class
);
load
(
FooConfiguration
.
class
);
this
.
context
.
refresh
();
AnnotationConfigApplicationContext
childContext
=
new
AnnotationConfigApplicationContext
();
AnnotationConfigApplicationContext
childContext
=
new
AnnotationConfigApplicationContext
();
childContext
.
setParent
(
this
.
context
);
childContext
.
setParent
(
this
.
context
);
childContext
.
register
(
HierarchyConsidered
.
class
);
childContext
.
register
(
HierarchyConsidered
.
class
);
...
@@ -104,8 +103,7 @@ public class ConditionalOnMissingBeanTests {
...
@@ -104,8 +103,7 @@ public class ConditionalOnMissingBeanTests {
@Test
@Test
public
void
hierarchyNotConsidered
()
{
public
void
hierarchyNotConsidered
()
{
this
.
context
.
register
(
FooConfiguration
.
class
);
load
(
FooConfiguration
.
class
);
this
.
context
.
refresh
();
AnnotationConfigApplicationContext
childContext
=
new
AnnotationConfigApplicationContext
();
AnnotationConfigApplicationContext
childContext
=
new
AnnotationConfigApplicationContext
();
childContext
.
setParent
(
this
.
context
);
childContext
.
setParent
(
this
.
context
);
childContext
.
register
(
HierarchyNotConsidered
.
class
);
childContext
.
register
(
HierarchyNotConsidered
.
class
);
...
@@ -115,15 +113,13 @@ public class ConditionalOnMissingBeanTests {
...
@@ -115,15 +113,13 @@ public class ConditionalOnMissingBeanTests {
@Test
@Test
public
void
impliedOnBeanMethod
()
{
public
void
impliedOnBeanMethod
()
{
this
.
context
.
register
(
ExampleBeanConfiguration
.
class
,
ImpliedOnBeanMethod
.
class
);
load
(
ExampleBeanConfiguration
.
class
,
ImpliedOnBeanMethod
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBeansOfType
(
ExampleBean
.
class
).
size
()).
isEqualTo
(
1
);
assertThat
(
this
.
context
.
getBeansOfType
(
ExampleBean
.
class
).
size
()).
isEqualTo
(
1
);
}
}
@Test
@Test
public
void
testAnnotationOnMissingBeanCondition
()
{
public
void
testAnnotationOnMissingBeanCondition
()
{
this
.
context
.
register
(
FooConfiguration
.
class
,
OnAnnotationConfiguration
.
class
);
load
(
FooConfiguration
.
class
,
OnAnnotationConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
containsBean
(
"bar"
)).
isFalse
();
assertThat
(
this
.
context
.
containsBean
(
"bar"
)).
isFalse
();
assertThat
(
this
.
context
.
getBean
(
"foo"
)).
isEqualTo
(
"foo"
);
assertThat
(
this
.
context
.
getBean
(
"foo"
)).
isEqualTo
(
"foo"
);
}
}
...
@@ -131,10 +127,9 @@ public class ConditionalOnMissingBeanTests {
...
@@ -131,10 +127,9 @@ public class ConditionalOnMissingBeanTests {
// Rigorous test for SPR-11069
// Rigorous test for SPR-11069
@Test
@Test
public
void
testAnnotationOnMissingBeanConditionWithEagerFactoryBean
()
{
public
void
testAnnotationOnMissingBeanConditionWithEagerFactoryBean
()
{
this
.
context
.
register
(
FooConfiguration
.
class
,
OnAnnotationConfiguration
.
class
,
load
(
FooConfiguration
.
class
,
OnAnnotationConfiguration
.
class
,
FactoryBeanXmlConfiguration
.
class
,
FactoryBeanXmlConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
containsBean
(
"bar"
)).
isFalse
();
assertThat
(
this
.
context
.
containsBean
(
"bar"
)).
isFalse
();
assertThat
(
this
.
context
.
containsBean
(
"example"
)).
isTrue
();
assertThat
(
this
.
context
.
containsBean
(
"example"
)).
isTrue
();
assertThat
(
this
.
context
.
getBean
(
"foo"
)).
isEqualTo
(
"foo"
);
assertThat
(
this
.
context
.
getBean
(
"foo"
)).
isEqualTo
(
"foo"
);
...
@@ -142,130 +137,109 @@ public class ConditionalOnMissingBeanTests {
...
@@ -142,130 +137,109 @@ public class ConditionalOnMissingBeanTests {
@Test
@Test
public
void
testOnMissingBeanConditionWithFactoryBean
()
{
public
void
testOnMissingBeanConditionWithFactoryBean
()
{
this
.
context
.
register
(
FactoryBeanConfiguration
.
class
,
load
(
FactoryBeanConfiguration
.
class
,
ConditionalOnFactoryBean
.
class
,
ConditionalOnFactoryBean
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBean
(
ExampleBean
.
class
).
toString
())
assertThat
(
this
.
context
.
getBean
(
ExampleBean
.
class
).
toString
())
.
isEqualTo
(
"fromFactory"
);
.
isEqualTo
(
"fromFactory"
);
}
}
@Test
@Test
public
void
testOnMissingBeanConditionWithComponentScannedFactoryBean
()
{
public
void
testOnMissingBeanConditionWithComponentScannedFactoryBean
()
{
this
.
context
.
register
(
ComponentScannedFactoryBeanBeanMethodConfiguration
.
class
,
load
(
ComponentScannedFactoryBeanBeanMethodConfiguration
.
class
,
ConditionalOnFactoryBean
.
class
,
ConditionalOnFactoryBean
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBean
(
ExampleBean
.
class
).
toString
())
assertThat
(
this
.
context
.
getBean
(
ExampleBean
.
class
).
toString
())
.
isEqualTo
(
"fromFactory"
);
.
isEqualTo
(
"fromFactory"
);
}
}
@Test
@Test
public
void
testOnMissingBeanConditionWithComponentScannedFactoryBeanWithBeanMethodArguments
()
{
public
void
testOnMissingBeanConditionWithComponentScannedFactoryBeanWithBeanMethodArguments
()
{
this
.
context
.
register
(
load
(
ComponentScannedFactoryBeanBeanMethodWithArgumentsConfiguration
.
class
,
ComponentScannedFactoryBeanBeanMethodWithArgumentsConfiguration
.
class
,
ConditionalOnFactoryBean
.
class
,
ConditionalOnFactoryBean
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBean
(
ExampleBean
.
class
).
toString
())
assertThat
(
this
.
context
.
getBean
(
ExampleBean
.
class
).
toString
())
.
isEqualTo
(
"fromFactory"
);
.
isEqualTo
(
"fromFactory"
);
}
}
@Test
@Test
public
void
testOnMissingBeanConditionWithFactoryBeanWithBeanMethodArguments
()
{
public
void
testOnMissingBeanConditionWithFactoryBeanWithBeanMethodArguments
()
{
this
.
context
.
register
(
FactoryBeanWithBeanMethodArgumentsConfiguration
.
class
,
load
(
new
Class
<?>[]
{
FactoryBeanWithBeanMethodArgumentsConfiguration
.
class
,
ConditionalOnFactoryBean
.
class
,
ConditionalOnFactoryBean
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
PropertyPlaceholderAutoConfiguration
.
class
},
"theValue:foo"
);
TestPropertyValues
.
of
(
"theValue:foo"
).
applyTo
(
this
.
context
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBean
(
ExampleBean
.
class
).
toString
())
assertThat
(
this
.
context
.
getBean
(
ExampleBean
.
class
).
toString
())
.
isEqualTo
(
"fromFactory"
);
.
isEqualTo
(
"fromFactory"
);
}
}
@Test
@Test
public
void
testOnMissingBeanConditionWithConcreteFactoryBean
()
{
public
void
testOnMissingBeanConditionWithConcreteFactoryBean
()
{
this
.
context
.
register
(
ConcreteFactoryBeanConfiguration
.
class
,
load
(
ConcreteFactoryBeanConfiguration
.
class
,
ConditionalOnFactoryBean
.
class
,
ConditionalOnFactoryBean
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBean
(
ExampleBean
.
class
).
toString
())
assertThat
(
this
.
context
.
getBean
(
ExampleBean
.
class
).
toString
())
.
isEqualTo
(
"fromFactory"
);
.
isEqualTo
(
"fromFactory"
);
}
}
@Test
@Test
public
void
testOnMissingBeanConditionWithUnhelpfulFactoryBean
()
{
public
void
testOnMissingBeanConditionWithUnhelpfulFactoryBean
()
{
this
.
context
.
register
(
UnhelpfulFactoryBeanConfiguration
.
class
,
load
(
UnhelpfulFactoryBeanConfiguration
.
class
,
ConditionalOnFactoryBean
.
class
,
ConditionalOnFactoryBean
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
// We could not tell that the FactoryBean would ultimately create an ExampleBean
// We could not tell that the FactoryBean would ultimately create an ExampleBean
assertThat
(
this
.
context
.
getBeansOfType
(
ExampleBean
.
class
).
values
()).
hasSize
(
2
);
assertThat
(
this
.
context
.
getBeansOfType
(
ExampleBean
.
class
).
values
()).
hasSize
(
2
);
}
}
@Test
@Test
public
void
testOnMissingBeanConditionWithRegisteredFactoryBean
()
{
public
void
testOnMissingBeanConditionWithRegisteredFactoryBean
()
{
this
.
context
.
register
(
RegisteredFactoryBeanConfiguration
.
class
,
load
(
RegisteredFactoryBeanConfiguration
.
class
,
ConditionalOnFactoryBean
.
class
,
ConditionalOnFactoryBean
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBean
(
ExampleBean
.
class
).
toString
())
assertThat
(
this
.
context
.
getBean
(
ExampleBean
.
class
).
toString
())
.
isEqualTo
(
"fromFactory"
);
.
isEqualTo
(
"fromFactory"
);
}
}
@Test
@Test
public
void
testOnMissingBeanConditionWithNonspecificFactoryBeanWithClassAttribute
()
{
public
void
testOnMissingBeanConditionWithNonspecificFactoryBeanWithClassAttribute
()
{
this
.
context
.
register
(
NonspecificFactoryBeanClassAttributeConfiguration
.
class
,
load
(
NonspecificFactoryBeanClassAttributeConfiguration
.
class
,
ConditionalOnFactoryBean
.
class
,
ConditionalOnFactoryBean
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBean
(
ExampleBean
.
class
).
toString
())
assertThat
(
this
.
context
.
getBean
(
ExampleBean
.
class
).
toString
())
.
isEqualTo
(
"fromFactory"
);
.
isEqualTo
(
"fromFactory"
);
}
}
@Test
@Test
public
void
testOnMissingBeanConditionWithNonspecificFactoryBeanWithStringAttribute
()
{
public
void
testOnMissingBeanConditionWithNonspecificFactoryBeanWithStringAttribute
()
{
this
.
context
.
register
(
NonspecificFactoryBeanStringAttributeConfiguration
.
class
,
load
(
NonspecificFactoryBeanStringAttributeConfiguration
.
class
,
ConditionalOnFactoryBean
.
class
,
ConditionalOnFactoryBean
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBean
(
ExampleBean
.
class
).
toString
())
assertThat
(
this
.
context
.
getBean
(
ExampleBean
.
class
).
toString
())
.
isEqualTo
(
"fromFactory"
);
.
isEqualTo
(
"fromFactory"
);
}
}
@Test
@Test
public
void
testOnMissingBeanConditionWithFactoryBeanInXml
()
{
public
void
testOnMissingBeanConditionWithFactoryBeanInXml
()
{
this
.
context
.
register
(
FactoryBeanXmlConfiguration
.
class
,
load
(
FactoryBeanXmlConfiguration
.
class
,
ConditionalOnFactoryBean
.
class
,
ConditionalOnFactoryBean
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBean
(
ExampleBean
.
class
).
toString
())
assertThat
(
this
.
context
.
getBean
(
ExampleBean
.
class
).
toString
())
.
isEqualTo
(
"fromFactory"
);
.
isEqualTo
(
"fromFactory"
);
}
}
@Test
@Test
public
void
testOnMissingBeanConditionWithIgnoredSubclass
()
{
public
void
testOnMissingBeanConditionWithIgnoredSubclass
()
{
this
.
context
.
register
(
CustomExampleBeanConfiguration
.
class
,
load
(
CustomExampleBeanConfiguration
.
class
,
ConditionalOnIgnoredSubclass
.
class
,
ConditionalOnIgnoredSubclass
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBeansOfType
(
ExampleBean
.
class
)).
hasSize
(
2
);
assertThat
(
this
.
context
.
getBeansOfType
(
ExampleBean
.
class
)).
hasSize
(
2
);
assertThat
(
this
.
context
.
getBeansOfType
(
CustomExampleBean
.
class
)).
hasSize
(
1
);
assertThat
(
this
.
context
.
getBeansOfType
(
CustomExampleBean
.
class
)).
hasSize
(
1
);
}
}
@Test
@Test
public
void
testOnMissingBeanConditionWithIgnoredSubclassByName
()
{
public
void
testOnMissingBeanConditionWithIgnoredSubclassByName
()
{
this
.
context
.
register
(
CustomExampleBeanConfiguration
.
class
,
load
(
CustomExampleBeanConfiguration
.
class
,
ConditionalOnIgnoredSubclassByName
.
class
,
ConditionalOnIgnoredSubclassByName
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBeansOfType
(
ExampleBean
.
class
)).
hasSize
(
2
);
assertThat
(
this
.
context
.
getBeansOfType
(
ExampleBean
.
class
)).
hasSize
(
2
);
assertThat
(
this
.
context
.
getBeansOfType
(
CustomExampleBean
.
class
)).
hasSize
(
1
);
assertThat
(
this
.
context
.
getBeansOfType
(
CustomExampleBean
.
class
)).
hasSize
(
1
);
}
}
@Test
@Test
public
void
grandparentIsConsideredWhenUsingAncestorsStrategy
()
{
public
void
grandparentIsConsideredWhenUsingAncestorsStrategy
()
{
this
.
context
.
register
(
ExampleBeanConfiguration
.
class
);
load
(
ExampleBeanConfiguration
.
class
);
this
.
context
.
refresh
();
AnnotationConfigApplicationContext
parent
=
new
AnnotationConfigApplicationContext
();
AnnotationConfigApplicationContext
parent
=
new
AnnotationConfigApplicationContext
();
parent
.
setParent
(
this
.
context
);
parent
.
setParent
(
this
.
context
);
parent
.
refresh
();
parent
.
refresh
();
...
@@ -292,13 +266,22 @@ public class ConditionalOnMissingBeanTests {
...
@@ -292,13 +266,22 @@ public class ConditionalOnMissingBeanTests {
@Test
@Test
public
void
beanProducedByFactoryBeanIsConsideredWhenMatchingOnAnnotation
()
{
public
void
beanProducedByFactoryBeanIsConsideredWhenMatchingOnAnnotation
()
{
this
.
context
.
register
(
ConcreteFactoryBeanConfiguration
.
class
,
load
(
ConcreteFactoryBeanConfiguration
.
class
,
OnAnnotationWithFactoryBeanConfiguration
.
class
);
OnAnnotationWithFactoryBeanConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
containsBean
(
"bar"
)).
isFalse
();
assertThat
(
this
.
context
.
containsBean
(
"bar"
)).
isFalse
();
assertThat
(
this
.
context
.
getBeansOfType
(
ExampleBean
.
class
)).
hasSize
(
1
);
assertThat
(
this
.
context
.
getBeansOfType
(
ExampleBean
.
class
)).
hasSize
(
1
);
}
}
private
void
load
(
Class
<?>...
configs
)
{
load
(
configs
,
new
String
[]
{});
}
private
void
load
(
Class
<?>[]
configs
,
String
...
environmentValues
)
{
this
.
context
.
register
(
configs
);
TestPropertyValues
.
of
(
environmentValues
).
applyTo
(
this
.
context
);
this
.
context
.
refresh
();
}
@Configuration
@Configuration
protected
static
class
OnBeanInAncestorsConfiguration
{
protected
static
class
OnBeanInAncestorsConfiguration
{
...
...
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