Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
3ee6286e
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,发现更多精彩内容 >>
提交
3ee6286e
编写于
12月 18, 2016
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Support for functional instance supplier callback at BeanDefinition level
Issue: SPR-14832
上级
12aa14dd
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
96 addition
and
1 deletion
+96
-1
spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java
...s/factory/support/AbstractAutowireCapableBeanFactory.java
+8
-0
spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java
...amework/beans/factory/support/AbstractBeanDefinition.java
+27
-0
spring-beans/src/main/java/org/springframework/beans/factory/support/RootBeanDefinition.java
...ngframework/beans/factory/support/RootBeanDefinition.java
+34
-0
spring-context/src/test/java/org/springframework/context/support/GenericApplicationContextTests.java
...ework/context/support/GenericApplicationContextTests.java
+27
-1
未找到文件。
spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java
浏览文件 @
3ee6286e
...
...
@@ -38,6 +38,7 @@ import java.util.Set;
import
java.util.TreeSet
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ConcurrentMap
;
import
java.util.function.Supplier
;
import
org.springframework.beans.BeanUtils
;
import
org.springframework.beans.BeanWrapper
;
...
...
@@ -1036,6 +1037,13 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
"Bean class isn't public, and non-public access not allowed: "
+
beanClass
.
getName
());
}
Supplier
<?>
instanceSupplier
=
mbd
.
getInstanceSupplier
();
if
(
instanceSupplier
!=
null
)
{
BeanWrapper
bw
=
new
BeanWrapperImpl
(
instanceSupplier
.
get
());
initBeanWrapper
(
bw
);
return
bw
;
}
if
(
mbd
.
getFactoryMethodName
()
!=
null
)
{
return
instantiateUsingFactoryMethod
(
beanName
,
mbd
,
args
);
}
...
...
spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java
浏览文件 @
3ee6286e
...
...
@@ -22,6 +22,7 @@ import java.util.LinkedHashMap;
import
java.util.LinkedHashSet
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.function.Supplier
;
import
org.springframework.beans.BeanMetadataAttributeAccessor
;
import
org.springframework.beans.MutablePropertyValues
;
...
...
@@ -165,6 +166,8 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
private
MethodOverrides
methodOverrides
=
new
MethodOverrides
();
private
Supplier
<?>
instanceSupplier
;
private
String
factoryBeanName
;
private
String
factoryMethodName
;
...
...
@@ -234,6 +237,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
setPrimary
(
originalAbd
.
isPrimary
());
setNonPublicAccessAllowed
(
originalAbd
.
isNonPublicAccessAllowed
());
setLenientConstructorResolution
(
originalAbd
.
isLenientConstructorResolution
());
setInstanceSupplier
(
originalAbd
.
getInstanceSupplier
());
setInitMethodName
(
originalAbd
.
getInitMethodName
());
setEnforceInitMethod
(
originalAbd
.
isEnforceInitMethod
());
setDestroyMethodName
(
originalAbd
.
getDestroyMethodName
());
...
...
@@ -298,6 +302,7 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
setDependsOn
(
otherAbd
.
getDependsOn
());
setNonPublicAccessAllowed
(
otherAbd
.
isNonPublicAccessAllowed
());
setLenientConstructorResolution
(
otherAbd
.
isLenientConstructorResolution
());
setInstanceSupplier
(
otherAbd
.
getInstanceSupplier
());
if
(
StringUtils
.
hasLength
(
otherAbd
.
getInitMethodName
()))
{
setInitMethodName
(
otherAbd
.
getInitMethodName
());
setEnforceInitMethod
(
otherAbd
.
isEnforceInitMethod
());
...
...
@@ -738,6 +743,28 @@ public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccess
}
/**
* Specify a callback for creating an instance of the bean,
* as an alternative to a declaratively specified factory method.
* <p>If such a callback is set, it will override any other constructor
* or factory method metadata. However, bean property population and
* potential annotation-driven injection will still apply as usual.
* @since 5.0
* @see #setConstructorArgumentValues(ConstructorArgumentValues)
* @see #setPropertyValues(MutablePropertyValues)
*/
public
void
setInstanceSupplier
(
Supplier
<?>
instanceSupplier
)
{
this
.
instanceSupplier
=
instanceSupplier
;
}
/**
* Return a callback for creating an instance of the bean, if any.
* @since 5.0
*/
public
Supplier
<?>
getInstanceSupplier
()
{
return
this
.
instanceSupplier
;
}
@Override
public
void
setFactoryBeanName
(
String
factoryBeanName
)
{
this
.
factoryBeanName
=
factoryBeanName
;
...
...
spring-beans/src/main/java/org/springframework/beans/factory/support/RootBeanDefinition.java
浏览文件 @
3ee6286e
...
...
@@ -22,6 +22,7 @@ import java.lang.reflect.Member;
import
java.lang.reflect.Method
;
import
java.util.HashSet
;
import
java.util.Set
;
import
java.util.function.Supplier
;
import
org.springframework.beans.MutablePropertyValues
;
import
org.springframework.beans.factory.config.BeanDefinition
;
...
...
@@ -116,12 +117,45 @@ public class RootBeanDefinition extends AbstractBeanDefinition {
/**
* Create a new RootBeanDefinition for a singleton.
* @param beanClass the class of the bean to instantiate
* @see #setBeanClass
*/
public
RootBeanDefinition
(
Class
<?>
beanClass
)
{
super
();
setBeanClass
(
beanClass
);
}
/**
* Create a new RootBeanDefinition for a singleton bean, constructing each instance
* through calling the given supplier (possibly a lambda or method reference).
* @param beanClass the class of the bean to instantiate
* @param instanceSupplier the supplier to construct a bean instance,
* as an alternative to a declaratively specified factory method
* @since 5.0
* @see #setInstanceSupplier(Supplier)
*/
public
<
T
>
RootBeanDefinition
(
Class
<
T
>
beanClass
,
Supplier
<
T
>
instanceSupplier
)
{
super
();
setBeanClass
(
beanClass
);
setInstanceSupplier
(
instanceSupplier
);
}
/**
* Create a new RootBeanDefinition for a scoped bean, constructing each instance
* through calling the given supplier (possibly a lambda or method reference).
* @param beanClass the class of the bean to instantiate
* @param scope the name of the corresponding scope
* @param instanceSupplier the supplier to construct a bean instance,
* as an alternative to a declaratively specified factory method
* @since 5.0
* @see #setInstanceSupplier(Supplier)
*/
public
<
T
>
RootBeanDefinition
(
Class
<
T
>
beanClass
,
String
scope
,
Supplier
<
T
>
instanceSupplier
)
{
super
();
setBeanClass
(
beanClass
);
setScope
(
scope
);
setInstanceSupplier
(
instanceSupplier
);
}
/**
* Create a new RootBeanDefinition for a singleton,
* using the given autowire mode.
...
...
spring-context/src/test/java/org/springframework/context/support/GenericApplicationContextTests.java
浏览文件 @
3ee6286e
/*
* Copyright 2002-201
5
the original author or authors.
* Copyright 2002-201
6
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.
...
...
@@ -43,6 +43,7 @@ public class GenericApplicationContextTests {
ac
.
registerBeanDefinition
(
"testBean"
,
new
RootBeanDefinition
(
String
.
class
));
ac
.
refresh
();
assertEquals
(
""
,
ac
.
getBean
(
"testBean"
));
assertSame
(
ac
.
getBean
(
"testBean"
),
ac
.
getBean
(
String
.
class
));
assertSame
(
ac
.
getBean
(
"testBean"
),
ac
.
getBean
(
CharSequence
.
class
));
...
...
@@ -55,6 +56,31 @@ public class GenericApplicationContextTests {
}
}
@Test
public
void
withSingletonSupplier
()
{
GenericApplicationContext
ac
=
new
GenericApplicationContext
();
ac
.
registerBeanDefinition
(
"testBean"
,
new
RootBeanDefinition
(
String
.
class
,
ac:
:
toString
));
ac
.
refresh
();
assertSame
(
ac
.
getBean
(
"testBean"
),
ac
.
getBean
(
"testBean"
));
assertSame
(
ac
.
getBean
(
"testBean"
),
ac
.
getBean
(
String
.
class
));
assertSame
(
ac
.
getBean
(
"testBean"
),
ac
.
getBean
(
CharSequence
.
class
));
assertEquals
(
ac
.
toString
(),
ac
.
getBean
(
"testBean"
));
}
@Test
public
void
withScopedSupplier
()
{
GenericApplicationContext
ac
=
new
GenericApplicationContext
();
ac
.
registerBeanDefinition
(
"testBean"
,
new
RootBeanDefinition
(
String
.
class
,
RootBeanDefinition
.
SCOPE_PROTOTYPE
,
ac:
:
toString
));
ac
.
refresh
();
assertNotSame
(
ac
.
getBean
(
"testBean"
),
ac
.
getBean
(
"testBean"
));
assertEquals
(
ac
.
getBean
(
"testBean"
),
ac
.
getBean
(
String
.
class
));
assertEquals
(
ac
.
getBean
(
"testBean"
),
ac
.
getBean
(
CharSequence
.
class
));
assertEquals
(
ac
.
toString
(),
ac
.
getBean
(
"testBean"
));
}
@Test
public
void
accessAfterClosing
()
{
GenericApplicationContext
ac
=
new
GenericApplicationContext
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录