Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
981aefc2
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,体验更适合开发者的 AI 搜索 >>
提交
981aefc2
编写于
2月 10, 2015
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Properly honor conditions on overriding bean methods
Issue: SPR-12694
上级
918bc3b1
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
73 addition
and
19 deletion
+73
-19
spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClass.java
...pringframework/context/annotation/ConfigurationClass.java
+4
-1
spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java
...xt/annotation/ConfigurationClassBeanDefinitionReader.java
+25
-17
spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassWithConditionTests.java
...text/annotation/ConfigurationClassWithConditionTests.java
+44
-1
未找到文件。
spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClass.java
浏览文件 @
981aefc2
/*
* Copyright 2002-201
4
the original author or authors.
* Copyright 2002-201
5
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.
...
...
@@ -16,6 +16,7 @@
package
org.springframework.context.annotation
;
import
java.util.HashSet
;
import
java.util.LinkedHashMap
;
import
java.util.LinkedHashSet
;
import
java.util.Map
;
...
...
@@ -63,6 +64,8 @@ final class ConfigurationClass {
private
final
Map
<
ImportBeanDefinitionRegistrar
,
AnnotationMetadata
>
importBeanDefinitionRegistrars
=
new
LinkedHashMap
<
ImportBeanDefinitionRegistrar
,
AnnotationMetadata
>();
final
Set
<
String
>
skippedBeans
=
new
HashSet
<
String
>();
/**
* Create a new {@link ConfigurationClass} with the given name.
...
...
spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java
浏览文件 @
981aefc2
/*
* Copyright 2002-201
4
the original author or authors.
* Copyright 2002-201
5
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.
...
...
@@ -182,16 +182,37 @@ class ConfigurationClassBeanDefinitionReader {
* with the BeanDefinitionRegistry based on its contents.
*/
private
void
loadBeanDefinitionsForBeanMethod
(
BeanMethod
beanMethod
)
{
if
(
this
.
conditionEvaluator
.
shouldSkip
(
beanMethod
.
getMetadata
(),
ConfigurationPhase
.
REGISTER_BEAN
))
{
ConfigurationClass
configClass
=
beanMethod
.
getConfigurationClass
();
MethodMetadata
metadata
=
beanMethod
.
getMetadata
();
// Consider name and any aliases
AnnotationAttributes
bean
=
AnnotationConfigUtils
.
attributesFor
(
metadata
,
Bean
.
class
);
List
<
String
>
names
=
new
ArrayList
<
String
>(
Arrays
.
asList
(
bean
.
getStringArray
(
"name"
)));
String
beanName
=
(
names
.
size
()
>
0
?
names
.
remove
(
0
)
:
beanMethod
.
getMetadata
().
getMethodName
());
// Do we need to mark the bean as skipped by its condition?
if
(
this
.
conditionEvaluator
.
shouldSkip
(
metadata
,
ConfigurationPhase
.
REGISTER_BEAN
))
{
configClass
.
skippedBeans
.
add
(
beanName
);
return
;
}
if
(
configClass
.
skippedBeans
.
contains
(
beanName
))
{
return
;
}
ConfigurationClass
configClass
=
beanMethod
.
getConfigurationClass
();
MethodMetadata
metadata
=
beanMethod
.
getMetadata
();
// Register aliases even when overridden
for
(
String
alias
:
names
)
{
this
.
registry
.
registerAlias
(
beanName
,
alias
);
}
// Has this effectively been overridden before (e.g. via XML)?
if
(
isOverriddenByExistingDefinition
(
beanMethod
,
beanName
))
{
return
;
}
ConfigurationClassBeanDefinition
beanDef
=
new
ConfigurationClassBeanDefinition
(
configClass
,
metadata
);
beanDef
.
setResource
(
configClass
.
getResource
());
beanDef
.
setSource
(
this
.
sourceExtractor
.
extractSource
(
metadata
,
configClass
.
getResource
()));
if
(
metadata
.
isStatic
())
{
// static @Bean method
beanDef
.
setBeanClassName
(
configClass
.
getMetadata
().
getClassName
());
...
...
@@ -205,19 +226,6 @@ class ConfigurationClassBeanDefinitionReader {
beanDef
.
setAutowireMode
(
RootBeanDefinition
.
AUTOWIRE_CONSTRUCTOR
);
beanDef
.
setAttribute
(
RequiredAnnotationBeanPostProcessor
.
SKIP_REQUIRED_CHECK_ATTRIBUTE
,
Boolean
.
TRUE
);
// Consider name and any aliases
AnnotationAttributes
bean
=
AnnotationConfigUtils
.
attributesFor
(
metadata
,
Bean
.
class
);
List
<
String
>
names
=
new
ArrayList
<
String
>(
Arrays
.
asList
(
bean
.
getStringArray
(
"name"
)));
String
beanName
=
(
names
.
size
()
>
0
?
names
.
remove
(
0
)
:
beanMethod
.
getMetadata
().
getMethodName
());
for
(
String
alias
:
names
)
{
this
.
registry
.
registerAlias
(
beanName
,
alias
);
}
// Has this effectively been overridden before (e.g. via XML)?
if
(
isOverriddenByExistingDefinition
(
beanMethod
,
beanName
))
{
return
;
}
AnnotationConfigUtils
.
processCommonDefinitionAnnotations
(
beanDef
,
metadata
);
Autowire
autowire
=
bean
.
getEnum
(
"autowire"
);
...
...
spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassWithConditionTests.java
浏览文件 @
981aefc2
/*
* Copyright 2002-201
3
the original author or authors.
* Copyright 2002-201
5
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.
...
...
@@ -39,6 +39,7 @@ import static org.junit.Assert.*;
* Test for {@link Conditional} beans.
*
* @author Phillip Webb
* @author Juergen Hoeller
*/
@SuppressWarnings
(
"resource"
)
public
class
ConfigurationClassWithConditionTests
{
...
...
@@ -117,6 +118,19 @@ public class ConfigurationClassWithConditionTests {
ctx
.
refresh
();
}
@Test
public
void
conditionOnOverriddenMethodHonored
()
{
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
(
ConfigWithBeanSkipped
.
class
);
assertEquals
(
0
,
context
.
getBeansOfType
(
ExampleBean
.
class
).
size
());
}
@Test
public
void
noConditionOnOverriddenMethodHonored
()
{
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
(
ConfigWithBeanReactivated
.
class
);
assertEquals
(
1
,
context
.
getBeansOfType
(
ExampleBean
.
class
).
size
());
}
@Configuration
static
class
BeanOneConfiguration
{
@Bean
...
...
@@ -197,6 +211,7 @@ public class ConfigurationClassWithConditionTests {
}
static
class
NeverCondition
implements
Condition
{
@Override
public
boolean
matches
(
ConditionContext
context
,
AnnotatedTypeMetadata
metadata
)
{
return
false
;
...
...
@@ -261,4 +276,32 @@ public class ConfigurationClassWithConditionTests {
static
class
ExampleBean
{
}
@Configuration
private
static
class
ConfigWithBeanActive
{
@Bean
public
ExampleBean
baz
()
{
return
new
ExampleBean
();
}
}
private
static
class
ConfigWithBeanSkipped
extends
ConfigWithBeanActive
{
@Override
@Bean
@Conditional
(
NeverCondition
.
class
)
public
ExampleBean
baz
()
{
return
new
ExampleBean
();
}
}
private
static
class
ConfigWithBeanReactivated
extends
ConfigWithBeanSkipped
{
@Override
@Bean
public
ExampleBean
baz
()
{
return
new
ExampleBean
();
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录