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
6874c42d
Commit
6874c42d
authored
May 19, 2021
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.3.x' into 2.4.x
Closes gh-26605
parents
78da54d0
2a89b11d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
106 additions
and
3 deletions
+106
-3
LazyInitializationBeanFactoryPostProcessor.java
...work/boot/LazyInitializationBeanFactoryPostProcessor.java
+16
-3
LazyInitializationBeanFactoryPostProcessorTests.java
...boot/LazyInitializationBeanFactoryPostProcessorTests.java
+90
-0
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/LazyInitializationBeanFactoryPostProcessor.java
View file @
6874c42d
...
@@ -16,10 +16,12 @@
...
@@ -16,10 +16,12 @@
package
org
.
springframework
.
boot
;
package
org
.
springframework
.
boot
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.Collection
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.NoSuchBeanDefinitionException
;
import
org.springframework.beans.factory.NoSuchBeanDefinitionException
;
import
org.springframework.beans.factory.SmartInitializingSingleton
;
import
org.springframework.beans.factory.config.BeanDefinition
;
import
org.springframework.beans.factory.config.BeanDefinition
;
import
org.springframework.beans.factory.config.BeanFactoryPostProcessor
;
import
org.springframework.beans.factory.config.BeanFactoryPostProcessor
;
import
org.springframework.beans.factory.config.ConfigurableListableBeanFactory
;
import
org.springframework.beans.factory.config.ConfigurableListableBeanFactory
;
...
@@ -30,6 +32,11 @@ import org.springframework.core.Ordered;
...
@@ -30,6 +32,11 @@ import org.springframework.core.Ordered;
* {@link BeanFactoryPostProcessor} to set lazy-init on bean definitions that are not
* {@link BeanFactoryPostProcessor} to set lazy-init on bean definitions that are not
* {@link LazyInitializationExcludeFilter excluded} and have not already had a value
* {@link LazyInitializationExcludeFilter excluded} and have not already had a value
* explicitly set.
* explicitly set.
* <p>
* Note that {@link SmartInitializingSingleton SmartInitializingSingletons} are
* automatically excluded from lazy initialization to ensure that their
* {@link SmartInitializingSingleton#afterSingletonsInstantiated() callback method} is
* invoked.
*
*
* @author Andy Wilkinson
* @author Andy Wilkinson
* @author Madhura Bhave
* @author Madhura Bhave
...
@@ -42,9 +49,7 @@ public final class LazyInitializationBeanFactoryPostProcessor implements BeanFac
...
@@ -42,9 +49,7 @@ public final class LazyInitializationBeanFactoryPostProcessor implements BeanFac
@Override
@Override
public
void
postProcessBeanFactory
(
ConfigurableListableBeanFactory
beanFactory
)
throws
BeansException
{
public
void
postProcessBeanFactory
(
ConfigurableListableBeanFactory
beanFactory
)
throws
BeansException
{
// Take care not to force the eager init of factory beans when getting filters
Collection
<
LazyInitializationExcludeFilter
>
filters
=
getFilters
(
beanFactory
);
Collection
<
LazyInitializationExcludeFilter
>
filters
=
beanFactory
.
getBeansOfType
(
LazyInitializationExcludeFilter
.
class
,
false
,
false
).
values
();
for
(
String
beanName
:
beanFactory
.
getBeanDefinitionNames
())
{
for
(
String
beanName
:
beanFactory
.
getBeanDefinitionNames
())
{
BeanDefinition
beanDefinition
=
beanFactory
.
getBeanDefinition
(
beanName
);
BeanDefinition
beanDefinition
=
beanFactory
.
getBeanDefinition
(
beanName
);
if
(
beanDefinition
instanceof
AbstractBeanDefinition
)
{
if
(
beanDefinition
instanceof
AbstractBeanDefinition
)
{
...
@@ -53,6 +58,14 @@ public final class LazyInitializationBeanFactoryPostProcessor implements BeanFac
...
@@ -53,6 +58,14 @@ public final class LazyInitializationBeanFactoryPostProcessor implements BeanFac
}
}
}
}
private
Collection
<
LazyInitializationExcludeFilter
>
getFilters
(
ConfigurableListableBeanFactory
beanFactory
)
{
// Take care not to force the eager init of factory beans when getting filters
ArrayList
<
LazyInitializationExcludeFilter
>
filters
=
new
ArrayList
<>(
beanFactory
.
getBeansOfType
(
LazyInitializationExcludeFilter
.
class
,
false
,
false
).
values
());
filters
.
add
(
LazyInitializationExcludeFilter
.
forBeanTypes
(
SmartInitializingSingleton
.
class
));
return
filters
;
}
private
void
postProcess
(
ConfigurableListableBeanFactory
beanFactory
,
private
void
postProcess
(
ConfigurableListableBeanFactory
beanFactory
,
Collection
<
LazyInitializationExcludeFilter
>
filters
,
String
beanName
,
Collection
<
LazyInitializationExcludeFilter
>
filters
,
String
beanName
,
AbstractBeanDefinition
beanDefinition
)
{
AbstractBeanDefinition
beanDefinition
)
{
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/LazyInitializationBeanFactoryPostProcessorTests.java
0 → 100644
View file @
6874c42d
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.beans.factory.SmartInitializingSingleton
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
/**
* Tests for {@link LazyInitializationBeanFactoryPostProcessor}.
*
* @author Andy Wilkinson
*/
class
LazyInitializationBeanFactoryPostProcessorTests
{
@Test
void
whenLazyInitializationIsEnabledThenNormalBeansAreNotInitializedUntilRequired
()
{
try
(
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
())
{
context
.
addBeanFactoryPostProcessor
(
new
LazyInitializationBeanFactoryPostProcessor
());
context
.
register
(
BeanState
.
class
,
ExampleBean
.
class
);
context
.
refresh
();
BeanState
beanState
=
context
.
getBean
(
BeanState
.
class
);
assertThat
(
beanState
.
initializedBeans
).
isEmpty
();
context
.
getBean
(
ExampleBean
.
class
);
assertThat
(
beanState
.
initializedBeans
).
containsExactly
(
ExampleBean
.
class
);
}
}
@Test
void
whenLazyInitializationIsEnabledThenSmartInitializingSingletonsAreInitializedDuringRefresh
()
{
try
(
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
())
{
context
.
addBeanFactoryPostProcessor
(
new
LazyInitializationBeanFactoryPostProcessor
());
context
.
register
(
BeanState
.
class
,
ExampleSmartInitializingSingleton
.
class
);
context
.
refresh
();
BeanState
beanState
=
context
.
getBean
(
BeanState
.
class
);
assertThat
(
beanState
.
initializedBeans
).
containsExactly
(
ExampleSmartInitializingSingleton
.
class
);
assertThat
(
context
.
getBean
(
ExampleSmartInitializingSingleton
.
class
).
callbackInvoked
).
isTrue
();
}
}
static
class
ExampleBean
{
ExampleBean
(
BeanState
beanState
)
{
beanState
.
initializedBeans
.
add
(
getClass
());
}
}
static
class
ExampleSmartInitializingSingleton
implements
SmartInitializingSingleton
{
private
boolean
callbackInvoked
;
ExampleSmartInitializingSingleton
(
BeanState
beanState
)
{
beanState
.
initializedBeans
.
add
(
getClass
());
}
@Override
public
void
afterSingletonsInstantiated
()
{
this
.
callbackInvoked
=
true
;
}
}
static
class
BeanState
{
private
final
List
<
Class
<?>>
initializedBeans
=
new
ArrayList
<>();
}
}
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