Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
aecf60d2
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,发现更多精彩内容 >>
提交
aecf60d2
编写于
8月 26, 2013
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Discovering and accumulating all @Profile meta-annotations
Issue: SPR-10812
上级
c9771012
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
76 addition
and
25 deletion
+76
-25
spring-context/src/main/java/org/springframework/context/annotation/ProfileCondition.java
.../springframework/context/annotation/ProfileCondition.java
+10
-4
spring-context/src/test/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProviderTests.java
...ion/ClassPathScanningCandidateComponentProviderTests.java
+66
-21
未找到文件。
spring-context/src/main/java/org/springframework/context/annotation/ProfileCondition.java
浏览文件 @
aecf60d2
...
...
@@ -16,8 +16,8 @@
package
org.springframework.context.annotation
;
import
org.springframework.core.annotation.AnnotationAttributes
;
import
org.springframework.core.type.AnnotatedTypeMetadata
;
import
org.springframework.util.MultiValueMap
;
/**
* {@link Condition} that matches based on the value of a {@link Profile @Profile}
...
...
@@ -25,15 +25,21 @@ import org.springframework.core.type.AnnotatedTypeMetadata;
*
* @author Chris Beams
* @author Phillip Webb
* @author Juergen Hoeller
* @since 4.0
*/
class
ProfileCondition
implements
Condition
{
@Override
public
boolean
matches
(
ConditionContext
context
,
AnnotatedTypeMetadata
metadata
)
{
if
(
context
.
getEnvironment
()
!=
null
&&
metadata
.
isAnnotated
(
Profile
.
class
.
getName
()))
{
AnnotationAttributes
profile
=
AnnotationAttributes
.
fromMap
(
metadata
.
getAnnotationAttributes
(
Profile
.
class
.
getName
()));
if
(!
context
.
getEnvironment
().
acceptsProfiles
(
profile
.
getStringArray
(
"value"
)))
{
if
(
context
.
getEnvironment
()
!=
null
)
{
MultiValueMap
<
String
,
Object
>
attrs
=
metadata
.
getAllAnnotationAttributes
(
Profile
.
class
.
getName
());
if
(
attrs
!=
null
)
{
for
(
Object
value
:
attrs
.
get
(
"value"
))
{
if
(
context
.
getEnvironment
().
acceptsProfiles
(((
String
[])
value
)))
{
return
true
;
}
}
return
false
;
}
}
...
...
spring-context/src/test/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProviderTests.java
浏览文件 @
aecf60d2
...
...
@@ -16,18 +16,25 @@
package
org.springframework.context.annotation
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
java.util.Iterator
;
import
java.lang.annotation.Retention
;
import
java.lang.annotation.RetentionPolicy
;
import
java.util.Set
;
import
java.util.regex.Pattern
;
import
example.profilescan.DevComponent
;
import
example.profilescan.ProfileAnnotatedComponent
;
import
example.profilescan.ProfileMetaAnnotatedComponent
;
import
example.scannable.FooDao
;
import
example.scannable.FooService
;
import
example.scannable.FooServiceImpl
;
import
example.scannable.MessageBean
;
import
example.scannable.NamedComponent
;
import
example.scannable.NamedStubDao
;
import
example.scannable.ServiceInvocationCounter
;
import
example.scannable.StubFooDao
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.junit.Test
;
import
org.springframework.beans.factory.config.BeanDefinition
;
import
org.springframework.core.env.ConfigurableEnvironment
;
import
org.springframework.core.env.StandardEnvironment
;
...
...
@@ -39,17 +46,8 @@ import org.springframework.stereotype.Controller;
import
org.springframework.stereotype.Repository
;
import
org.springframework.stereotype.Service
;
import
example.profilescan.DevComponent
;
import
example.profilescan.ProfileAnnotatedComponent
;
import
example.profilescan.ProfileMetaAnnotatedComponent
;
import
example.scannable.FooDao
;
import
example.scannable.FooService
;
import
example.scannable.FooServiceImpl
;
import
example.scannable.MessageBean
;
import
example.scannable.NamedComponent
;
import
example.scannable.NamedStubDao
;
import
example.scannable.ServiceInvocationCounter
;
import
example.scannable.StubFooDao
;
import
static
org
.
hamcrest
.
CoreMatchers
.*;
import
static
org
.
junit
.
Assert
.*;
/**
* @author Mark Fisher
...
...
@@ -276,9 +274,39 @@ public class ClassPathScanningCandidateComponentProviderTests {
}
}
@Test
public
void
testIntegrationWithAnnotationConfigApplicationContext_metaProfile
()
{
Class
<?>
beanClass
=
MetaProfileAnnotatedComponent
.
class
;
String
beanName
=
MetaProfileAnnotatedComponent
.
BEAN_NAME
;
{
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
();
ctx
.
getEnvironment
().
setDefaultProfiles
(
TEST_DEFAULT_PROFILE_NAME
);
// no active profiles are set
ctx
.
register
(
beanClass
);
ctx
.
refresh
();
assertThat
(
ctx
.
containsBean
(
beanName
),
is
(
true
));
}
{
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
();
ctx
.
getEnvironment
().
setDefaultProfiles
(
TEST_DEFAULT_PROFILE_NAME
);
ctx
.
getEnvironment
().
setActiveProfiles
(
"dev"
);
ctx
.
register
(
beanClass
);
ctx
.
refresh
();
assertThat
(
ctx
.
containsBean
(
beanName
),
is
(
true
));
}
{
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
();
ctx
.
getEnvironment
().
setDefaultProfiles
(
TEST_DEFAULT_PROFILE_NAME
);
ctx
.
getEnvironment
().
setActiveProfiles
(
"other"
);
ctx
.
register
(
beanClass
);
ctx
.
refresh
();
assertThat
(
ctx
.
containsBean
(
beanName
),
is
(
false
));
}
}
private
boolean
containsBeanClass
(
Set
<
BeanDefinition
>
candidates
,
Class
<?>
beanClass
)
{
for
(
Iterator
<
BeanDefinition
>
it
=
candidates
.
iterator
();
it
.
hasNext
();
)
{
ScannedGenericBeanDefinition
definition
=
(
ScannedGenericBeanDefinition
)
it
.
next
()
;
for
(
BeanDefinition
candidate
:
candidates
)
{
ScannedGenericBeanDefinition
definition
=
(
ScannedGenericBeanDefinition
)
candidate
;
if
(
beanClass
.
getName
().
equals
(
definition
.
getBeanClassName
()))
{
return
true
;
}
...
...
@@ -293,9 +321,26 @@ public class ClassPathScanningCandidateComponentProviderTests {
static
final
String
BEAN_NAME
=
"defaultProfileAnnotatedComponent"
;
}
@Profile
({
TEST_DEFAULT_PROFILE_NAME
,
"dev"
})
@Profile
({
TEST_DEFAULT_PROFILE_NAME
,
"dev"
})
@Component
(
DefaultAndDevProfileAnnotatedComponent
.
BEAN_NAME
)
private
static
class
DefaultAndDevProfileAnnotatedComponent
{
static
final
String
BEAN_NAME
=
"defaultAndDevProfileAnnotatedComponent"
;
}
@DefaultProfile
@DevProfile
@Component
(
MetaProfileAnnotatedComponent
.
BEAN_NAME
)
private
static
class
MetaProfileAnnotatedComponent
{
static
final
String
BEAN_NAME
=
"metaProfileAnnotatedComponent"
;
}
@Profile
(
TEST_DEFAULT_PROFILE_NAME
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
public
@interface
DefaultProfile
{
}
@Profile
(
"dev"
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
public
@interface
DevProfile
{
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录