Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
hk581
spring-framework
提交
18f2e6a1
S
spring-framework
项目概览
hk581
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
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 搜索 >>
提交
18f2e6a1
编写于
3月 05, 2019
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
AnnotationConfigRegistry exposes registerBean with supplier/qualifiers
Closes gh-22457
上级
eeed20d8
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
297 addition
and
18 deletion
+297
-18
spring-context/src/main/java/org/springframework/context/annotation/AnnotatedBeanDefinitionReader.java
...ork/context/annotation/AnnotatedBeanDefinitionReader.java
+38
-1
spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigApplicationContext.java
...ontext/annotation/AnnotationConfigApplicationContext.java
+71
-7
spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigRegistry.java
...ramework/context/annotation/AnnotationConfigRegistry.java
+32
-1
spring-web/src/main/java/org/springframework/web/context/support/AnnotationConfigWebApplicationContext.java
...ontext/support/AnnotationConfigWebApplicationContext.java
+106
-8
spring-web/src/test/java/org/springframework/web/context/support/AnnotationConfigWebApplicationContextTests.java
...t/support/AnnotationConfigWebApplicationContextTests.java
+50
-1
未找到文件。
spring-context/src/main/java/org/springframework/context/annotation/AnnotatedBeanDefinitionReader.java
浏览文件 @
18f2e6a1
/*
* Copyright 2002-201
7
the original author or authors.
* Copyright 2002-201
9
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.
...
...
@@ -197,6 +197,43 @@ public class AnnotatedBeanDefinitionReader {
doRegisterBean
(
annotatedClass
,
null
,
name
,
qualifiers
);
}
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations, using the given supplier for obtaining a new
* instance (possibly declared as a lambda expression or method reference).
* @param annotatedClass the class of the bean
* @param instanceSupplier a callback for creating an instance of the bean
* (may be {@code null})
* @param qualifiers specific qualifier annotations to consider,
* in addition to qualifiers at the bean class level
* @since 5.2
*/
@SuppressWarnings
(
"unchecked"
)
public
<
T
>
void
registerBean
(
Class
<
T
>
annotatedClass
,
@Nullable
Supplier
<
T
>
instanceSupplier
,
Class
<?
extends
Annotation
>...
qualifiers
)
{
doRegisterBean
(
annotatedClass
,
instanceSupplier
,
null
,
qualifiers
);
}
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations, using the given supplier for obtaining a new
* instance (possibly declared as a lambda expression or method reference).
* @param annotatedClass the class of the bean
* @param name an explicit name for the bean
* @param instanceSupplier a callback for creating an instance of the bean
* (may be {@code null})
* @param qualifiers specific qualifier annotations to consider,
* in addition to qualifiers at the bean class level
* @since 5.2
*/
@SuppressWarnings
(
"unchecked"
)
public
<
T
>
void
registerBean
(
Class
<
T
>
annotatedClass
,
String
name
,
@Nullable
Supplier
<
T
>
instanceSupplier
,
Class
<?
extends
Annotation
>...
qualifiers
)
{
doRegisterBean
(
annotatedClass
,
instanceSupplier
,
name
,
qualifiers
);
}
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations.
...
...
spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigApplicationContext.java
浏览文件 @
18f2e6a1
/*
* Copyright 2002-201
8
the original author or authors.
* Copyright 2002-201
9
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.lang.annotation.Annotation
;
import
java.util.function.Supplier
;
import
org.springframework.beans.factory.config.BeanDefinitionCustomizer
;
...
...
@@ -152,7 +153,8 @@ public class AnnotationConfigApplicationContext extends GenericApplicationContex
* @see #scan(String...)
* @see #refresh()
*/
public
void
register
(
Class
<?>...
annotatedClasses
)
{
@Override
public
final
void
register
(
Class
<?>...
annotatedClasses
)
{
Assert
.
notEmpty
(
annotatedClasses
,
"At least one annotated class must be specified"
);
this
.
reader
.
register
(
annotatedClasses
);
}
...
...
@@ -165,7 +167,8 @@ public class AnnotationConfigApplicationContext extends GenericApplicationContex
* @see #register(Class...)
* @see #refresh()
*/
public
void
scan
(
String
...
basePackages
)
{
@Override
public
final
void
scan
(
String
...
basePackages
)
{
Assert
.
notEmpty
(
basePackages
,
"At least one base package must be specified"
);
this
.
scanner
.
scan
(
basePackages
);
}
...
...
@@ -175,6 +178,65 @@ public class AnnotationConfigApplicationContext extends GenericApplicationContex
// Convenient methods for registering individual beans
//---------------------------------------------------------------------
/**
* Register a bean from the given bean class.
* @param annotatedClass the class of the bean
* @since 5.2
*/
public
final
<
T
>
void
registerBean
(
Class
<
T
>
annotatedClass
)
{
this
.
reader
.
doRegisterBean
(
annotatedClass
,
null
,
null
,
null
);
}
/**
* Register a bean from the given bean class, using the given supplier for
* obtaining a new instance (typically declared as a lambda expression or
* method reference).
* @param annotatedClass the class of the bean
* @param supplier a callback for creating an instance of the bean
* @since 5.2
*/
public
final
<
T
>
void
registerBean
(
Class
<
T
>
annotatedClass
,
Supplier
<
T
>
supplier
)
{
this
.
reader
.
doRegisterBean
(
annotatedClass
,
supplier
,
null
,
null
);
}
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations.
* @param annotatedClass the class of the bean
* @param qualifiers specific qualifier annotations to consider,
* in addition to qualifiers at the bean class level (may be empty).
* These can be actual autowire qualifiers as well as {@link Primary}
* and {@link Lazy}.
* @since 5.2
*/
@Override
@SafeVarargs
@SuppressWarnings
(
"varargs"
)
public
final
<
T
>
void
registerBean
(
Class
<
T
>
annotatedClass
,
Class
<?
extends
Annotation
>...
qualifiers
)
{
this
.
reader
.
doRegisterBean
(
annotatedClass
,
null
,
null
,
qualifiers
);
}
/**
* Register a bean from the given bean class, using the given supplier for
* obtaining a new instance (typically declared as a lambda expression or
* method reference).
* @param annotatedClass the class of the bean
* @param supplier a callback for creating an instance of the bean
* @param qualifiers specific qualifier annotations to consider,
* in addition to qualifiers at the bean class level (may be empty).
* These can be actual autowire qualifiers as well as {@link Primary}
* and {@link Lazy}.
* @since 5.2
*/
@Override
@SafeVarargs
@SuppressWarnings
(
"varargs"
)
public
final
<
T
>
void
registerBean
(
Class
<
T
>
annotatedClass
,
Supplier
<
T
>
supplier
,
Class
<?
extends
Annotation
>...
qualifiers
)
{
this
.
reader
.
doRegisterBean
(
annotatedClass
,
supplier
,
null
,
qualifiers
);
}
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations, and optionally providing explicit constructor
...
...
@@ -187,7 +249,7 @@ public class AnnotationConfigApplicationContext extends GenericApplicationContex
* (may be {@code null} or empty)
* @since 5.0
*/
public
<
T
>
void
registerBean
(
Class
<
T
>
annotatedClass
,
Object
...
constructorArguments
)
{
public
final
<
T
>
void
registerBean
(
Class
<
T
>
annotatedClass
,
Object
...
constructorArguments
)
{
registerBean
(
null
,
annotatedClass
,
constructorArguments
);
}
...
...
@@ -203,7 +265,9 @@ public class AnnotationConfigApplicationContext extends GenericApplicationContex
* (may be {@code null} or empty)
* @since 5.0
*/
public
<
T
>
void
registerBean
(
@Nullable
String
beanName
,
Class
<
T
>
annotatedClass
,
Object
...
constructorArguments
)
{
public
final
<
T
>
void
registerBean
(
@Nullable
String
beanName
,
Class
<
T
>
annotatedClass
,
Object
...
constructorArguments
)
{
this
.
reader
.
doRegisterBean
(
annotatedClass
,
null
,
beanName
,
null
,
bd
->
{
for
(
Object
arg
:
constructorArguments
)
{
...
...
@@ -213,8 +277,8 @@ public class AnnotationConfigApplicationContext extends GenericApplicationContex
}
@Override
public
<
T
>
void
registerBean
(
@Nullable
String
beanName
,
Class
<
T
>
beanClass
,
@Nullable
Supplier
<
T
>
supplier
,
BeanDefinitionCustomizer
...
customizers
)
{
public
final
<
T
>
void
registerBean
(
@Nullable
String
beanName
,
Class
<
T
>
beanClass
,
@Nullable
Supplier
<
T
>
supplier
,
BeanDefinitionCustomizer
...
customizers
)
{
this
.
reader
.
doRegisterBean
(
beanClass
,
supplier
,
beanName
,
null
,
customizers
);
}
...
...
spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigRegistry.java
浏览文件 @
18f2e6a1
/*
* Copyright 2002-201
4
the original author or authors.
* Copyright 2002-201
9
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,9 @@
package
org.springframework.context.annotation
;
import
java.lang.annotation.Annotation
;
import
java.util.function.Supplier
;
/**
* Common interface for annotation config application contexts,
* defining {@link #register} and {@link #scan} methods.
...
...
@@ -40,4 +43,32 @@ public interface AnnotationConfigRegistry {
*/
void
scan
(
String
...
basePackages
);
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations.
* @param annotatedClass the class of the bean
* @param qualifiers specific qualifier annotations to consider,
* in addition to qualifiers at the bean class level (may be empty).
* These can be actual autowire qualifiers as well as {@link Primary}
* and {@link Lazy}.
* @since 5.2
*/
@SuppressWarnings
(
"unchecked"
)
<
T
>
void
registerBean
(
Class
<
T
>
annotatedClass
,
Class
<?
extends
Annotation
>...
qualifiers
);
/**
* Register a bean from the given bean class, using the given supplier for
* obtaining a new instance (typically declared as a lambda expression or
* method reference).
* @param annotatedClass the class of the bean
* @param supplier a callback for creating an instance of the bean
* @param qualifiers specific qualifier annotations to consider,
* in addition to qualifiers at the bean class level (may be empty).
* These can be actual autowire qualifiers as well as {@link Primary}
* and {@link Lazy}.
* @since 5.2
*/
@SuppressWarnings
(
"unchecked"
)
<
T
>
void
registerBean
(
Class
<
T
>
annotatedClass
,
Supplier
<
T
>
supplier
,
Class
<?
extends
Annotation
>...
qualifiers
);
}
spring-web/src/main/java/org/springframework/web/context/support/AnnotationConfigWebApplicationContext.java
浏览文件 @
18f2e6a1
/*
* Copyright 2002-201
8
the original author or authors.
* Copyright 2002-201
9
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,9 +16,11 @@
package
org.springframework.web.context.support
;
import
java.lang.annotation.Annotation
;
import
java.util.Collections
;
import
java.util.LinkedHashSet
;
import
java.util.Set
;
import
java.util.function.Supplier
;
import
org.springframework.beans.factory.support.BeanNameGenerator
;
import
org.springframework.beans.factory.support.DefaultListableBeanFactory
;
...
...
@@ -94,6 +96,8 @@ public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWe
private
final
Set
<
String
>
basePackages
=
new
LinkedHashSet
<>();
private
final
Set
<
BeanRegistration
>
registeredBeans
=
new
LinkedHashSet
<>();
/**
* Set a custom {@link BeanNameGenerator} for use with {@link AnnotatedBeanDefinitionReader}
...
...
@@ -140,14 +144,14 @@ public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWe
* Register one or more annotated classes to be processed.
* <p>Note that {@link #refresh()} must be called in order for the context
* to fully process the new classes.
* @param annotatedClasses one or more annotated classes,
*
e.g.
{@link org.springframework.context.annotation.Configuration @Configuration} classes
* @param annotatedClasses one or more annotated classes,
e.g.
* {@link org.springframework.context.annotation.Configuration @Configuration} classes
* @see #scan(String...)
* @see #loadBeanDefinitions(DefaultListableBeanFactory)
* @see #setConfigLocation(String)
* @see #refresh()
*/
public
void
register
(
Class
<?>...
annotatedClasses
)
{
@Override
public
final
void
register
(
Class
<?>...
annotatedClasses
)
{
Assert
.
notEmpty
(
annotatedClasses
,
"At least one annotated class must be specified"
);
Collections
.
addAll
(
this
.
annotatedClasses
,
annotatedClasses
);
}
...
...
@@ -157,16 +161,58 @@ public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWe
* <p>Note that {@link #refresh()} must be called in order for the context
* to fully process the new classes.
* @param basePackages the packages to check for annotated classes
* @see #loadBeanDefinitions(DefaultListableBeanFactory)
* @see #register(Class...)
* @see #
setConfigLocation(String
)
* @see #
loadBeanDefinitions(DefaultListableBeanFactory
)
* @see #refresh()
*/
public
void
scan
(
String
...
basePackages
)
{
@Override
public
final
void
scan
(
String
...
basePackages
)
{
Assert
.
notEmpty
(
basePackages
,
"At least one base package must be specified"
);
Collections
.
addAll
(
this
.
basePackages
,
basePackages
);
}
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations.
* <p>Note that {@link #refresh()} must be called in order for the context
* to fully process the new classes.
* @param annotatedClass the class of the bean
* @param qualifiers specific qualifier annotations to consider,
* in addition to qualifiers at the bean class level (may be empty)
* @since 5.2
* @see #register(Class...)
* @see #loadBeanDefinitions(DefaultListableBeanFactory)
* @see #refresh()
*/
@Override
@SafeVarargs
@SuppressWarnings
(
"varargs"
)
public
final
<
T
>
void
registerBean
(
Class
<
T
>
annotatedClass
,
Class
<?
extends
Annotation
>...
qualifiers
)
{
this
.
registeredBeans
.
add
(
new
BeanRegistration
(
annotatedClass
,
null
,
qualifiers
));
}
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations.
* <p>Note that {@link #refresh()} must be called in order for the context
* to fully process the new classes.
* @param annotatedClass the class of the bean
* @param qualifiers specific qualifier annotations to consider,
* in addition to qualifiers at the bean class level (may be empty)
* @since 5.2
* @see #register(Class...)
* @see #loadBeanDefinitions(DefaultListableBeanFactory)
* @see #refresh()
*/
@Override
@SafeVarargs
@SuppressWarnings
(
"varargs"
)
public
final
<
T
>
void
registerBean
(
Class
<
T
>
annotatedClass
,
Supplier
<
T
>
supplier
,
Class
<?
extends
Annotation
>...
qualifiers
)
{
this
.
registeredBeans
.
add
(
new
BeanRegistration
(
annotatedClass
,
supplier
,
qualifiers
));
}
/**
* Register a {@link org.springframework.beans.factory.config.BeanDefinition} for
...
...
@@ -191,6 +237,7 @@ public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWe
* @see ClassPathBeanDefinitionScanner
*/
@Override
@SuppressWarnings
(
"unchecked"
)
protected
void
loadBeanDefinitions
(
DefaultListableBeanFactory
beanFactory
)
{
AnnotatedBeanDefinitionReader
reader
=
getAnnotatedBeanDefinitionReader
(
beanFactory
);
ClassPathBeanDefinitionScanner
scanner
=
getClassPathBeanDefinitionScanner
(
beanFactory
);
...
...
@@ -224,6 +271,15 @@ public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWe
scanner
.
scan
(
StringUtils
.
toStringArray
(
this
.
basePackages
));
}
if
(!
this
.
registeredBeans
.
isEmpty
())
{
if
(
logger
.
isDebugEnabled
())
{
logger
.
debug
(
"Registering supplied beans: ["
+
StringUtils
.
collectionToCommaDelimitedString
(
this
.
registeredBeans
)
+
"]"
);
}
this
.
registeredBeans
.
forEach
(
reg
->
reader
.
registerBean
(
reg
.
getAnnotatedClass
(),
reg
.
getSupplier
(),
reg
.
getQualifiers
()));
}
String
[]
configLocations
=
getConfigLocations
();
if
(
configLocations
!=
null
)
{
for
(
String
configLocation
:
configLocations
)
{
...
...
@@ -277,4 +333,46 @@ public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWe
return
new
ClassPathBeanDefinitionScanner
(
beanFactory
,
true
,
getEnvironment
());
}
/**
* Holder for a programmatic bean registration.
* @see #registerBean(Class, Class[])
* @see #registerBean(Class, Supplier, Class[])
*/
private
static
class
BeanRegistration
{
private
final
Class
<?>
annotatedClass
;
@Nullable
private
final
Supplier
<?>
supplier
;
private
final
Class
<?
extends
Annotation
>[]
qualifiers
;
public
BeanRegistration
(
Class
<?>
annotatedClass
,
@Nullable
Supplier
<?>
supplier
,
Class
<?
extends
Annotation
>[]
qualifiers
)
{
this
.
annotatedClass
=
annotatedClass
;
this
.
supplier
=
supplier
;
this
.
qualifiers
=
qualifiers
;
}
public
Class
<?>
getAnnotatedClass
()
{
return
this
.
annotatedClass
;
}
@Nullable
@SuppressWarnings
(
"rawtypes"
)
public
Supplier
getSupplier
()
{
return
this
.
supplier
;
}
public
Class
<?
extends
Annotation
>[]
getQualifiers
()
{
return
this
.
qualifiers
;
}
@Override
public
String
toString
()
{
return
this
.
annotatedClass
.
getName
();
}
}
}
spring-web/src/test/java/org/springframework/web/context/support/AnnotationConfigWebApplicationContextTests.java
浏览文件 @
18f2e6a1
/*
* Copyright 2002-201
5
the original author or authors.
* Copyright 2002-201
9
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.
...
...
@@ -23,6 +23,7 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import
org.springframework.context.annotation.AnnotationBeanNameGenerator
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Lazy
;
import
static
org
.
hamcrest
.
CoreMatchers
.*;
import
static
org
.
junit
.
Assert
.*;
...
...
@@ -82,6 +83,54 @@ public class AnnotationConfigWebApplicationContextTests {
assertThat
(
ctx
.
containsBean
(
"custom-myConfig"
),
is
(
true
));
}
@Test
@SuppressWarnings
(
"resource"
)
public
void
registerBean
()
{
AnnotationConfigWebApplicationContext
ctx
=
new
AnnotationConfigWebApplicationContext
();
ctx
.
registerBean
(
TestBean
.
class
);
ctx
.
refresh
();
assertTrue
(
ctx
.
getBeanFactory
().
containsSingleton
(
"annotationConfigWebApplicationContextTests.TestBean"
));
TestBean
bean
=
ctx
.
getBean
(
TestBean
.
class
);
assertNotNull
(
bean
);
}
@Test
@SuppressWarnings
(
"resource"
)
public
void
registerBeanWithLazy
()
{
AnnotationConfigWebApplicationContext
ctx
=
new
AnnotationConfigWebApplicationContext
();
ctx
.
registerBean
(
TestBean
.
class
,
Lazy
.
class
);
ctx
.
refresh
();
assertFalse
(
ctx
.
getBeanFactory
().
containsSingleton
(
"annotationConfigWebApplicationContextTests.TestBean"
));
TestBean
bean
=
ctx
.
getBean
(
TestBean
.
class
);
assertNotNull
(
bean
);
}
@Test
@SuppressWarnings
(
"resource"
)
public
void
registerBeanWithSupplier
()
{
AnnotationConfigWebApplicationContext
ctx
=
new
AnnotationConfigWebApplicationContext
();
ctx
.
registerBean
(
TestBean
.
class
,
TestBean:
:
new
);
ctx
.
refresh
();
assertTrue
(
ctx
.
getBeanFactory
().
containsSingleton
(
"annotationConfigWebApplicationContextTests.TestBean"
));
TestBean
bean
=
ctx
.
getBean
(
TestBean
.
class
);
assertNotNull
(
bean
);
}
@Test
@SuppressWarnings
(
"resource"
)
public
void
registerBeanWithSupplierAndLazy
()
{
AnnotationConfigWebApplicationContext
ctx
=
new
AnnotationConfigWebApplicationContext
();
ctx
.
registerBean
(
TestBean
.
class
,
TestBean:
:
new
,
Lazy
.
class
);
ctx
.
refresh
();
assertFalse
(
ctx
.
getBeanFactory
().
containsSingleton
(
"annotationConfigWebApplicationContextTests.TestBean"
));
TestBean
bean
=
ctx
.
getBean
(
TestBean
.
class
);
assertNotNull
(
bean
);
}
@Configuration
(
"myConfig"
)
static
class
Config
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录