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
80650f48
Commit
80650f48
authored
Jul 10, 2019
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish "Allow depended on beans to be identified by type"
See gh-17020
parent
9923ffe9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
31 deletions
+26
-31
AbstractDependsOnBeanFactoryPostProcessor.java
...oconfigure/AbstractDependsOnBeanFactoryPostProcessor.java
+18
-23
AbstractDependsOnBeanFactoryPostProcessorTests.java
...igure/AbstractDependsOnBeanFactoryPostProcessorTests.java
+8
-8
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AbstractDependsOnBeanFactoryPostProcessor.java
View file @
80650f48
...
@@ -17,8 +17,10 @@
...
@@ -17,8 +17,10 @@
package
org
.
springframework
.
boot
.
autoconfigure
;
package
org
.
springframework
.
boot
.
autoconfigure
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.HashSet
;
import
java.util.LinkedHashSet
;
import
java.util.LinkedHashSet
;
import
java.util.Set
;
import
java.util.Set
;
import
java.util.function.Function
;
import
java.util.stream.Collectors
;
import
java.util.stream.Collectors
;
import
org.springframework.beans.factory.BeanFactory
;
import
org.springframework.beans.factory.BeanFactory
;
...
@@ -33,8 +35,8 @@ import org.springframework.util.StringUtils;
...
@@ -33,8 +35,8 @@ import org.springframework.util.StringUtils;
/**
/**
* Abstract base class for a {@link BeanFactoryPostProcessor} that can be used to
* Abstract base class for a {@link BeanFactoryPostProcessor} that can be used to
* dynamically declare that all beans of a specific type should depend on
one or more
* dynamically declare that all beans of a specific type should depend on
specific other
*
specific beans
.
*
beans identified by name or type
.
*
*
* @author Marcel Overdijk
* @author Marcel Overdijk
* @author Dave Syer
* @author Dave Syer
...
@@ -50,27 +52,29 @@ public abstract class AbstractDependsOnBeanFactoryPostProcessor implements BeanF
...
@@ -50,27 +52,29 @@ public abstract class AbstractDependsOnBeanFactoryPostProcessor implements BeanF
private
final
Class
<?
extends
FactoryBean
<?>>
factoryBeanClass
;
private
final
Class
<?
extends
FactoryBean
<?>>
factoryBeanClass
;
private
final
Object
[]
dependsOn
;
private
final
Function
<
ListableBeanFactory
,
Set
<
String
>>
dependsOn
;
protected
AbstractDependsOnBeanFactoryPostProcessor
(
Class
<?>
beanClass
,
protected
AbstractDependsOnBeanFactoryPostProcessor
(
Class
<?>
beanClass
,
Class
<?
extends
FactoryBean
<?>>
factoryBeanClass
,
String
...
dependsOn
)
{
Class
<?
extends
FactoryBean
<?>>
factoryBeanClass
,
String
...
dependsOn
)
{
this
.
beanClass
=
beanClass
;
this
.
beanClass
=
beanClass
;
this
.
factoryBeanClass
=
factoryBeanClass
;
this
.
factoryBeanClass
=
factoryBeanClass
;
this
.
dependsOn
=
dependsOn
;
this
.
dependsOn
=
(
beanFactory
)
->
new
HashSet
<>(
Arrays
.
asList
(
dependsOn
))
;
}
}
/**
/**
* Create an instance with target bean and factory bean classes and dependency types.
* Create an instance with target bean and factory bean classes and dependency types.
* @param beanClass target bean class
* @param beanClass target bean class
* @param factoryBeanClass target factory bean class
* @param factoryBeanClass target factory bean class
* @param depend
sOn
dependency types
* @param depend
encyTypes
dependency types
* @since 2.
2.0
* @since 2.
1.7
*/
*/
protected
AbstractDependsOnBeanFactoryPostProcessor
(
Class
<?>
beanClass
,
protected
AbstractDependsOnBeanFactoryPostProcessor
(
Class
<?>
beanClass
,
Class
<?
extends
FactoryBean
<?>>
factoryBeanClass
,
Class
<?>...
depend
sOn
)
{
Class
<?
extends
FactoryBean
<?>>
factoryBeanClass
,
Class
<?>...
depend
encyTypes
)
{
this
.
beanClass
=
beanClass
;
this
.
beanClass
=
beanClass
;
this
.
factoryBeanClass
=
factoryBeanClass
;
this
.
factoryBeanClass
=
factoryBeanClass
;
this
.
dependsOn
=
dependsOn
;
this
.
dependsOn
=
(
beanFactory
)
->
Arrays
.
stream
(
dependencyTypes
)
.
flatMap
((
dependencyType
)
->
getBeanNames
(
beanFactory
,
dependencyType
).
stream
())
.
collect
(
Collectors
.
toSet
());
}
}
/**
/**
...
@@ -86,11 +90,11 @@ public abstract class AbstractDependsOnBeanFactoryPostProcessor implements BeanF
...
@@ -86,11 +90,11 @@ public abstract class AbstractDependsOnBeanFactoryPostProcessor implements BeanF
/**
/**
* Create an instance with target bean class and dependency types.
* Create an instance with target bean class and dependency types.
* @param beanClass target bean class
* @param beanClass target bean class
* @param depend
sOn
dependency types
* @param depend
encyTypes
dependency types
* @since 2.
2.0
* @since 2.
1.7
*/
*/
protected
AbstractDependsOnBeanFactoryPostProcessor
(
Class
<?>
beanClass
,
Class
<?>...
depend
sOn
)
{
protected
AbstractDependsOnBeanFactoryPostProcessor
(
Class
<?>
beanClass
,
Class
<?>...
depend
encyTypes
)
{
this
(
beanClass
,
null
,
depend
sOn
);
this
(
beanClass
,
null
,
depend
encyTypes
);
}
}
@Override
@Override
...
@@ -98,22 +102,13 @@ public abstract class AbstractDependsOnBeanFactoryPostProcessor implements BeanF
...
@@ -98,22 +102,13 @@ public abstract class AbstractDependsOnBeanFactoryPostProcessor implements BeanF
for
(
String
beanName
:
getBeanNames
(
beanFactory
))
{
for
(
String
beanName
:
getBeanNames
(
beanFactory
))
{
BeanDefinition
definition
=
getBeanDefinition
(
beanName
,
beanFactory
);
BeanDefinition
definition
=
getBeanDefinition
(
beanName
,
beanFactory
);
String
[]
dependencies
=
definition
.
getDependsOn
();
String
[]
dependencies
=
definition
.
getDependsOn
();
for
(
String
bean
:
getDependsOn
(
beanFactory
))
{
for
(
String
dependencyName
:
this
.
dependsOn
.
apply
(
beanFactory
))
{
dependencies
=
StringUtils
.
addStringToArray
(
dependencies
,
bean
);
dependencies
=
StringUtils
.
addStringToArray
(
dependencies
,
dependencyName
);
}
}
definition
.
setDependsOn
(
dependencies
);
definition
.
setDependsOn
(
dependencies
);
}
}
}
}
private
Set
<
String
>
getDependsOn
(
ListableBeanFactory
beanFactory
)
{
if
(
this
.
dependsOn
instanceof
Class
[])
{
return
Arrays
.
stream
(((
Class
[])
this
.
dependsOn
))
.
flatMap
((
beanClass
)
->
getBeanNames
(
beanFactory
,
beanClass
).
stream
())
.
collect
(
Collectors
.
toCollection
(
LinkedHashSet:
:
new
));
}
return
Arrays
.
stream
(
this
.
dependsOn
).
map
(
String:
:
valueOf
).
collect
(
Collectors
.
toCollection
(
LinkedHashSet:
:
new
));
}
private
Set
<
String
>
getBeanNames
(
ListableBeanFactory
beanFactory
)
{
private
Set
<
String
>
getBeanNames
(
ListableBeanFactory
beanFactory
)
{
Set
<
String
>
names
=
getBeanNames
(
beanFactory
,
this
.
beanClass
);
Set
<
String
>
names
=
getBeanNames
(
beanFactory
,
this
.
beanClass
);
if
(
this
.
factoryBeanClass
!=
null
)
{
if
(
this
.
factoryBeanClass
!=
null
)
{
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AbstractDependsOnBeanFactoryPostProcessorTests.java
View file @
80650f48
...
@@ -16,7 +16,7 @@
...
@@ -16,7 +16,7 @@
package
org
.
springframework
.
boot
.
autoconfigure
;
package
org
.
springframework
.
boot
.
autoconfigure
;
import
org.junit.
jupiter.api.
Test
;
import
org.junit.Test
;
import
org.springframework.beans.factory.BeanFactory
;
import
org.springframework.beans.factory.BeanFactory
;
import
org.springframework.beans.factory.FactoryBean
;
import
org.springframework.beans.factory.FactoryBean
;
...
@@ -36,27 +36,27 @@ import static org.assertj.core.api.Assertions.assertThat;
...
@@ -36,27 +36,27 @@ import static org.assertj.core.api.Assertions.assertThat;
*
*
* @author Dmytro Nosan
* @author Dmytro Nosan
*/
*/
class
AbstractDependsOnBeanFactoryPostProcessorTests
{
public
class
AbstractDependsOnBeanFactoryPostProcessorTests
{
private
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
private
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
.
withUserConfiguration
(
FooBarConfiguration
.
class
);
.
withUserConfiguration
(
FooBarConfiguration
.
class
);
@Test
@Test
void
fooBeansShouldDependOnBarBeanNames
()
{
public
void
fooBeansShouldDependOnBarBeanNames
()
{
this
.
contextRunner
this
.
contextRunner
.
withUserConfiguration
(
FooDependsOnBarNamePostProcessor
.
class
,
FooBarFactoryBeanConfiguration
.
class
)
.
withUserConfiguration
(
FooDependsOnBarNamePostProcessor
.
class
,
FooBarFactoryBeanConfiguration
.
class
)
.
run
(
this
::
assertThatFooDependsOnBar
);
.
run
(
this
::
assertThatFooDependsOnBar
);
}
}
@Test
@Test
void
fooBeansShouldDependOnBarBeanTypes
()
{
public
void
fooBeansShouldDependOnBarBeanTypes
()
{
this
.
contextRunner
this
.
contextRunner
.
withUserConfiguration
(
FooDependsOnBarTypePostProcessor
.
class
,
FooBarFactoryBeanConfiguration
.
class
)
.
withUserConfiguration
(
FooDependsOnBarTypePostProcessor
.
class
,
FooBarFactoryBeanConfiguration
.
class
)
.
run
(
this
::
assertThatFooDependsOnBar
);
.
run
(
this
::
assertThatFooDependsOnBar
);
}
}
@Test
@Test
void
fooBeansShouldDependOnBarBeanNamesParentContext
()
{
public
void
fooBeansShouldDependOnBarBeanNamesParentContext
()
{
try
(
AnnotationConfigApplicationContext
parentContext
=
new
AnnotationConfigApplicationContext
(
try
(
AnnotationConfigApplicationContext
parentContext
=
new
AnnotationConfigApplicationContext
(
FooBarFactoryBeanConfiguration
.
class
))
{
FooBarFactoryBeanConfiguration
.
class
))
{
this
.
contextRunner
.
withUserConfiguration
(
FooDependsOnBarNamePostProcessor
.
class
).
withParent
(
parentContext
)
this
.
contextRunner
.
withUserConfiguration
(
FooDependsOnBarNamePostProcessor
.
class
).
withParent
(
parentContext
)
...
@@ -65,7 +65,7 @@ class AbstractDependsOnBeanFactoryPostProcessorTests {
...
@@ -65,7 +65,7 @@ class AbstractDependsOnBeanFactoryPostProcessorTests {
}
}
@Test
@Test
void
fooBeansShouldDependOnBarBeanTypesParentContext
()
{
public
void
fooBeansShouldDependOnBarBeanTypesParentContext
()
{
try
(
AnnotationConfigApplicationContext
parentContext
=
new
AnnotationConfigApplicationContext
(
try
(
AnnotationConfigApplicationContext
parentContext
=
new
AnnotationConfigApplicationContext
(
FooBarFactoryBeanConfiguration
.
class
))
{
FooBarFactoryBeanConfiguration
.
class
))
{
this
.
contextRunner
.
withUserConfiguration
(
FooDependsOnBarTypePostProcessor
.
class
).
withParent
(
parentContext
)
this
.
contextRunner
.
withUserConfiguration
(
FooDependsOnBarTypePostProcessor
.
class
).
withParent
(
parentContext
)
...
@@ -101,7 +101,7 @@ class AbstractDependsOnBeanFactoryPostProcessorTests {
...
@@ -101,7 +101,7 @@ class AbstractDependsOnBeanFactoryPostProcessorTests {
}
}
@Configuration
(
proxyBeanMethods
=
false
)
@Configuration
static
class
FooBarFactoryBeanConfiguration
{
static
class
FooBarFactoryBeanConfiguration
{
@Bean
@Bean
...
@@ -116,7 +116,7 @@ class AbstractDependsOnBeanFactoryPostProcessorTests {
...
@@ -116,7 +116,7 @@ class AbstractDependsOnBeanFactoryPostProcessorTests {
}
}
@Configuration
(
proxyBeanMethods
=
false
)
@Configuration
static
class
FooBarConfiguration
{
static
class
FooBarConfiguration
{
@Bean
@Bean
...
...
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