Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
带水的鱼儿
spring-framework
提交
b3dcb64f
S
spring-framework
项目概览
带水的鱼儿
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
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,发现更多精彩内容 >>
提交
b3dcb64f
编写于
5月 28, 2021
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Generic bean type resolution for lookup methods
Closes gh-26998
上级
b18f8771
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
102 addition
and
23 deletion
+102
-23
spring-beans/src/main/java/org/springframework/beans/factory/support/CglibSubclassingInstantiationStrategy.java
...actory/support/CglibSubclassingInstantiationStrategy.java
+6
-3
spring-beans/src/main/java/org/springframework/beans/factory/support/LookupOverride.java
...springframework/beans/factory/support/LookupOverride.java
+16
-8
spring-beans/src/main/java/org/springframework/beans/factory/support/MethodReplacer.java
...springframework/beans/factory/support/MethodReplacer.java
+3
-4
spring-beans/src/main/java/org/springframework/beans/factory/support/ReplaceOverride.java
...pringframework/beans/factory/support/ReplaceOverride.java
+3
-3
spring-beans/src/test/java/org/springframework/beans/factory/annotation/LookupAnnotationTests.java
...ework/beans/factory/annotation/LookupAnnotationTests.java
+35
-1
spring-beans/src/test/java/org/springframework/beans/factory/support/LookupMethodTests.java
...ingframework/beans/factory/support/LookupMethodTests.java
+39
-4
未找到文件。
spring-beans/src/main/java/org/springframework/beans/factory/support/CglibSubclassingInstantiationStrategy.java
浏览文件 @
b3dcb64f
/*
* Copyright 2002-202
0
the original author or authors.
* Copyright 2002-202
1
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.
...
...
@@ -35,6 +35,7 @@ import org.springframework.cglib.proxy.Factory;
import
org.springframework.cglib.proxy.MethodInterceptor
;
import
org.springframework.cglib.proxy.MethodProxy
;
import
org.springframework.cglib.proxy.NoOp
;
import
org.springframework.core.ResolvableType
;
import
org.springframework.lang.Nullable
;
import
org.springframework.util.Assert
;
import
org.springframework.util.StringUtils
;
...
...
@@ -244,8 +245,10 @@ public class CglibSubclassingInstantiationStrategy extends SimpleInstantiationSt
return
(
bean
.
equals
(
null
)
?
null
:
bean
);
}
else
{
return
(
argsToUse
!=
null
?
this
.
owner
.
getBean
(
method
.
getReturnType
(),
argsToUse
)
:
this
.
owner
.
getBean
(
method
.
getReturnType
()));
// Find target bean matching the (potentially generic) method return type
ResolvableType
genericReturnType
=
ResolvableType
.
forMethodReturnType
(
method
);
return
(
argsToUse
!=
null
?
this
.
owner
.
getBeanProvider
(
genericReturnType
).
getObject
(
argsToUse
)
:
this
.
owner
.
getBeanProvider
(
genericReturnType
).
getObject
());
}
}
}
...
...
spring-beans/src/main/java/org/springframework/beans/factory/support/LookupOverride.java
浏览文件 @
b3dcb64f
/*
* Copyright 2002-20
17
the original author or authors.
* Copyright 2002-20
21
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.
...
...
@@ -19,17 +19,25 @@ package org.springframework.beans.factory.support;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Modifier
;
import
org.springframework.core.ResolvableType
;
import
org.springframework.lang.Nullable
;
import
org.springframework.util.ObjectUtils
;
/**
* Represents an override of a method that looks up an object in the same IoC context.
* Represents an override of a method that looks up an object in the same IoC context,
* either by bean name or by bean type (based on the declared method return type).
*
* <p>Methods eligible for lookup override must not have arguments.
* <p>Methods eligible for lookup override may declare arguments in which case the
* given arguments are passed to the bean retrieval operation.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @since 1.1
* @see org.springframework.beans.factory.BeanFactory#getBean(String)
* @see org.springframework.beans.factory.BeanFactory#getBean(Class)
* @see org.springframework.beans.factory.BeanFactory#getBean(String, Object...)
* @see org.springframework.beans.factory.BeanFactory#getBean(Class, Object...)
* @see org.springframework.beans.factory.BeanFactory#getBeanProvider(ResolvableType)
*/
public
class
LookupOverride
extends
MethodOverride
{
...
...
@@ -43,8 +51,8 @@ public class LookupOverride extends MethodOverride {
/**
* Construct a new LookupOverride.
* @param methodName the name of the method to override
* @param beanName the name of the bean in the current {@code BeanFactory}
*
that the overridden method should return (may be {@code null}
)
* @param beanName the name of the bean in the current {@code BeanFactory}
that the
*
overridden method should return (may be {@code null} for type-based bean retrieval
)
*/
public
LookupOverride
(
String
methodName
,
@Nullable
String
beanName
)
{
super
(
methodName
);
...
...
@@ -53,9 +61,9 @@ public class LookupOverride extends MethodOverride {
/**
* Construct a new LookupOverride.
* @param method the method to override
* @param beanName the name of the bean in the current {@code BeanFactory}
*
that the overridden method should return (may be {@code null}
)
* @param method the method
declaration
to override
* @param beanName the name of the bean in the current {@code BeanFactory}
that the
*
overridden method should return (may be {@code null} for type-based bean retrieval
)
*/
public
LookupOverride
(
Method
method
,
@Nullable
String
beanName
)
{
super
(
method
.
getName
());
...
...
spring-beans/src/main/java/org/springframework/beans/factory/support/MethodReplacer.java
浏览文件 @
b3dcb64f
/*
* Copyright 2002-20
12
the original author or authors.
* Copyright 2002-20
21
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.
...
...
@@ -19,9 +19,8 @@ package org.springframework.beans.factory.support;
import
java.lang.reflect.Method
;
/**
* Interface to be implemented by classes that can reimplement any method
* on an IoC-managed object: the <b>Method Injection</b> form of
* Dependency Injection.
* Interface to be implemented by classes that can reimplement any method on an
* IoC-managed object: the <b>Method Injection</b> form of Dependency Injection.
*
* <p>Such methods may be (but need not be) abstract, in which case the
* container will create a concrete subclass to instantiate.
...
...
spring-beans/src/main/java/org/springframework/beans/factory/support/ReplaceOverride.java
浏览文件 @
b3dcb64f
/*
* Copyright 2002-202
0
the original author or authors.
* Copyright 2002-202
1
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.
...
...
@@ -25,7 +25,7 @@ import org.springframework.util.Assert;
import
org.springframework.util.ObjectUtils
;
/**
* Extension of
MethodOverride
that represents an arbitrary
* Extension of
{@link MethodOverride}
that represents an arbitrary
* override of a method by the IoC container.
*
* <p>Any non-final method can be overridden, irrespective of its
...
...
@@ -45,7 +45,7 @@ public class ReplaceOverride extends MethodOverride {
/**
* Construct a new ReplaceOverride.
* @param methodName the name of the method to override
* @param methodReplacerBeanName the bean name of the
MethodReplacer
* @param methodReplacerBeanName the bean name of the
{@link MethodReplacer}
*/
public
ReplaceOverride
(
String
methodName
,
String
methodReplacerBeanName
)
{
super
(
methodName
);
...
...
spring-beans/src/test/java/org/springframework/beans/factory/annotation/LookupAnnotationTests.java
浏览文件 @
b3dcb64f
/*
* Copyright 2002-202
0
the original author or authors.
* Copyright 2002-202
1
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.
...
...
@@ -121,6 +121,18 @@ public class LookupAnnotationTests {
assertThat
(
beanFactory
.
getBean
(
BeanConsumer
.
class
).
abstractBean
).
isSameAs
(
bean
);
}
@Test
public
void
testWithGenericBean
()
{
beanFactory
.
registerBeanDefinition
(
"numberBean"
,
new
RootBeanDefinition
(
NumberBean
.
class
));
beanFactory
.
registerBeanDefinition
(
"doubleStore"
,
new
RootBeanDefinition
(
DoubleStore
.
class
));
beanFactory
.
registerBeanDefinition
(
"floatStore"
,
new
RootBeanDefinition
(
FloatStore
.
class
));
NumberBean
bean
=
(
NumberBean
)
beanFactory
.
getBean
(
"numberBean"
);
assertThat
(
bean
).
isNotNull
();
assertThat
(
beanFactory
.
getBean
(
DoubleStore
.
class
)).
isSameAs
(
bean
.
getDoubleStore
());
assertThat
(
beanFactory
.
getBean
(
FloatStore
.
class
)).
isSameAs
(
bean
.
getFloatStore
());
}
public
static
abstract
class
AbstractBean
{
...
...
@@ -147,4 +159,26 @@ public class LookupAnnotationTests {
AbstractBean
abstractBean
;
}
public
static
class
NumberStore
<
T
extends
Number
>
{
}
public
static
class
DoubleStore
extends
NumberStore
<
Double
>
{
}
public
static
class
FloatStore
extends
NumberStore
<
Float
>
{
}
public
static
abstract
class
NumberBean
{
@Lookup
public
abstract
NumberStore
<
Double
>
getDoubleStore
();
@Lookup
public
abstract
NumberStore
<
Float
>
getFloatStore
();
}
}
spring-beans/src/test/java/org/springframework/beans/factory/support/LookupMethodTests.java
浏览文件 @
b3dcb64f
/*
* Copyright 2002-20
19
the original author or authors.
* Copyright 2002-20
21
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.
...
...
@@ -36,7 +36,7 @@ public class LookupMethodTests {
@BeforeEach
public
void
set
U
p
()
{
public
void
set
u
p
()
{
beanFactory
=
new
DefaultListableBeanFactory
();
XmlBeanDefinitionReader
reader
=
new
XmlBeanDefinitionReader
(
beanFactory
);
reader
.
loadBeanDefinitions
(
new
ClassPathResource
(
"lookupMethodTests.xml"
,
getClass
()));
...
...
@@ -83,8 +83,8 @@ public class LookupMethodTests {
public
void
testWithThreeArgsShouldFail
()
{
AbstractBean
bean
=
(
AbstractBean
)
beanFactory
.
getBean
(
"abstractBean"
);
assertThat
(
bean
).
isNotNull
();
assertThatExceptionOfType
(
AbstractMethodError
.
class
).
as
(
"does not have a three arg constructor"
)
.
isThrownBy
(()
->
bean
.
getThreeArguments
(
"name"
,
1
,
2
));
assertThatExceptionOfType
(
AbstractMethodError
.
class
).
as
(
"does not have a three arg constructor"
)
.
isThrownBy
(()
->
bean
.
getThreeArguments
(
"name"
,
1
,
2
));
}
@Test
...
...
@@ -97,6 +97,21 @@ public class LookupMethodTests {
assertThat
(
expected
.
isJedi
()).
isTrue
();
}
@Test
public
void
testWithGenericBean
()
{
RootBeanDefinition
bd
=
new
RootBeanDefinition
(
NumberBean
.
class
);
bd
.
getMethodOverrides
().
addOverride
(
new
LookupOverride
(
"getDoubleStore"
,
null
));
bd
.
getMethodOverrides
().
addOverride
(
new
LookupOverride
(
"getFloatStore"
,
null
));
beanFactory
.
registerBeanDefinition
(
"numberBean"
,
bd
);
beanFactory
.
registerBeanDefinition
(
"doubleStore"
,
new
RootBeanDefinition
(
DoubleStore
.
class
));
beanFactory
.
registerBeanDefinition
(
"floatStore"
,
new
RootBeanDefinition
(
FloatStore
.
class
));
NumberBean
bean
=
(
NumberBean
)
beanFactory
.
getBean
(
"numberBean"
);
assertThat
(
bean
).
isNotNull
();
assertThat
(
beanFactory
.
getBean
(
DoubleStore
.
class
)).
isSameAs
(
bean
.
getDoubleStore
());
assertThat
(
beanFactory
.
getBean
(
FloatStore
.
class
)).
isSameAs
(
bean
.
getFloatStore
());
}
public
static
abstract
class
AbstractBean
{
...
...
@@ -111,4 +126,24 @@ public class LookupMethodTests {
public
abstract
TestBean
getThreeArguments
(
String
name
,
int
age
,
int
anotherArg
);
}
public
static
class
NumberStore
<
T
extends
Number
>
{
}
public
static
class
DoubleStore
extends
NumberStore
<
Double
>
{
}
public
static
class
FloatStore
extends
NumberStore
<
Float
>
{
}
public
static
abstract
class
NumberBean
{
public
abstract
NumberStore
<
Double
>
getDoubleStore
();
public
abstract
NumberStore
<
Float
>
getFloatStore
();
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录