Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
1f5eaf20
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,发现更多精彩内容 >>
提交
1f5eaf20
编写于
5月 04, 2017
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Basic test for functional bean registration within a configuration class
Issue: SPR-13779
上级
de35ff11
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
45 addition
and
16 deletion
+45
-16
spring-context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassProcessingTests.java
...tion/configuration/ConfigurationClassProcessingTests.java
+45
-16
未找到文件。
spring-context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassProcessingTests.java
浏览文件 @
1f5eaf20
...
...
@@ -58,6 +58,7 @@ import org.springframework.context.annotation.Scope;
import
org.springframework.context.event.ContextRefreshedEvent
;
import
org.springframework.context.support.GenericApplicationContext
;
import
org.springframework.tests.sample.beans.ITestBean
;
import
org.springframework.tests.sample.beans.NestedTestBean
;
import
org.springframework.tests.sample.beans.TestBean
;
import
static
org
.
junit
.
Assert
.*;
...
...
@@ -206,40 +207,50 @@ public class ConfigurationClassProcessingTests {
@Test
public
void
configurationWithAdaptivePrototypes
()
{
AnnotationConfigApplicationContext
factory
=
new
AnnotationConfigApplicationContext
();
factory
.
register
(
ConfigWithPrototypeBean
.
class
,
AdaptiveInjectionPoints
.
class
);
factory
.
refresh
();
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
();
ctx
.
register
(
ConfigWithPrototypeBean
.
class
,
AdaptiveInjectionPoints
.
class
);
ctx
.
refresh
();
AdaptiveInjectionPoints
adaptive
=
factory
.
getBean
(
AdaptiveInjectionPoints
.
class
);
AdaptiveInjectionPoints
adaptive
=
ctx
.
getBean
(
AdaptiveInjectionPoints
.
class
);
assertEquals
(
"adaptiveInjectionPoint1"
,
adaptive
.
adaptiveInjectionPoint1
.
getName
());
assertEquals
(
"setAdaptiveInjectionPoint2"
,
adaptive
.
adaptiveInjectionPoint2
.
getName
());
adaptive
=
factory
.
getBean
(
AdaptiveInjectionPoints
.
class
);
adaptive
=
ctx
.
getBean
(
AdaptiveInjectionPoints
.
class
);
assertEquals
(
"adaptiveInjectionPoint1"
,
adaptive
.
adaptiveInjectionPoint1
.
getName
());
assertEquals
(
"setAdaptiveInjectionPoint2"
,
adaptive
.
adaptiveInjectionPoint2
.
getName
());
factory
.
close
();
ctx
.
close
();
}
@Test
public
void
configurationWithPostProcessor
()
{
AnnotationConfigApplicationContext
factory
=
new
AnnotationConfigApplicationContext
();
factory
.
register
(
ConfigWithPostProcessor
.
class
);
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
();
ctx
.
register
(
ConfigWithPostProcessor
.
class
);
RootBeanDefinition
placeholderConfigurer
=
new
RootBeanDefinition
(
PropertyPlaceholderConfigurer
.
class
);
placeholderConfigurer
.
getPropertyValues
().
add
(
"properties"
,
"myProp=myValue"
);
factory
.
registerBeanDefinition
(
"placeholderConfigurer"
,
placeholderConfigurer
);
factory
.
refresh
();
ctx
.
registerBeanDefinition
(
"placeholderConfigurer"
,
placeholderConfigurer
);
ctx
.
refresh
();
TestBean
foo
=
factory
.
getBean
(
"foo"
,
TestBean
.
class
);
ITestBean
bar
=
factory
.
getBean
(
"bar"
,
ITestBean
.
class
);
ITestBean
baz
=
factory
.
getBean
(
"baz"
,
ITestBean
.
class
);
TestBean
foo
=
ctx
.
getBean
(
"foo"
,
TestBean
.
class
);
ITestBean
bar
=
ctx
.
getBean
(
"bar"
,
ITestBean
.
class
);
ITestBean
baz
=
ctx
.
getBean
(
"baz"
,
ITestBean
.
class
);
assertEquals
(
"foo-processed-myValue"
,
foo
.
getName
());
assertEquals
(
"bar-processed-myValue"
,
bar
.
getName
());
assertEquals
(
"baz-processed-myValue"
,
baz
.
getName
());
SpousyTestBean
listener
=
factory
.
getBean
(
"listenerTestBean"
,
SpousyTestBean
.
class
);
SpousyTestBean
listener
=
ctx
.
getBean
(
"listenerTestBean"
,
SpousyTestBean
.
class
);
assertTrue
(
listener
.
refreshed
);
factory
.
close
();
ctx
.
close
();
}
@Test
public
void
configurationWithFunctionalRegistration
()
{
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
();
ctx
.
register
(
ConfigWithFunctionalRegistration
.
class
);
ctx
.
refresh
();
assertSame
(
ctx
.
getBean
(
"spouse"
),
ctx
.
getBean
(
TestBean
.
class
).
getSpouse
());
assertEquals
(
"functional"
,
ctx
.
getBean
(
NestedTestBean
.
class
).
getCompany
());
}
...
...
@@ -419,7 +430,6 @@ public class ConfigurationClassProcessingTests {
}
@SuppressWarnings
(
"unused"
)
static
class
ConfigWithPostProcessor
extends
ConfigWithPrototypeBean
{
@Value
(
"${myProp}"
)
...
...
@@ -496,4 +506,23 @@ public class ConfigurationClassProcessingTests {
}
}
@Configuration
static
class
ConfigWithFunctionalRegistration
{
@Autowired
void
register
(
GenericApplicationContext
ctx
)
{
ctx
.
registerBean
(
"spouse"
,
TestBean
.
class
,
()
->
new
TestBean
(
"functional"
));
ctx
.
registerBean
(
TestBean
.
class
,
()
->
new
TestBean
(
ctx
.
getBean
(
"spouse"
,
TestBean
.
class
)),
bd
->
bd
.
setPrimary
(
true
));
}
@Bean
public
NestedTestBean
nestedTestBean
(
TestBean
testBean
)
{
return
new
NestedTestBean
(
testBean
.
getSpouse
().
getName
());
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录