Skip to content

Commit 3ffc0a2

Browse files
committed
SpringBeanAutowiringSupport is able to process @value annotations on any given target instance (SPR-8574)
1 parent acac25b commit 3ffc0a2

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ protected Object doResolveDependency(DependencyDescriptor descriptor, Class<?> t
715715
if (value != null) {
716716
if (value instanceof String) {
717717
String strVal = resolveEmbeddedValue((String) value);
718-
BeanDefinition bd = (containsBean(beanName) ? getMergedBeanDefinition(beanName) : null);
718+
BeanDefinition bd = (beanName != null && containsBean(beanName) ? getMergedBeanDefinition(beanName) : null);
719719
value = evaluateBeanDefinitionString(strVal, bd);
720720
}
721721
TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2002-2011 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.context.support;
18+
19+
import org.junit.Test;
20+
21+
import org.springframework.beans.ITestBean;
22+
import org.springframework.beans.MutablePropertyValues;
23+
import org.springframework.beans.TestBean;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.beans.factory.annotation.Value;
26+
import org.springframework.mock.web.MockServletContext;
27+
import org.springframework.web.context.WebApplicationContext;
28+
29+
import static org.junit.Assert.*;
30+
31+
/**
32+
* @author Juergen Hoeller
33+
*/
34+
public class SpringBeanAutowiringSupportTests {
35+
36+
@Test
37+
public void testProcessInjectionBasedOnServletContext() {
38+
MockServletContext sc = new MockServletContext();
39+
StaticWebApplicationContext wac = new StaticWebApplicationContext();
40+
MutablePropertyValues pvs = new MutablePropertyValues();
41+
pvs.add("name", "tb");
42+
wac.registerSingleton("testBean", TestBean.class, pvs);
43+
wac.setServletContext(sc);
44+
wac.refresh();
45+
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
46+
InjectionTarget target = new InjectionTarget();
47+
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(target, sc);
48+
assertTrue(target.testBean instanceof TestBean);
49+
assertEquals("tb", target.name);
50+
}
51+
52+
53+
public static class InjectionTarget {
54+
55+
@Autowired
56+
public ITestBean testBean;
57+
58+
@Value("#{testBean.name}")
59+
public String name;
60+
}
61+
62+
}

0 commit comments

Comments
 (0)