Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
70fec479
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,发现更多精彩内容 >>
提交
70fec479
编写于
10月 06, 2014
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Provider declaration for @Value method argument works again
Issue: SPR-12297
上级
9d832816
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
146 addition
and
47 deletion
+146
-47
spring-context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java
...annotation/configuration/AutowiredConfigurationTests.java
+141
-43
spring-context/src/test/java/org/springframework/context/expression/ApplicationContextExpressionTests.java
...context/expression/ApplicationContextExpressionTests.java
+5
-1
spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java
...java/org/springframework/core/convert/TypeDescriptor.java
+0
-3
未找到文件。
spring-context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java
浏览文件 @
70fec479
/*
/*
* Copyright 2002-201
3
the original author or authors.
* Copyright 2002-201
4
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -16,24 +16,29 @@
...
@@ -16,24 +16,29 @@
package
org.springframework.context.annotation.configuration
;
package
org.springframework.context.annotation.configuration
;
import
static
org
.
hamcrest
.
CoreMatchers
.*
;
import
javax.inject.Provider
;
import
static
org
.
junit
.
Assert
.*;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.tests.sample.beans.Colour
;
import
org.springframework.tests.sample.beans.TestBean
;
import
org.springframework.beans.factory.BeanCreationException
;
import
org.springframework.beans.factory.BeanCreationException
;
import
org.springframework.beans.factory.BeanFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.support.DefaultListableBeanFactory
;
import
org.springframework.beans.factory.support.DefaultListableBeanFactory
;
import
org.springframework.beans.factory.support.RootBeanDefinition
;
import
org.springframework.beans.factory.support.RootBeanDefinition
;
import
org.springframework.beans.factory.xml.XmlBeanDefinitionReader
;
import
org.springframework.beans.factory.xml.XmlBeanDefinitionReader
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Scope
;
import
org.springframework.context.annotation.Scope
;
import
org.springframework.context.support.ClassPathXmlApplicationContext
;
import
org.springframework.context.support.ClassPathXmlApplicationContext
;
import
org.springframework.context.support.GenericApplicationContext
;
import
org.springframework.context.support.GenericApplicationContext
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.tests.sample.beans.Colour
;
import
org.springframework.tests.sample.beans.TestBean
;
import
static
org
.
hamcrest
.
CoreMatchers
.*;
import
static
org
.
junit
.
Assert
.*;
/**
/**
* System tests covering use of {@link Autowired} and {@link Value} within
* System tests covering use of {@link Autowired} and {@link Value} within
...
@@ -53,32 +58,11 @@ public class AutowiredConfigurationTests {
...
@@ -53,32 +58,11 @@ public class AutowiredConfigurationTests {
assertThat
(
factory
.
getBean
(
"testBean"
,
TestBean
.
class
).
getName
(),
equalTo
(
Colour
.
RED
.
toString
()));
assertThat
(
factory
.
getBean
(
"testBean"
,
TestBean
.
class
).
getName
(),
equalTo
(
Colour
.
RED
.
toString
()));
}
}
@Configuration
static
class
AutowiredConfig
{
@Autowired
private
Colour
colour
;
@Bean
public
TestBean
testBean
()
{
return
new
TestBean
(
colour
.
toString
());
}
}
@Configuration
static
class
ColorConfig
{
@Bean
public
Colour
colour
()
{
return
Colour
.
RED
;
}
}
/**
/**
* {@link Autowired} constructors are not supported on {@link Configuration} classes
* {@link Autowired} constructors are not supported on {@link Configuration} classes
* due to CGLIB constraints
* due to CGLIB constraints
*/
*/
@Test
(
expected
=
BeanCreationException
.
class
)
@Test
(
expected
=
BeanCreationException
.
class
)
public
void
testAutowiredConfigurationConstructorsAreNotSupported
()
{
public
void
testAutowiredConfigurationConstructorsAreNotSupported
()
{
DefaultListableBeanFactory
factory
=
new
DefaultListableBeanFactory
();
DefaultListableBeanFactory
factory
=
new
DefaultListableBeanFactory
();
new
XmlBeanDefinitionReader
(
factory
).
loadBeanDefinitions
(
new
XmlBeanDefinitionReader
(
factory
).
loadBeanDefinitions
(
...
@@ -89,22 +73,35 @@ public class AutowiredConfigurationTests {
...
@@ -89,22 +73,35 @@ public class AutowiredConfigurationTests {
ctx
.
refresh
();
// should throw
ctx
.
refresh
();
// should throw
}
}
@Configuration
@Test
static
class
AutowiredConstructorConfig
{
public
void
testValueInjection
()
{
Colour
colour
;
ClassPathXmlApplicationContext
factory
=
new
ClassPathXmlApplicationContext
(
"ValueInjectionTests.xml"
,
AutowiredConfigurationTests
.
class
);
@Autowired
doTestValueInjection
(
factory
);
AutowiredConstructorConfig
(
Colour
colour
)
{
this
.
colour
=
colour
;
}
}
@Test
public
void
testValueInjectionWithProviderFields
()
{
AnnotationConfigApplicationContext
factory
=
new
AnnotationConfigApplicationContext
(
ValueConfigWithProviderFields
.
class
);
doTestValueInjection
(
factory
);
}
}
@Test
public
void
testValueInjectionWithProviderConstructorArguments
()
{
AnnotationConfigApplicationContext
factory
=
new
AnnotationConfigApplicationContext
(
ValueConfigWithProviderConstructorArguments
.
class
);
doTestValueInjection
(
factory
);
}
@Test
@Test
public
void
testValueInjection
()
{
public
void
testValueInjectionWithProviderMethodArguments
()
{
ClassPathXmlApplicationContext
factory
=
new
ClassPathXmlApplicationContext
(
AnnotationConfigApplicationContext
factory
=
"ValueInjectionTests.xml"
,
AutowiredConfigurationTests
.
class
);
new
AnnotationConfigApplicationContext
(
ValueConfigWithProviderMethodArguments
.
class
);
doTestValueInjection
(
factory
);
}
private
void
doTestValueInjection
(
BeanFactory
factory
)
{
System
.
clearProperty
(
"myProp"
);
System
.
clearProperty
(
"myProp"
);
TestBean
testBean
=
factory
.
getBean
(
"testBean"
,
TestBean
.
class
);
TestBean
testBean
=
factory
.
getBean
(
"testBean"
,
TestBean
.
class
);
...
@@ -130,6 +127,51 @@ public class AutowiredConfigurationTests {
...
@@ -130,6 +127,51 @@ public class AutowiredConfigurationTests {
assertNull
(
testBean
.
getName
());
assertNull
(
testBean
.
getName
());
}
}
@Test
public
void
testCustomProperties
()
{
ClassPathXmlApplicationContext
factory
=
new
ClassPathXmlApplicationContext
(
"AutowiredConfigurationTests-custom.xml"
,
AutowiredConfigurationTests
.
class
);
TestBean
testBean
=
factory
.
getBean
(
"testBean"
,
TestBean
.
class
);
assertThat
(
testBean
.
getName
(),
equalTo
(
"localhost"
));
}
@Configuration
static
class
AutowiredConfig
{
@Autowired
private
Colour
colour
;
@Bean
public
TestBean
testBean
()
{
return
new
TestBean
(
colour
.
toString
());
}
}
@Configuration
static
class
ColorConfig
{
@Bean
public
Colour
colour
()
{
return
Colour
.
RED
;
}
}
@Configuration
static
class
AutowiredConstructorConfig
{
Colour
colour
;
@Autowired
AutowiredConstructorConfig
(
Colour
colour
)
{
this
.
colour
=
colour
;
}
}
@Configuration
@Configuration
static
class
ValueConfig
{
static
class
ValueConfig
{
...
@@ -155,15 +197,71 @@ public class AutowiredConfigurationTests {
...
@@ -155,15 +197,71 @@ public class AutowiredConfigurationTests {
}
}
@Test
@Configuration
public
void
testCustomProperties
()
{
static
class
ValueConfigWithProviderFields
{
ClassPathXmlApplicationContext
factory
=
new
ClassPathXmlApplicationContext
(
"AutowiredConfigurationTests-custom.xml"
,
AutowiredConfigurationTests
.
class
);
TestBean
testBean
=
factory
.
getBean
(
"testBean"
,
TestBean
.
class
);
@Value
(
"#{systemProperties[myProp]}"
)
assertThat
(
testBean
.
getName
(),
equalTo
(
"localhost"
));
private
Provider
<
String
>
name
;
private
Provider
<
String
>
name2
;
@Value
(
"#{systemProperties[myProp]}"
)
public
void
setName2
(
Provider
<
String
>
name
)
{
this
.
name2
=
name
;
}
}
@Bean
@Scope
(
"prototype"
)
public
TestBean
testBean
()
{
return
new
TestBean
(
name
.
get
());
}
@Bean
@Scope
(
"prototype"
)
public
TestBean
testBean2
()
{
return
new
TestBean
(
name2
.
get
());
}
}
static
class
ValueConfigWithProviderConstructorArguments
{
private
final
Provider
<
String
>
name
;
private
final
Provider
<
String
>
name2
;
@Autowired
public
ValueConfigWithProviderConstructorArguments
(
@Value
(
"#{systemProperties[myProp]}"
)
Provider
<
String
>
name
,
@Value
(
"#{systemProperties[myProp]}"
)
Provider
<
String
>
name2
)
{
this
.
name
=
name
;
this
.
name2
=
name2
;
}
@Bean
@Scope
(
"prototype"
)
public
TestBean
testBean
()
{
return
new
TestBean
(
name
.
get
());
}
@Bean
@Scope
(
"prototype"
)
public
TestBean
testBean2
()
{
return
new
TestBean
(
name2
.
get
());
}
}
@Configuration
static
class
ValueConfigWithProviderMethodArguments
{
@Bean
@Scope
(
"prototype"
)
public
TestBean
testBean
(
@Value
(
"#{systemProperties[myProp]}"
)
Provider
<
String
>
name
)
{
return
new
TestBean
(
name
.
get
());
}
@Bean
@Scope
(
"prototype"
)
public
TestBean
testBean2
(
@Value
(
"#{systemProperties[myProp]}"
)
Provider
<
String
>
name2
)
{
return
new
TestBean
(
name2
.
get
());
}
}
@Configuration
@Configuration
static
class
PropertiesConfig
{
static
class
PropertiesConfig
{
...
...
spring-context/src/test/java/org/springframework/context/expression/ApplicationContextExpressionTests.java
浏览文件 @
70fec479
/*
/*
* Copyright 2002-201
3
the original author or authors.
* Copyright 2002-201
4
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -153,6 +153,7 @@ public class ApplicationContextExpressionTests {
...
@@ -153,6 +153,7 @@ public class ApplicationContextExpressionTests {
ValueTestBean
tb3
=
ac
.
getBean
(
"tb3"
,
ValueTestBean
.
class
);
ValueTestBean
tb3
=
ac
.
getBean
(
"tb3"
,
ValueTestBean
.
class
);
assertEquals
(
"XXXmyNameYYY42ZZZ"
,
tb3
.
name
);
assertEquals
(
"XXXmyNameYYY42ZZZ"
,
tb3
.
name
);
assertEquals
(
42
,
tb3
.
age
);
assertEquals
(
42
,
tb3
.
age
);
assertEquals
(
42
,
tb3
.
ageFactory
.
getObject
().
intValue
());
assertEquals
(
"123 UK"
,
tb3
.
country
);
assertEquals
(
"123 UK"
,
tb3
.
country
);
assertEquals
(
"123 UK"
,
tb3
.
countryFactory
.
getObject
());
assertEquals
(
"123 UK"
,
tb3
.
countryFactory
.
getObject
());
System
.
getProperties
().
put
(
"country"
,
"US"
);
System
.
getProperties
().
put
(
"country"
,
"US"
);
...
@@ -321,6 +322,9 @@ public class ApplicationContextExpressionTests {
...
@@ -321,6 +322,9 @@ public class ApplicationContextExpressionTests {
@Autowired
@Value
(
"#{mySpecialAttr}"
)
@Autowired
@Value
(
"#{mySpecialAttr}"
)
public
int
age
;
public
int
age
;
@Value
(
"#{mySpecialAttr}"
)
public
ObjectFactory
<
Integer
>
ageFactory
;
@Value
(
"${code} #{systemProperties.country}"
)
@Value
(
"${code} #{systemProperties.country}"
)
public
String
country
;
public
String
country
;
...
...
spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java
浏览文件 @
70fec479
...
@@ -74,9 +74,6 @@ public class TypeDescriptor implements Serializable {
...
@@ -74,9 +74,6 @@ public class TypeDescriptor implements Serializable {
*/
*/
public
TypeDescriptor
(
MethodParameter
methodParameter
)
{
public
TypeDescriptor
(
MethodParameter
methodParameter
)
{
Assert
.
notNull
(
methodParameter
,
"MethodParameter must not be null"
);
Assert
.
notNull
(
methodParameter
,
"MethodParameter must not be null"
);
if
(
methodParameter
.
getNestingLevel
()
!=
1
)
{
throw
new
IllegalArgumentException
(
"MethodParameter argument must have its nestingLevel set to 1"
);
}
this
.
resolvableType
=
ResolvableType
.
forMethodParameter
(
methodParameter
);
this
.
resolvableType
=
ResolvableType
.
forMethodParameter
(
methodParameter
);
this
.
type
=
this
.
resolvableType
.
resolve
(
methodParameter
.
getParameterType
());
this
.
type
=
this
.
resolvableType
.
resolve
(
methodParameter
.
getParameterType
());
this
.
annotations
=
(
methodParameter
.
getParameterIndex
()
==
-
1
?
this
.
annotations
=
(
methodParameter
.
getParameterIndex
()
==
-
1
?
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录