Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
54571bf0
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,发现更多精彩内容 >>
提交
54571bf0
编写于
11月 15, 2013
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Introduced getBeanNamesForAnnotation method on ListableBeanFactory
Issue: SPR-11069
上级
b43901ed
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
78 addition
and
9 deletion
+78
-9
spring-beans/src/main/java/org/springframework/beans/factory/ListableBeanFactory.java
...rg/springframework/beans/factory/ListableBeanFactory.java
+18
-5
spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java
...ork/beans/factory/support/DefaultListableBeanFactory.java
+20
-1
spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java
...work/beans/factory/support/StaticListableBeanFactory.java
+14
-1
spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java
...mework/beans/factory/DefaultListableBeanFactoryTests.java
+9
-0
spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java
...framework/context/support/AbstractApplicationContext.java
+9
-1
spring-test/src/main/java/org/springframework/test/web/servlet/setup/StubWebApplicationContext.java
...ork/test/web/servlet/setup/StubWebApplicationContext.java
+8
-1
未找到文件。
spring-beans/src/main/java/org/springframework/beans/factory/ListableBeanFactory.java
浏览文件 @
54571bf0
...
...
@@ -212,23 +212,36 @@ public interface ListableBeanFactory extends BeanFactory {
throws
BeansException
;
/**
* Find all beans whose {@code Class} has the supplied {@link java.lang.annotation.Annotation} type.
* Find all beans whose {@code Class} has the supplied {@link Annotation} type,
* without creating any bean instances yet.
* @param annotationType the type of annotation to look for
* @return the names of all matching beans
* @since 4.0
*/
String
[]
getBeanNamesForAnnotation
(
Class
<?
extends
Annotation
>
annotationType
);
/**
* Find all beans whose {@code Class} has the supplied {@link Annotation} type,
* returning a Map of bean names with corresponding bean instances.
* @param annotationType the type of annotation to look for
* @return a Map with the matching beans, containing the bean names as
* keys and the corresponding bean instances as values
* @throws BeansException if a bean could not be created
* @since 3.0
*/
Map
<
String
,
Object
>
getBeansWithAnnotation
(
Class
<?
extends
Annotation
>
annotationType
)
throws
BeansException
;
Map
<
String
,
Object
>
getBeansWithAnnotation
(
Class
<?
extends
Annotation
>
annotationType
)
throws
BeansException
;
/**
* Find a {@link Annotation} of {@code annotationType} on the specified
* Find a
n
{@link Annotation} of {@code annotationType} on the specified
* bean, traversing its interfaces and super classes if no annotation can be
* found on the given class itself.
* @param beanName the name of the bean to look for annotations on
* @param annotationType the annotation class to look for
* @return the annotation of the given type found, or {@code null}
* @throws NoSuchBeanDefinitionException if there is no bean with the given name
* @since 3.0
*/
<
A
extends
Annotation
>
A
findAnnotationOnBean
(
String
beanName
,
Class
<
A
>
annotationType
);
<
A
extends
Annotation
>
A
findAnnotationOnBean
(
String
beanName
,
Class
<
A
>
annotationType
)
throws
NoSuchBeanDefinitionException
;
}
spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java
浏览文件 @
54571bf0
...
...
@@ -487,6 +487,23 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
return
result
;
}
@Override
public
String
[]
getBeanNamesForAnnotation
(
Class
<?
extends
Annotation
>
annotationType
)
{
List
<
String
>
results
=
new
ArrayList
<
String
>();
for
(
String
beanName
:
getBeanDefinitionNames
())
{
BeanDefinition
beanDefinition
=
getBeanDefinition
(
beanName
);
if
(!
beanDefinition
.
isAbstract
()
&&
findAnnotationOnBean
(
beanName
,
annotationType
)
!=
null
)
{
results
.
add
(
beanName
);
}
}
for
(
String
beanName
:
getSingletonNames
())
{
if
(!
results
.
contains
(
beanName
)
&&
findAnnotationOnBean
(
beanName
,
annotationType
)
!=
null
)
{
results
.
add
(
beanName
);
}
}
return
results
.
toArray
(
new
String
[
results
.
size
()]);
}
@Override
public
Map
<
String
,
Object
>
getBeansWithAnnotation
(
Class
<?
extends
Annotation
>
annotationType
)
{
Map
<
String
,
Object
>
results
=
new
LinkedHashMap
<
String
,
Object
>();
...
...
@@ -511,7 +528,9 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
* if not found on the exposed bean reference (e.g. in case of a proxy).
*/
@Override
public
<
A
extends
Annotation
>
A
findAnnotationOnBean
(
String
beanName
,
Class
<
A
>
annotationType
)
{
public
<
A
extends
Annotation
>
A
findAnnotationOnBean
(
String
beanName
,
Class
<
A
>
annotationType
)
throws
NoSuchBeanDefinitionException
{
A
ann
=
null
;
Class
<?>
beanType
=
getType
(
beanName
);
if
(
beanType
!=
null
)
{
...
...
spring-beans/src/main/java/org/springframework/beans/factory/support/StaticListableBeanFactory.java
浏览文件 @
54571bf0
...
...
@@ -276,6 +276,17 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
return
matches
;
}
@Override
public
String
[]
getBeanNamesForAnnotation
(
Class
<?
extends
Annotation
>
annotationType
)
{
List
<
String
>
results
=
new
ArrayList
<
String
>();
for
(
String
beanName
:
this
.
beans
.
keySet
())
{
if
(
findAnnotationOnBean
(
beanName
,
annotationType
)
!=
null
)
{
results
.
add
(
beanName
);
}
}
return
results
.
toArray
(
new
String
[
results
.
size
()]);
}
@Override
public
Map
<
String
,
Object
>
getBeansWithAnnotation
(
Class
<?
extends
Annotation
>
annotationType
)
throws
BeansException
{
...
...
@@ -290,7 +301,9 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
}
@Override
public
<
A
extends
Annotation
>
A
findAnnotationOnBean
(
String
beanName
,
Class
<
A
>
annotationType
)
{
public
<
A
extends
Annotation
>
A
findAnnotationOnBean
(
String
beanName
,
Class
<
A
>
annotationType
)
throws
NoSuchBeanDefinitionException
{
return
AnnotationUtils
.
findAnnotation
(
getType
(
beanName
),
annotationType
);
}
...
...
spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java
浏览文件 @
54571bf0
...
...
@@ -172,6 +172,9 @@ public class DefaultListableBeanFactoryTests {
assertTrue
(
"prototype not instantiated"
,
!
DummyFactory
.
wasPrototypeCreated
());
String
[]
beanNames
=
lbf
.
getBeanNamesForType
(
TestBean
.
class
,
true
,
false
);
assertEquals
(
0
,
beanNames
.
length
);
beanNames
=
lbf
.
getBeanNamesForAnnotation
(
SuppressWarnings
.
class
);
assertEquals
(
0
,
beanNames
.
length
);
assertFalse
(
lbf
.
containsSingleton
(
"x1"
));
assertTrue
(
lbf
.
containsBean
(
"x1"
));
assertTrue
(
lbf
.
containsBean
(
"&x1"
));
...
...
@@ -201,6 +204,9 @@ public class DefaultListableBeanFactoryTests {
assertTrue
(
"prototype not instantiated"
,
!
DummyFactory
.
wasPrototypeCreated
());
String
[]
beanNames
=
lbf
.
getBeanNamesForType
(
TestBean
.
class
,
true
,
false
);
assertEquals
(
0
,
beanNames
.
length
);
beanNames
=
lbf
.
getBeanNamesForAnnotation
(
SuppressWarnings
.
class
);
assertEquals
(
0
,
beanNames
.
length
);
assertFalse
(
lbf
.
containsSingleton
(
"x1"
));
assertTrue
(
lbf
.
containsBean
(
"x1"
));
assertTrue
(
lbf
.
containsBean
(
"&x1"
));
...
...
@@ -229,6 +235,9 @@ public class DefaultListableBeanFactoryTests {
assertTrue
(
"prototype not instantiated"
,
!
DummyFactory
.
wasPrototypeCreated
());
String
[]
beanNames
=
lbf
.
getBeanNamesForType
(
TestBean
.
class
,
true
,
false
);
assertEquals
(
0
,
beanNames
.
length
);
beanNames
=
lbf
.
getBeanNamesForAnnotation
(
SuppressWarnings
.
class
);
assertEquals
(
0
,
beanNames
.
length
);
assertFalse
(
lbf
.
containsSingleton
(
"x1"
));
assertTrue
(
lbf
.
containsBean
(
"x1"
));
assertTrue
(
lbf
.
containsBean
(
"&x1"
));
...
...
spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java
浏览文件 @
54571bf0
...
...
@@ -1071,6 +1071,12 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
return
getBeanFactory
().
getBeansOfType
(
type
,
includeNonSingletons
,
allowEagerInit
);
}
@Override
public
String
[]
getBeanNamesForAnnotation
(
Class
<?
extends
Annotation
>
annotationType
)
{
assertBeanFactoryActive
();
return
getBeanFactory
().
getBeanNamesForAnnotation
(
annotationType
);
}
@Override
public
Map
<
String
,
Object
>
getBeansWithAnnotation
(
Class
<?
extends
Annotation
>
annotationType
)
throws
BeansException
{
...
...
@@ -1080,7 +1086,9 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
}
@Override
public
<
A
extends
Annotation
>
A
findAnnotationOnBean
(
String
beanName
,
Class
<
A
>
annotationType
)
{
public
<
A
extends
Annotation
>
A
findAnnotationOnBean
(
String
beanName
,
Class
<
A
>
annotationType
)
throws
NoSuchBeanDefinitionException
{
assertBeanFactoryActive
();
return
getBeanFactory
().
findAnnotationOnBean
(
beanName
,
annotationType
);
}
...
...
spring-test/src/main/java/org/springframework/test/web/servlet/setup/StubWebApplicationContext.java
浏览文件 @
54571bf0
...
...
@@ -238,6 +238,11 @@ class StubWebApplicationContext implements WebApplicationContext {
return
this
.
beanFactory
.
getBeansOfType
(
type
,
includeNonSingletons
,
allowEagerInit
);
}
@Override
public
String
[]
getBeanNamesForAnnotation
(
Class
<?
extends
Annotation
>
annotationType
)
{
return
this
.
beanFactory
.
getBeanNamesForAnnotation
(
annotationType
);
}
@Override
public
Map
<
String
,
Object
>
getBeansWithAnnotation
(
Class
<?
extends
Annotation
>
annotationType
)
throws
BeansException
{
...
...
@@ -246,7 +251,9 @@ class StubWebApplicationContext implements WebApplicationContext {
}
@Override
public
<
A
extends
Annotation
>
A
findAnnotationOnBean
(
String
beanName
,
Class
<
A
>
annotationType
)
{
public
<
A
extends
Annotation
>
A
findAnnotationOnBean
(
String
beanName
,
Class
<
A
>
annotationType
)
throws
NoSuchBeanDefinitionException
{
return
this
.
beanFactory
.
findAnnotationOnBean
(
beanName
,
annotationType
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录