Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
a95843a0
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a95843a0
编写于
4月 07, 2017
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Dependency tracking for Supplier-created beans
Issue: SPR-15417
上级
ea5cb26d
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
66 addition
and
8 deletion
+66
-8
spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java
...s/factory/support/AbstractAutowireCapableBeanFactory.java
+60
-7
spring-context/src/test/java/org/springframework/context/annotation/AnnotationConfigApplicationContextTests.java
...t/annotation/AnnotationConfigApplicationContextTests.java
+6
-1
未找到文件。
spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java
浏览文件 @
a95843a0
...
...
@@ -73,6 +73,7 @@ import org.springframework.beans.factory.config.TypedStringValue;
import
org.springframework.core.DefaultParameterNameDiscoverer
;
import
org.springframework.core.GenericTypeResolver
;
import
org.springframework.core.MethodParameter
;
import
org.springframework.core.NamedThreadLocal
;
import
org.springframework.core.ParameterNameDiscoverer
;
import
org.springframework.core.PriorityOrdered
;
import
org.springframework.core.ResolvableType
;
...
...
@@ -146,6 +147,12 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
*/
private
final
Set
<
Class
<?>>
ignoredDependencyInterfaces
=
new
HashSet
<>();
/**
* The name of the currently created bean, for implicit dependency registration
* on getBean etc invocations triggered from a user-specified Supplier callback.
*/
private
final
NamedThreadLocal
<
String
>
currentlyCreatedBean
=
new
NamedThreadLocal
<>(
"Currently created bean"
);
/** Cache of unfinished FactoryBean instances: FactoryBean name --> BeanWrapper */
private
final
Map
<
String
,
BeanWrapper
>
factoryBeanInstanceCache
=
new
ConcurrentHashMap
<>(
16
);
...
...
@@ -1062,7 +1069,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
* @param beanName the name of the bean
* @param mbd the bean definition for the bean
* @param args explicit arguments to use for constructor or factory method invocation
* @return BeanWrapper for the new instance
* @return a BeanWrapper for the new instance
* @see #obtainFromSupplier
* @see #instantiateUsingFactoryMethod
* @see #autowireConstructor
* @see #instantiateBean
...
...
@@ -1078,9 +1086,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
Supplier
<?>
instanceSupplier
=
mbd
.
getInstanceSupplier
();
if
(
instanceSupplier
!=
null
)
{
BeanWrapper
bw
=
new
BeanWrapperImpl
(
instanceSupplier
.
get
());
initBeanWrapper
(
bw
);
return
bw
;
return
obtainFromSupplier
(
instanceSupplier
,
beanName
);
}
if
(
mbd
.
getFactoryMethodName
()
!=
null
)
{
...
...
@@ -1119,6 +1125,53 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
return
instantiateBean
(
beanName
,
mbd
);
}
/**
* Obtain a bean instance from the given supplier.
* @param instanceSupplier the configured supplier
* @param beanName the corresponding bean name
* @return a BeanWrapper for the new instance
* @since 5.0
* @see #getObjectForBeanInstance
*/
protected
BeanWrapper
obtainFromSupplier
(
Supplier
<?>
instanceSupplier
,
String
beanName
)
{
String
outerBean
=
this
.
currentlyCreatedBean
.
get
();
this
.
currentlyCreatedBean
.
set
(
beanName
);
Object
instance
;
try
{
instance
=
instanceSupplier
.
get
();
}
finally
{
if
(
outerBean
!=
null
)
{
this
.
currentlyCreatedBean
.
set
(
outerBean
);
}
else
{
this
.
currentlyCreatedBean
.
remove
();
}
}
BeanWrapper
bw
=
new
BeanWrapperImpl
(
instance
);
initBeanWrapper
(
bw
);
return
bw
;
}
/**
* Overridden in order to implicitly register the currently created bean as
* dependent on further beans getting programmatically retrieved during a
* {@link Supplier} callback.
* @since 5.0
* @see #obtainFromSupplier
*/
@Override
protected
Object
getObjectForBeanInstance
(
Object
beanInstance
,
String
name
,
String
beanName
,
RootBeanDefinition
mbd
)
{
String
currentlyCreatedBean
=
this
.
currentlyCreatedBean
.
get
();
if
(
currentlyCreatedBean
!=
null
)
{
registerDependentBean
(
beanName
,
currentlyCreatedBean
);
}
return
super
.
getObjectForBeanInstance
(
beanInstance
,
name
,
beanName
,
mbd
);
}
/**
* Determine candidate constructors to use for the given bean, checking all registered
* {@link SmartInstantiationAwareBeanPostProcessor SmartInstantiationAwareBeanPostProcessors}.
...
...
@@ -1149,7 +1202,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
* Instantiate the given bean using its default constructor.
* @param beanName the name of the bean
* @param mbd the bean definition for the bean
* @return BeanWrapper for the new instance
* @return
a
BeanWrapper for the new instance
*/
protected
BeanWrapper
instantiateBean
(
final
String
beanName
,
final
RootBeanDefinition
mbd
)
{
try
{
...
...
@@ -1184,7 +1237,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
* @param mbd the bean definition for the bean
* @param explicitArgs argument values passed in programmatically via the getBean method,
* or {@code null} if none (-> use constructor argument values from bean definition)
* @return BeanWrapper for the new instance
* @return
a
BeanWrapper for the new instance
* @see #getBean(String, Object[])
*/
protected
BeanWrapper
instantiateUsingFactoryMethod
(
...
...
@@ -1205,7 +1258,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
* @param ctors the chosen candidate constructors
* @param explicitArgs argument values passed in programmatically via the getBean method,
* or {@code null} if none (-> use constructor argument values from bean definition)
* @return BeanWrapper for the new instance
* @return
a
BeanWrapper for the new instance
*/
protected
BeanWrapper
autowireConstructor
(
String
beanName
,
RootBeanDefinition
mbd
,
Constructor
<?>[]
ctors
,
Object
[]
explicitArgs
)
{
...
...
spring-context/src/test/java/org/springframework/context/annotation/AnnotationConfigApplicationContextTests.java
浏览文件 @
a95843a0
/*
* Copyright 2002-201
6
the original author or authors.
* Copyright 2002-201
7
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.
...
...
@@ -156,6 +156,11 @@ public class AnnotationConfigApplicationContextTests {
assertSame
(
context
.
getBean
(
BeanB
.
class
),
context
.
getBean
(
BeanA
.
class
).
b
);
assertSame
(
context
.
getBean
(
BeanC
.
class
),
context
.
getBean
(
BeanA
.
class
).
c
);
assertSame
(
context
,
context
.
getBean
(
BeanB
.
class
).
applicationContext
);
assertArrayEquals
(
new
String
[]
{
"annotationConfigApplicationContextTests.BeanA"
},
context
.
getDefaultListableBeanFactory
().
getDependentBeans
(
"annotationConfigApplicationContextTests.BeanB"
));
assertArrayEquals
(
new
String
[]
{
"annotationConfigApplicationContextTests.BeanA"
},
context
.
getDefaultListableBeanFactory
().
getDependentBeans
(
"annotationConfigApplicationContextTests.BeanC"
));
}
@Test
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录