Skip to content

Commit 6183e83

Browse files
committed
Provider declaration for @value method argument works again
Issue: SPR-12297 (cherry picked from commit c672678)
1 parent a1538a4 commit 6183e83

File tree

3 files changed

+152
-52
lines changed

3 files changed

+152
-52
lines changed

spring-context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java

Lines changed: 141 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,24 +16,29 @@
1616

1717
package org.springframework.context.annotation.configuration;
1818

19-
import static org.hamcrest.CoreMatchers.*;
20-
import static org.junit.Assert.*;
19+
import javax.inject.Provider;
20+
2121
import org.junit.Test;
22-
import org.springframework.tests.sample.beans.Colour;
23-
import org.springframework.tests.sample.beans.TestBean;
2422

2523
import org.springframework.beans.factory.BeanCreationException;
24+
import org.springframework.beans.factory.BeanFactory;
2625
import org.springframework.beans.factory.annotation.Autowired;
2726
import org.springframework.beans.factory.annotation.Value;
2827
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
2928
import org.springframework.beans.factory.support.RootBeanDefinition;
3029
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
30+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3131
import org.springframework.context.annotation.Bean;
3232
import org.springframework.context.annotation.Configuration;
3333
import org.springframework.context.annotation.Scope;
3434
import org.springframework.context.support.ClassPathXmlApplicationContext;
3535
import org.springframework.context.support.GenericApplicationContext;
3636
import org.springframework.core.io.ClassPathResource;
37+
import org.springframework.tests.sample.beans.Colour;
38+
import org.springframework.tests.sample.beans.TestBean;
39+
40+
import static org.hamcrest.CoreMatchers.*;
41+
import static org.junit.Assert.*;
3742

3843
/**
3944
* System tests covering use of {@link Autowired} and {@link Value} within
@@ -53,32 +58,11 @@ public void testAutowiredConfigurationDependencies() {
5358
assertThat(factory.getBean("testBean", TestBean.class).getName(), equalTo(Colour.RED.toString()));
5459
}
5560

56-
@Configuration
57-
static class AutowiredConfig {
58-
@Autowired
59-
private Colour colour;
60-
61-
@Bean
62-
public TestBean testBean() {
63-
return new TestBean(colour.toString());
64-
}
65-
}
66-
67-
@Configuration
68-
static class ColorConfig {
69-
70-
@Bean
71-
public Colour colour() {
72-
return Colour.RED;
73-
}
74-
}
75-
76-
7761
/**
7862
* {@link Autowired} constructors are not supported on {@link Configuration} classes
7963
* due to CGLIB constraints
8064
*/
81-
@Test(expected=BeanCreationException.class)
65+
@Test(expected = BeanCreationException.class)
8266
public void testAutowiredConfigurationConstructorsAreNotSupported() {
8367
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
8468
new XmlBeanDefinitionReader(factory).loadBeanDefinitions(
@@ -89,22 +73,35 @@ public void testAutowiredConfigurationConstructorsAreNotSupported() {
8973
ctx.refresh(); // should throw
9074
}
9175

92-
@Configuration
93-
static class AutowiredConstructorConfig {
94-
Colour colour;
76+
@Test
77+
public void testValueInjection() {
78+
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext(
79+
"ValueInjectionTests.xml", AutowiredConfigurationTests.class);
80+
doTestValueInjection(factory);
81+
}
9582

96-
@Autowired
97-
AutowiredConstructorConfig(Colour colour) {
98-
this.colour = colour;
99-
}
83+
@Test
84+
public void testValueInjectionWithProviderFields() {
85+
AnnotationConfigApplicationContext factory =
86+
new AnnotationConfigApplicationContext(ValueConfigWithProviderFields.class);
87+
doTestValueInjection(factory);
10088
}
10189

90+
@Test
91+
public void testValueInjectionWithProviderConstructorArguments() {
92+
AnnotationConfigApplicationContext factory =
93+
new AnnotationConfigApplicationContext(ValueConfigWithProviderConstructorArguments.class);
94+
doTestValueInjection(factory);
95+
}
10296

10397
@Test
104-
public void testValueInjection() {
105-
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext(
106-
"ValueInjectionTests.xml", AutowiredConfigurationTests.class);
98+
public void testValueInjectionWithProviderMethodArguments() {
99+
AnnotationConfigApplicationContext factory =
100+
new AnnotationConfigApplicationContext(ValueConfigWithProviderMethodArguments.class);
101+
doTestValueInjection(factory);
102+
}
107103

104+
private void doTestValueInjection(BeanFactory factory) {
108105
System.clearProperty("myProp");
109106

110107
TestBean testBean = factory.getBean("testBean", TestBean.class);
@@ -130,6 +127,51 @@ public void testValueInjection() {
130127
assertNull(testBean.getName());
131128
}
132129

130+
@Test
131+
public void testCustomProperties() {
132+
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext(
133+
"AutowiredConfigurationTests-custom.xml", AutowiredConfigurationTests.class);
134+
135+
TestBean testBean = factory.getBean("testBean", TestBean.class);
136+
assertThat(testBean.getName(), equalTo("localhost"));
137+
}
138+
139+
140+
@Configuration
141+
static class AutowiredConfig {
142+
143+
@Autowired
144+
private Colour colour;
145+
146+
@Bean
147+
public TestBean testBean() {
148+
return new TestBean(colour.toString());
149+
}
150+
}
151+
152+
153+
@Configuration
154+
static class ColorConfig {
155+
156+
@Bean
157+
public Colour colour() {
158+
return Colour.RED;
159+
}
160+
}
161+
162+
163+
@Configuration
164+
static class AutowiredConstructorConfig {
165+
166+
Colour colour;
167+
168+
@Autowired
169+
AutowiredConstructorConfig(Colour colour) {
170+
this.colour = colour;
171+
}
172+
}
173+
174+
133175
@Configuration
134176
static class ValueConfig {
135177

@@ -155,15 +197,71 @@ public TestBean testBean2() {
155197
}
156198

157199

158-
@Test
159-
public void testCustomProperties() {
160-
ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext(
161-
"AutowiredConfigurationTests-custom.xml", AutowiredConfigurationTests.class);
200+
@Configuration
201+
static class ValueConfigWithProviderFields {
162202

163-
TestBean testBean = factory.getBean("testBean", TestBean.class);
164-
assertThat(testBean.getName(), equalTo("localhost"));
203+
@Value("#{systemProperties[myProp]}")
204+
private Provider<String> name;
205+
206+
private Provider<String> name2;
207+
208+
@Value("#{systemProperties[myProp]}")
209+
public void setName2(Provider<String> name) {
210+
this.name2 = name;
211+
}
212+
213+
@Bean @Scope("prototype")
214+
public TestBean testBean() {
215+
return new TestBean(name.get());
216+
}
217+
218+
@Bean @Scope("prototype")
219+
public TestBean testBean2() {
220+
return new TestBean(name2.get());
221+
}
165222
}
166223

224+
225+
static class ValueConfigWithProviderConstructorArguments {
226+
227+
private final Provider<String> name;
228+
229+
private final Provider<String> name2;
230+
231+
@Autowired
232+
public ValueConfigWithProviderConstructorArguments(@Value("#{systemProperties[myProp]}") Provider<String> name,
233+
@Value("#{systemProperties[myProp]}") Provider<String> name2) {
234+
this.name = name;
235+
this.name2 = name2;
236+
}
237+
238+
@Bean @Scope("prototype")
239+
public TestBean testBean() {
240+
return new TestBean(name.get());
241+
}
242+
243+
@Bean @Scope("prototype")
244+
public TestBean testBean2() {
245+
return new TestBean(name2.get());
246+
}
247+
}
248+
249+
250+
@Configuration
251+
static class ValueConfigWithProviderMethodArguments {
252+
253+
@Bean @Scope("prototype")
254+
public TestBean testBean(@Value("#{systemProperties[myProp]}") Provider<String> name) {
255+
return new TestBean(name.get());
256+
}
257+
258+
@Bean @Scope("prototype")
259+
public TestBean testBean2(@Value("#{systemProperties[myProp]}") Provider<String> name2) {
260+
return new TestBean(name2.get());
261+
}
262+
}
263+
264+
167265
@Configuration
168266
static class PropertiesConfig {
169267

spring-context/src/test/java/org/springframework/context/expression/ApplicationContextExpressionTests.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,10 +23,8 @@
2323

2424
import org.apache.commons.logging.Log;
2525
import org.apache.commons.logging.LogFactory;
26-
import static org.junit.Assert.*;
2726
import org.junit.Test;
2827

29-
import org.springframework.tests.sample.beans.TestBean;
3028
import org.springframework.beans.factory.ObjectFactory;
3129
import org.springframework.beans.factory.annotation.Autowired;
3230
import org.springframework.beans.factory.annotation.Qualifier;
@@ -38,13 +36,16 @@
3836
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
3937
import org.springframework.beans.factory.support.GenericBeanDefinition;
4038
import org.springframework.beans.factory.support.RootBeanDefinition;
41-
import org.springframework.tests.Assume;
42-
import org.springframework.tests.TestGroup;
4339
import org.springframework.context.annotation.AnnotationConfigUtils;
4440
import org.springframework.context.support.GenericApplicationContext;
41+
import org.springframework.tests.Assume;
42+
import org.springframework.tests.TestGroup;
43+
import org.springframework.tests.sample.beans.TestBean;
4544
import org.springframework.util.SerializationTestUtils;
4645
import org.springframework.util.StopWatch;
4746

47+
import static org.junit.Assert.*;
48+
4849
/**
4950
* @author Juergen Hoeller
5051
* @since 3.0
@@ -150,6 +151,7 @@ public String getConversationId() {
150151
ValueTestBean tb3 = ac.getBean("tb3", ValueTestBean.class);
151152
assertEquals("XXXmyNameYYY42ZZZ", tb3.name);
152153
assertEquals(42, tb3.age);
154+
assertEquals(42, tb3.ageFactory.getObject().intValue());
153155
assertEquals("123 UK", tb3.country);
154156
assertEquals("123 UK", tb3.countryFactory.getObject());
155157
System.getProperties().put("country", "US");
@@ -310,6 +312,9 @@ public static class ValueTestBean implements Serializable {
310312
@Autowired @Value("#{mySpecialAttr}")
311313
public int age;
312314

315+
@Value("#{mySpecialAttr}")
316+
public ObjectFactory<Integer> ageFactory;
317+
313318
@Value("${code} #{systemProperties.country}")
314319
public String country;
315320

spring-core/src/main/java/org/springframework/core/convert/ParameterDescriptor.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,9 +32,6 @@ class ParameterDescriptor extends AbstractDescriptor {
3232

3333
public ParameterDescriptor(MethodParameter methodParameter) {
3434
super(methodParameter.getParameterType());
35-
if (methodParameter.getNestingLevel() != 1) {
36-
throw new IllegalArgumentException("MethodParameter argument must have its nestingLevel set to 1");
37-
}
3835
this.methodParameter = methodParameter;
3936
}
4037

0 commit comments

Comments
 (0)